Tuesday 6 March 2012

Structures in C#


// Structure in Depth
using System;

struct Person
{
//Private data declaration
string firstName,lastName;
int age;
Gender g;

//Declaring Enumeration
public enum Gender{Male,Female};

//Constructors
public Person(string firstName,string lastName,int age,Gender g)
{
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
this.g=g;
}

//override ToString() Method
public override string ToString()
{
return firstName+" "+lastName+" "+age.ToString()+" "+g.ToString();
}

//Main Method
static void Main()
{
Person obj=new Person("Ankur","Bhatnagar",26,Person.Gender.Male);
Console.WriteLine(obj);
}
}

No comments:

Post a Comment