Tuesday, 10 July 2012

Eighth Class (Name Space,Structure & Enumeration

Material of eighth class (user defined namespace, structure and Enumeration)

namespace : Its a collection classes and sub namespace.

two type of namespace
1. predefined namespace
2. user defined namespace


1. predefined namespace

which we use already defined that`s called predefined like System namespace.

2. user defined namespace

we can create our own namespace and can use functionality many times after making a namespace.

Example 1:


using System;
namespace Believe
{

class Func
{
static int  a, b, c;
public static void Sum()
{
                        // always method should be static
a=2;b=3;
c=a+b;

                        Console.WriteLine("Sum of two no is :"+c);
}
public static void Sub()
{
                        // always method should be static
a=2;b=3;
c=a-b;
Console.WriteLine("Sum of two no is :"+c);
}

}

}
class entry
{
public static void Main()
{
Believe.Func.Sum();
Believe.Func.Sub();
}
}

Structure Data type

Define : Hold different type of data type variable in a single structure variable.
its a value type data type.
use struct keyword to use structure.


using System;
struct book_details
{
public int Book_id;
public string Book_name;
public double Book_cost;
};
class Store
{
public static void Main()
{
book_details b=new book_details();
b.Book_id=1234;
b.Book_name="Dunya Infocom";
b.Book_cost=2003.22;
Console.WriteLine("Book ID is : "+b.Book_id);
Console.WriteLine("Book Name is : "+b.Book_name);
Console.WriteLine("Book cost is : "+b.Book_cost);
}
}

Structure cannot be default constructor. Here book_details() is not a constructor.

Enumeration Data type


use enum keyword to use enumeration
its a value type data type.
always start index at 0.

using System;
class enumeration
{
enum Days{sun, mon, tues, wed=10, thur, fri, sat};
public static void Main()
{
int nameoffirstdays= (int)Days.sun;
int nameoflastdays=(int)Days.sat;
Console.WriteLine("Name of first days : "+nameoffirstdays);
Console.WriteLine("Name of last days : "+nameoflastdays);
}
}

Output 

No comments:

Post a Comment