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....

Monday, June 29, 2009

C# string handling - Part 1

We have lots of string handling functions in C#.net. We are just going to see some basic

functions.

C# string Split() - Splitting a string to an Array

The split() can be used to Split an entire string in to an string array. This will be

done using this split method. This can be used when you need long strings to be splitted

to seperate string objects.

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

string mString = "This is to show Splitting in strings";
string[] mArr = mString.Split(' ');

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

Here in the above code we have a string named mString and we have assigned a string to

it. Now check the second line of it. We have created an String Array to store the values

that gets Splitted.
mString.Split(' ') method splits the Entire string into an array. We have given a white

space as the argument for the Split method for the string. This will allow it to split

the string whenever it sees a white space inside the string.

Now, if you need to get the Splitted values of the string you must have to fetch the

entire items of the Array mArr using some loops.


C# string.Join() - Joining an Array as a String

The Join() method can be used to join an Array to a single String. If we have an array

to be used as a single string you can use this string.Join() method.
Let's see an example for it

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

string joinedString = string.Join("-", mArr);

//Here the JoinedString variables value will be
// "This-is-to-show-Splitting-in-strings"

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

In the above code in the Join function we have two overloads mainly
string.Join("String Separator","The array to Join");
Here in the above we have set the Separator as "-"(hyphen). So it will attach each Array

element with this "-". Like wise we can join the Strings in any way we like.

Wednesday, June 17, 2009

Write a Program to show numbers in Fibonacci Series in C#??

Fibonacci series is a pattern in which digits are shown in a manner that it adds the last two digit from the series ie..
0 1 1 2 3 5 8......... n..

Here, the first no is 0 and the second no is 1 so the third number must be 0+1, and the fourth number must be Sum of Second and Third.. ie 1+1, like wise we must create it for the Entire Series....

Here is the Program to show the first ten digits in Fibonacci Series in C#...
----------------------------------------------
using System;

namespace MyFibonacciApp
{
class _FibonacciSeries
{
static void Main(string[] args)
{
int j = 0;
int k = 0;
int l = 1;
for(int i = 0; i <10;i++)
{
Console.Write("{0} ", k);
j = l;
l = k;
k = l + j;
}
}
}
}
----------------------------------