Start C# here

Study C#.. Go ahead ! !

Wednesday, July 8, 2009

C# Enum - Enumerator

An enumerator is a Distinct Type containg a set of Named Constants called Enumerator List. We create an Enumerator with the enum keyword. Enumerator can be of any integer type.

Let's see an example for that
--------------------------------------
enum Gender { Male, Female };
or
enum Gender : int { Male, Female };
--------------------------------------

The above one is and Enumerator with the name Gender. Now we can get the values Male or Female using the enumerator Gender. By default the Value of the First element of the enumerator will be 0 and the rest will be increased by 1.

If you need to use the Enumerator then you can access it using the Gender keyword.
--------------------------------------
Console.WriteLine(Gender.Male);
or
Console.WriteLine(Gender.Female);
--------------------------------------

and if you need to take the value of this Enumerator you can do it like this..
--------------------------------------
int i=(int)Gender.Male;
--------------------------------------

Here in the above code the value will be 0 for the variable i, and if you can take Female the value will be 1.


You can change the values of the enumerator elements in manual.

--------------------------------------
enum Gender { Male=1, Female };
--------------------------------------

Here we forcefully change the value of the first element of the enumerator, so all the rest will be having values after that.

Now let's See and Example for C# Enumerator..
--------------------------------------
using System;

namespace ConsoleApplication1
{
class Program
{
//Creating an Enumerator
enum WeekDays { Sunday=1, Monday,Tuesday, Wednesday,Thursday, Friday, Saturday};

static void Main(string[] args)
{
Console.WriteLine("The value for the week day {0} is {1}",WeekDays.Thursday, (int)WeekDays.Thursday);
}
}
}
--------------------------------------

The output of the Program will be:-

The value for the week day Thursday is 5.

Monday, July 6, 2009

C# Interface

An interface is Created using the interface Keyword.
Here is an example for the Interface program.

----------------------------------------

interface IStudent
{
int RollNo
{
get;
set;
}
string Name
{
get;
set;
}

void DispDetails();

}
-----------------------------------------

The above one is an interface named IStudent. In this there are two Properties namely RollNo and Name. There is also a Method named DispDetails(). If you look at the codes... we don't have any definitions for these properties and Method(s).

Now, we will check how we can implement these codes inside a Class.

Exaple for C# Interface:-
-----------------------------------------
//Inheriting interface IStudent to class MySchool
class MySchool : IStudent
{
int iRollNo = 0;
string sName = "";]

//Definition for Property Declared inside the Interface
public int RollNo
{
get { return iRollNo; }
set { iRollNo = value; }
}
public string Name
{
get { return sName; }
set { sName = value; }
}
//Definition for Method Declared inside the Interface.
public void DispDetails()
{
Console.WriteLine("The RollNo is:{0} and the Name is {1}", iRollNo, sName);
}

static void Main(string[] args)
{
MySchool sObj = new MySchool();
sObj.RollNo = 12500;
sObj.Name = "SMILU";

sObj.DispDetails();
}
}

--------------------------------------------

In the above class MySchool we have Inherited the IStudent interface. So it's mandatory that we must write the Definitions for those methods and Properties declared inside the Interface.
Now, If in case we don't give a definition for any method or property declared inside the Interface, it will throw Error(s) and we will not be able to execute the program.

So, its necessary that there must be definition for all the methods and Properties which we have inside our Interface.

What is an Interface in C#?

An Interface is an abstract type which we use to Implement certain Rules to our class.
This will be certain contracts that we need to Implement into our C# class. Interface will not be having any definitions for the methods and properties it has. We just create the Prototype of methods and Properties we need to implement inside the class. Which all classes Inherit these Interfaces Must have the definitions for the Methods and Properties which we have Declared inside the Interface. We create an Interface using the interface keyword.

See an C# Interface EXAMPLE here....