Interpreting A C# Class Code Structure

Recently asked on Yahoo! Answers:

C# Code Explanation?

using System;
class Base {

public Base() {
Console.WriteLine("Printing the Base constructor 1!");
}

public Base(int x) {
Console.WriteLine("Printing the Base constructor 2!");
}
}

class Derived : Base {
// implicitly call the Base(int x)
public Derived() : base(10) {
Console.WriteLine("Printing the Derived constructor!");
}
}

class MyClass {
public static void Main() {
// Displays the Base constructor 2 followed by Derived Constructor
Derived d1 = new Derived();
Console.ReadLine();
}
}

This is clearly a sample some professor has put together to illustrate some basic principles behind classes, which are also often called “objects.”

First, let’s get a really rudimentary understanding of what a class is.

A class is a way of combining all sorts of data that relates to a single thing in one place, and a way of associating functions with that data.

For example, you can think of a human as a class.

Humans have certain properties — skin color, height, weight, age, etc. We also have methods, or ways of going about doing things or changing our properties: eating, sleeping, dieting, for example. Finally, we have triggers, or events, that tell us when to employ a method or change properties: hunger, cold, illness, marriage, etc.

In the human class, you are an instance. For example, you might think that God decided to run this code when you were born:

HumanBeing YourNameHere = new HumanBeing();

What He did was create a variable, named YourNameHere, and set the value of that variable to be an instance of the HumanBeing object.

Now that we know what a class is, we can begin interpreting the code.

Namespaces

using System;

This line tells us the classes we create will need to use the System namespace, since certain objects and data types we intend to use are in that namespace.

A namespace is a collection of classes (and, sometimes, other namespaces) that all relate to a common thing.

For example, the US government is very complex, but certain things about it are all in common and relate to one another. The executive, legislative and judicial branches all work in concert, and all as dictated by the Constitution.

So, to define those relationships, and help each branch better interact with other branches, as well as other types of government agencies / employees in the same branch, we would use namespaces.

For example, if we had a namespace called Government, it might contain the classes / objects / namespaces President, Congress, Supreme Court, Department of Justice, Social Security, etc.

If we had a namespace named Congress, we might employ it in a C# program by calling:

using Government.Congress;

If we wanted the Senate namespace, we might call:

using Government.Congress.Senate;

Within the Senate namespace, we might have the class Senator, with properties such as Name, State and Party; methods such as MakeSpeech, ShakeHands or KissBabies; and events such as OnBeingInterviewed, OnMakingSpeech or OnBeingIndicted.

Constructors

OK, with namespaces covered, let’s get back to the code:

class Base {
{
//{snip}
}

This tells us we are about to declare a class that will have the name Base.

If you’ve ever seen a line such as:

SqlCommand myCommand = New SqlCommand();

that line is declaring a variable named myCommand, using the class SqlCommand. myCommand is the name of the variable that is creating an instance of the class SqlCommand.

Using our government metaphor as another example, Senator might be a class; Hillary Clinton would be an instance of that class.

public Base()
{
Console.WriteLine("Printing the Base constructor 1!");
}

This is the the default constructor for the class.

A constructor is a way of making sure that every time you create an instance of the class, there are certain properties or methods set for that instance. In C#, constructors either are named the same name as the name of the class, or use the code in the Main() function.

For example, if we were going to make a new Senator, and the Senator class had a constructor that took the arguments name, party and state:

Senator mySenator = new Senator('JohnQPublic', 'Democrat', 'Oregon');

We would be telling the class constructor that we are making a new Senator; that mySenator.Name will equal JohnQPublic; that mySenator.Party will be Democrat, and that mySenator.State will be Oregon.

The first Base() constructor doesn’t take arguments or set properties; it simply echos out a message.

public Base(int x)
{
Console.WriteLine("Printing the Base constructor 2!");
}

This is a second constructor available to the class. This constructor takes an integer as an argument, but it doesn’t use that integer in any way.

Derived Classes

class Derived : Base
{
//{snip}
}

This code tell us that we will have a “child” class that draws on the “parent” class named Base. “Child” classes are often called subclasses or, as it is called here, a derived class.

Sometimes, we want to have a class that is a lot like another class, but with special properties, methods and events that are different from the other class.

For example, using our government metaphor again, we might want to have a class named RetiredSenator.

We would still want the name, party and state of the RetiredSenator — properties that are common with the Senator class — but we might want some new properties, such as LastElectionWon, HasSecretServiceProtection or NewOccupation, that are unique to the RetiredSenator class.

In that case, it would make the most sense to have RetiredSenator be a derived class of Senator.

public Derived() : base(10)
{
Console.WriteLine("Printing the Derived constructor!");
}

This is the constructor for the Derived class. It sets the default constructor for the Derived class to be the Base(int x) constructor in the Base class, and always supplies the x argument for that constructor with the value 10.

Because this is the only constructor available in Derived, any time we create a Derived, it will automatically make itself a Base. In the case of the Base(int x) constructor, the x argument means nothing; however, every time we create a Derived, the Base(int x) constructor will be used, and the x argument will always be 10.

Custom Class

class MyClass
{
//{snip}
{

This defines another class; this time, it will be called MyClass.

public static void Main()
{
// Displays the Base constructor 2 followed by Derived Constructor
Derived d1 = new Derived();
Console.ReadLine();
}

This acts as a constructor for MyClass. Specifically, it creates a new variable, d1, as an instance of the Derived class. Then, it reads in whatever text the user has entered into the keyboard (I’m not sure why).

So, if you ever were to create a MyClass object:

MyClass somename = new MyClass()

The MyClass would first create a new Derived() object named d1, then would read in whatever you had entered into the keyboard.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Check out the Commenting Guidelines before commenting, please!
  • Want to share code? Please put it into a GitHub Gist, CodePen or pastebin and link to that in your comment.
  • Just have a line or two of markup? Wrap them in an appropriate SyntaxHighlighter Evolved shortcode for your programming language, please!