Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, 29 November 2011

C# readonly fields

The concept of constant variables is present in almost all of the programming languages. Just to recall, constant variables are those variables whose values are fixed and cannot be changed.

readonly fields is a relatively new concept in C#. Sometimes we may have some variable whose value we need to fix, but the value is not known until runtime. In these situations C# readonly fields come to help us. A readonly field is more flexible than a simple constant variable. Please note that in C# we use the const keyword to declare a variable as constant. A readonly field helps us in situations where we need a field to be constant but also need to carry out some calculations to determine its initial value.

The rule is very simple. We can assign values to readonly fields inside a constructor. Do not do it anywhere else or an error will result. A readonly field can be static or non-static. A static readonly field is shared among all instances of the class and it can also be used without creating an instance of the class. On the other hand a non-static readonly field is an instance field and is tied to a single object of the class.

Example:

public class ReadOnlyEx
{
public readonly int count;
public ReadOnlyEx()
{
count = 1;
}
}

The above code is legal. Now consider the following piece of code:

public class ReadOnlyEx
{
public readonly int count;
public ReadOnlyEx()
{
......
}
public SomeMethod()
{
count = 1; //Compile time error
}
}

The above code is not valid. It will generate a compile time error. readonly fields can only be assigned a value inside constructors. If you do not provide a value to a readonly field inside a constructor, it will contain the default value, depending on its data type or the value you assigned to it while declaring the field, if any. This rule applies to both statice readonly fields and the non-static readonly fields.

Monday, 28 November 2011

C# Enum


A C# enum (enumeration) is a user-defined integer TYPE. When we declare an enum in C# we have to supply a set of acceptable values (integers) that the instances of the enum type can hold. We have the freedom of naming those given values to make it more developer friendly. You will appreciate the real power of a C# enum from the fact that when, somewhere in your code, you attempt to assign an illegal value to an enum type, the code does not compile.

We benefit from a C# enum in a number of ways. Enums make our code more readable and, therefore, easier to maintain. With enums we no longer need to use those “magic” numbers in our code, we replace those magic numbers by descriptive names. Enums make our code easier to type, too. Whenever we need to supply a value to an enum type, the Visual Studio.NET’s IntelliSense helps us by providing a list of acceptable values to choose from.


We can define an enumeration as follows:

public enum DaysOfWeek
{
Friday = 0,
Saturday = 1,
Sunday = 2,
Monday = 3,
Tuesday = 4,
Wednesday = 5,
Thursday = 6
}

In the above DaysOfWeek enum type , we assign an integer value to represent each day of the week. To access the DaysOfWeek enum we use the ‘.’ notation. For example to DaysOfWeek.Friday would return the value 0 and DaysOfWeek.Thursday would return the integer value 6.

We can retrieve the string representation of an elements of an enum by calling the ToString() method. For example the following line of code would print Friday on the console:

Console.WriteLine(DaysOfWeek.Friday.ToString());

One more fact worth appreciating is that behind the curtains C# enums are instantiated as structs. To recall, the structs are value types and unlike reference types, that are created on the heap area of memory, they are created on the stack, as do ints, floats and chars etc., and are faster than the reference types. All the enums in C# are derived from the base class System.Enum.