Sonali Gogate
Defining Classes
class MyClass { private long ID; public string Name; … public static void main() { … } }
As you can see the syntax is very simple and quite similar to C++.
Class Members
fields
This is a member variable that holds a value.
methods
This is a piece of code that acts on other members such as fields(object
data).
properties
These are also called smart fields
— that is because though these are methods, they appear as fields from outside
the class.
indexers
These are smart arrays. These let you index into objects to get and set
values.
events
These, cause some code to be executed. These get fired by certain action
taken (like key press or mouse move) or after certain time interval (timer
events).
operators
In C# one can overload the standard mathematical operators so as to have
intuitive code.
Modifiers
Modifier |
Application |
Static |
Only one copy of the member (not one per instance of the class). Member is created when application containing the class is loaded and |
Readonly |
Applied to fields. You can only set the value of the field in the constructor. Everywhere |
Const |
Used for fields and defines constants. The difference between this and readonly |
Sealed |
Applied to a class. Ensures that no class is inherited from this class. |
Access Modifiers
In the class definition at the beginning of the article, private
and public are access
modifiers.
In C# we have the following access modifiers —
Access Modifier |
Meaning of this modifier |
Public |
Member is accessible from outside of the class definition. |
Protected |
Member visible only from inside of the class definition (accessible by |
Private |
Member visible only in the scope of the definition (not accessible even |
Internal |
Member is visible in current compilation unit. (Accessible to derived |
Example —
class MyClass
{
public int a; // each field separately needs the
public int b; // modifier
protected int c; // not like C++ where you club
protected int d; // all fields under a modifier
private int e;
}
Constructor
Constructor is a method that is always called when an instance of this class
is created and we can pass parameters to it.
In C# you can have a static
constructor — which means it is called only once — not for each instance of
the class.
In C# there is only one way to instantiate a class — using the new
keyword. And that is when the constructor is called.
MyClass myclass; // constructor not called at this point
…
myclass = new MyClass(); // constructor is called
C# object constructors (except for System.Object) include an invocation of
base class’s constructor before execution of first line of constructor.
Object Cleanup
In C#, in fact in the .Net world itself, the cleanup is done by Garbage
Collector (GC).
The GC keeps track of which objects are being referenced and it has a low
priority thread always scanning to see which objects are no longer referenced.
There is another thread, which does the actual cleanup after calling the object
finalizers.
Note: It is important to understand that you can not depend on the
finalizers being called in any specific order.
Inheritance
In C# there is no multiple inheritance. So a class can inherit only from one
class.
Methods
Main Method
Every C# application must have a main method that is a public
static method.
The main method can be placed in any class. In fact you can define main
method in more than one class and then pass a switch to compiler to indicate
which main method is to be used. (csc MyFile.cs /main:MyClass)
The command line arguments can be passed in as parameters to the main method.
public static void Main(string<> args)
Ref & Out Parameters
Ref Parameter
While passing parameters, if the keyword ref
is used, then it tells the C# compiler that the arguments being passed points to
the same memory as the variables in the calling code. That means, if these are
modified in the called module, the changes go back to the calling module.
Out Parameter
When you do want to send change back to the calling module from the called
module but do have a value coming in from the called module, you would use an out
parameter. To be able to use a ref
parameter, it needs to be initialized in the calling module, while as the out
parameter can be passed in and handled completely in the called module.
Example —
In this example we get the city, state and the postal code.
class CityState { private string City; private string State; private string Code; public CityState() // constructor { this.City = "Mumbai"; this.State = "Maharashtra"; this.Code = "400001"; } // we use out parameters here // we could use ref params too but then they would need to // be initialized in the calling method public void GetCityStatePCode(out string Postalcode, out string CityName, out string StateName) { CityName = this.City; StateName = this.State; PostalCode = this.Code; } } class testCode { public static void main() { CityState citystate = new CityState(); string City; string State; string PostalCode; // If the params were to be ref instead of out we would // need to assign values to them here before calling //the method CityState. GetCityStatePCode(out City, out State, out PostalCode); } }
Overloading
Method overloading in C# works pretty much like in C++. And you would need to
remember that the list of arguments has to be different for each definition of
the overloaded method.
Virtual &Static Methods
A static method exists one per class instead of one per instance.
When you define a virtual method
in the base class, it can be overridden from the derived class by using the override
keyword while defining the method in the derived class.