Horizontal Nav

Friday, 28 June 2013

C# Enum


Enums store special values. They make programs simpler. If you place constants directly where used, your C# program becomes complex. It becomes hard to change. Enums instead keep these magic constants in a distinct type.

Example

In this first example, we see an enum type that indicates the importance of something. An enum type internally contains an enumerator list. We use enums when we want readable code that uses constants. 
An enum type is a distinct value type that declares a set of named constants.
Hejlsberg et al., p. 585
Program that uses enums: C#

using System;
class Program
{
    enum Importance
    {
None,
Trivial,
Regular,
Important,
Critical
    };

    static void Main()
    {
// 1.
Importance value = Importance.Critical;

// 2.
if (value == Importance.Trivial)
{
   Console.WriteLine("Not true");
}
else if (value == Importance.Critical)
{
   Console.WriteLine("True");
}
    }
}

Output
True


We see a new variable with the identifier "value" is of the Importance type. It is initialized to Importance.Critical in part 1. At part 2, the variable is tested against other enum constants.

Tip:Enums can be used with IntelliSense in Visual Studio. Visual Studio will guess the value you are typing.
So:You can simply press tab and select the enum type you want. This is an advantage to using enum types.

Saturday, 15 June 2013

Difference Between Parameter and Argument

Parameter
Parameter is a variable in the declaration of function.
int add(int x,int y)
{
return x+y;
}
In this example int x,int y are parameters.
Argument
Argument is the actual value of this variable that gets passed to function.
E.g.
int result;
result=add(1,2);

In this example 1,2 are arguments.

Friday, 10 February 2012

What is delegate in C#?


Basically it is similar like the old "C" age function pointer, where functions can be assigned like a variable and called in the run time based on dynamic conditions. C# delegate is the smarter version of function pointer which helps software architects a lot, especially while utilizing design patterns.

At first, a delegate is defined with a specific signature (return type, parameter type and order etc). To invoke a delegate object, one or more methods are required with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a reference of a function. The function will then can be called via the delegate object.

Sounds easy? If not lets have a look in the code snippets below.

1. Defining the delegate

public delegate int Calculate (int value1, int value2);

 2. Creating methods which will be assigned to delegate object

//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegate

public int add(int value1, int value2)
{    return value1 + value2;            }

//a method, that will be assigned to delegate objects
//having the EXACT signature of the delegate
public int sub( int value1, int value2)
{    return value1 - value2;            }

 3. Creating the delegate object and assigning methods to those delegate objects

//creating the class which contains the methods
//that will be assigned to delegate objects
MyClass mc = new MyClass();
//creating delegate objects and assigning appropriate methods
//having the EXACT signature of the delegate
Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);

 4. Calling the methods via delegate objects

//using the delegate objects to call the assigned methods
Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));