I was studying my C# programming course material that day and came across an initialisation with a reference parameter.
Case Background
We are making a grade book for a student in his/her class. In the code, we are stimulating a scenario that we input the grade mark into his/her record. Every time, we input a mark, an event will be triggered, which print a message to the compiler.
I am not going to demo all the code in the project, as I mainly focus on the initialisation with a reference parameter practice in this post.
Approach
namespace GradeBook
{
public delegate void GradeAddedDelegate(object sender, EventArgs args);
public class NamedObject
{
public NamedObject(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}
public interface IBook
{
void AddGrade(double grade);
Statistics GetStatistics();
string Name { get; }
event GradeAddedDelegate GradeAdded;
}
public abstract class Book : NamedObject, IBook
{
public Book(string name) : base(name)
{
}
public abstract event GradeAddedDelegate GradeAdded;
public abstract void AddGrade(double grade);
public abstract Statistics GetStatistics();
}
public class DiskBook : Book
{
public DiskBook(string name) : base(name)
{
}
public override event GradeAddedDelegate GradeAdded;
public override void AddGrade(double grade)
{
using(var writer = File.AppendText($"{Name}.txt"))
{
writer.WriteLine(grade);
if(GradeAdded != null)
{
GradeAdded(this, new EventArgs());
}
}
}
public override Statistics GetStatistics()
{
var result = new Statistics();
using(var reader = File.OpenText($"{Name}.txt"))
{
var line = reader.ReadLine();
while(line != null)
{
var number = double.Parse(line);
result.Add(number);
line = reader.ReadLine();
}
}
return result;
}
}
Before we dive into deep, I think we need to have a look at the class arrangement in the above code snippet.
Class & Interface – Three Classes and One Interface In The Code Snippet
There are three classes, NameObject
, Book
and DiskBook
; one interface, IBook
. I prefer using a diagram to analyse class arrangement when it comes to reading other programmers’ code. Here is the diagram I draw.

As you can see in the diagram, NameObject
and IBook
class serve as a base class. NameObject
has a Name
property and an initialiser. Logically, a lot of things could be a NamedObject
. He’s saying a Book
is an IBook
, and also a NamedObject
. Imagine adding a Person
class. That Person
would be a NamedObject
, but not an IBook
. So you can reuse the base class without tying it to a Book
.
Parameter With A Base Keyword
Have a look at the Book class, which is a subclass of NameObject
and IBook
. There is an initialiser with a base type as a parameter.
public Book(string name) : base(name)
using : base(name)
says call the constructor on the base class with this parameter
. So you can be sure that NamedObject
‘s constructor will be called with the string value, setting the Name
property.
DiskBook
class has the same initialiser as well. DiskBook
is using : base(name)
to call the constructor on Book
, which in turn calls the constructor on NamedObject
.
Informative article, just what I needed. https://parbriz-auto-bucuresti.ro/parbriz-alfa_romeo-4c.html
Saved as a favorite, I really like your blog! https://vanzari-parbrize.ro/parbrize/parbrize-mitsubishi.html
I constantly emailed this web site post page to all my friends,
as if like to read it then my contacts will too. https://parbriz-auto-bucuresti.ro/geam-hyundai-elantra.html
What’s up i am kavin, its my first time to commenting anyplace, when i read this article
i thought i could also create comment due to this sensible piece
of writing. https://vanzari-parbrize.ro/parbrize/parbrize-dacia.html
Hi to all, the contents existing at this web page are truly amazing for people knowledge, well, keep up the nice work fellows. https://parbrizeladomiciliu.ro/luneta-volkswagen-bora.html
I pay a visit every day a few websites and sites to read content, however this blog offers feature based writing. https://parbrize-originale.ro/parbrize-originale/parbriz-originale-volkswagen-touareg_7l7-2010-289599.html
I am genuinely thankful to the holder of this web site who has shared this
impressive paragraph at at this time. https://anunturi-parbrize.ro/index.php?page=21401
Highly energetic blog, I liked that bit. Will there be a part
2? https://parbriz-auto-bucuresti.ro/parbriz-nissan-nv_400.html
I think what you published was very reasonable. But,
think about this, suppose you were to create a awesome headline?
I am not saying your information is not solid., however
what if you added something that grabbed a person’s attention?
I mean C# OOP Case Study: Initialization with a base parameter – Nan's Blog is kinda boring.
You could look at Yahoo’s home page and see how they
write news headlines to grab viewers interested.
You might add a related video or a picture or two to get people interested about everything’ve got to say.
Just my opinion, it could bring your posts a little bit more interesting. https://parbrizeladomiciliu.ro/parbriz-mercedes-r_class.html
continuously i used to read smaller posts that as well clear their
motive, and that is also happening with this article which
I am reading at this time. https://parbrize-originale.ro/parbrize-originale/parbriz-originale-mitsubishi-lancer_viii-2019-176157.html
Hi to every body, it’s my first visit of this webpage; this website carries amazing and in fact
good stuff in support of readers. https://anunturi-parbrize.ro/index.php?page=31856
I truly love your blog.. Excellent colors & theme.
Did you make this site yourself? Please reply back as I’m attempting to create my own personal site and would love to
find out where you got this from or exactly what the
theme is called. Thank you! https://parbriz-auto-bucuresti.ro/luneta-kia-opirus.html
I’m also commenting to let you be aware of of the brilliant discovery my child encountered checking your blog. She came to understand a lot of details, including how it is like to have a marvelous coaching heart to get folks easily fully grasp a variety of complex issues. You actually did more than my desires. I appreciate you for delivering such good, dependable, edifying and also cool guidance on that topic to Ethel.
I simply wanted to thank you very much once more. I do not know what I would have tried without these techniques shared by you regarding that subject matter. It was actually an absolute scary scenario in my position, nevertheless taking note of this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus sincerely hope you really know what an amazing job you have been putting in training the mediocre ones via your webpage. Most probably you haven’t met any of us.
I am commenting to let you know of the fabulous encounter my cousin’s daughter experienced reading your site. She even learned many issues, not to mention what it’s like to have a wonderful giving mindset to have many people just grasp chosen grueling subject areas. You really exceeded our expectations. Many thanks for distributing the informative, healthy, educational and as well as easy thoughts on the topic to Jane.
Thank you for every one of your labor on this web site. My daughter really loves managing investigations and it’s really obvious why. Most people learn all regarding the lively mode you present rewarding techniques through this web site and even inspire participation from some others on the point then our favorite princess is really starting to learn a whole lot. Take advantage of the rest of the year. You are always doing a splendid job.
I am only writing to make you understand what a superb experience our princess undergone viewing your webblog. She picked up such a lot of things, with the inclusion of what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tricky topics. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Sandra.
I am only writing to make you understand what a remarkable experience our princess undergone viewing your webblog. She picked up some things, which included what it is like to possess an ideal teaching spirit to make the mediocre ones without difficulty master specified tortuous things. You undoubtedly surpassed readers’ expected results. Thanks for rendering those useful, trustworthy, revealing as well as unique tips on your topic to Mary.