Adsence750x90

Friday, May 22, 2009

Partial Classes in C#.Net - examples usage definition

Definition :Partial types are allow to define a single class in multiple files (more than one file), when compilation time theses classes are combined to form a single class.

The partial modifier is not available on delegate or enumeration declarations.

Example
//partial class


public partial class Student

{
public virtual void GetRollNo();
}

public partial class Student
{
public virtual void GetStudentName();
}

//Derived class

public class School : Student
{
public void getStudentDetails()
{
GetRollNo();
getStudentDetails();
}
}

Monday, May 18, 2009

What is an Interface? CSharp tutorials and sample code

What is an Interface?
Interface is defined as set of methods properties evnets and fields.
The scope of an inetrface is public.interface contain only signature.(it contain only definition no implementation)
Implementation are done in derived class.Only the interface can support multiple inheritance in C#.



//Example of Interface
interface ITestInterface
{
void TestMethod();
//Declaring Properties in interface
int Count
{
get;
set;
}
//Read only Property
string Item
{
get;
}
//Write only Property
string Name
{
set;
}

}

//interface Implementation are done in derived class.so we are going to inherit ITestInterface in a class named InterfaceImplimentation
public class InterfaceImplimentation:ITestInterface
{
public InterfaceImplimentation()
{
}

#region ITestInterface Members

public void TestMethod()
{
//Write Implimentation
}
private int y;
private string z = string.Empty;
public int Count
{
get
{
return y;
}
set
{
y = value;
}
}
public string Item
{
get
{
return "Item1";
}
}
public string Name
{
set
{
z = value;
}
}

#endregion
}


//implimentation


public class Implementation
{
InterfaceImplimentation if1 = new InterfaceImplimentation();
public void GetItem()
{
if1.TestMethod();
}
}