C# notes


25
Jul 11

Readonly and const in C#

There are two type of constant in C#

  1. const – Compile time constant.
  2. readonly – Runtime constant.

const (Compile time constant) are bit faster but less flexible where readonly (runtime constant) are bit slower in comparison with const but more flexible. (If you change value of const in your class then every client code which using the reference of that class.)

If you define any const and use in you code it actually it replace with the value to const instead of holding the reference of const. Which makes const bit faster in comparison with readonly. But it also make less flexible.

  1. const can only be primitive type (numeric and string) because it has to replace the value to const where it used in code that’s why and any runtime evaluation is not allowed.
  2. If you change the value to const in you code then every assemblies which using that const must be recompile otherwise changes would not be reflect.

readonly can only be initialize while declaration or in constructor but not after that while const can declare in method as well. That make readonly flexible enough to create different constant for each instance of class which is not possible with const. Another flexibility with readonly is it can be any type.

If readonly is refrence type then reference is immutable but actual object is not; means we can change the reference but we can change the same object (like changing the data for its properties or data member).


25
Jun 11

Initialization of variables in C#

If we initialization of variable while declaring it then compiler generates the initialization code at beginning of each constructor. If there is no constructor then compiler generates the default constructor for that class. This initialization code execute before base class constructor and execute in order they declare.

Initialization of member variable while declaring it get rid of the problem of put member variable initialization code in constructor.

If you have to initialization your variable null then do do this because it will automatically initialize to null variable is not initialize.

Only do initialization of those variables which you want to initialize in constructor. If you have to initialize your variable somewhere else then previous initialize object will going to collect by the garbage.

If you do initialization while declaring the variable then you can not catch any exception if there is any. In that case use constructor.

To initialize static variable static constructor are the best place. Static constructor execute before any other method, properties or data member access first time and static constructor are called by CLR so they will be always parameter less.


25
Feb 11

Properties in C#

Properties are methods but accessed as a data member.


class TimeMachine
{
private double seconds;

public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}

class Program
{
static void Main()
{
Timemachine t = new TimeMachine();
// Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24;
// Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
// Output: Time in hours: 24

It makes easier to add any small logic with properties. In above example we have converted seconds in hours within properties.
Most of the time adding validation logic in get and set are handy.
Compiler generates get and set method for the properties. That’s why properties have all the language features of method.

In C# 4 you can declare implicit properties


class TimePeriod
{
public double Hours
{
get;
set;
}
}

In above example compiler will automatically generates the private filed for Hours (also known as backing fields).
There are some problem with implicit properties:
1. We can not initialize the properties as we can do in explicit properties.
2. While debugging you can not put break point on get or set, but with explicit properties we can set break point on get and set.

Both get and set can have different access modifiers which helps us to make read only and write only properties. It is not possible with data member.

Properties looks like data member but generates different MSIL, so if you change any data member to properties you must recompile all the assemblies which using that class.

As properties are method but we should always avoid any computation which take long time, because properties are made for holding data. It is replacement of public data member with some extra capabilities.