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.
Continue reading ‘Interpreting A C# Class Code Structure’ »