Website Design United States, Website Design California, Website Designing United States, Website Designing California

Using Namespaces - C#

Namespaces help you organize your code. True Object-Oriented Programming means you will be re-using old classes; the odds of ending up with two classes with the same name are greatly increased because of this. C#'s answer to this is namespaces, they allow you to label a section of code and refer to it using that label (much like referring to a file within a particular folder. Namespaces should be used on all class libraries so you can specifically identify them during re-use.
using System;

//This class is not contained within a namespace
class SameName
{
public int i=10;
}

namespace NameSpaceExample
{
/*Even though I am declaring a class with the same name as above
they both can be used in the same program because the second occurence
is within this namespace;*/
class SameName
{
public int i=20;
}
}
class TestItAll
{
static void Main()
{
//declare n1 as the class SameName which is NOT within a namespace
SameName n1 = new SameName();
//declare n2 as the class SameName which is contained within the namespace NameSpaceExample
NameSpaceExample.SameName n2 = new NameSpaceExample.SameName();
System.Console.WriteLine("Without referencing the namespace i="+n1.i+" from class SameName.");
System.Console.WriteLine("When the namespace is referenced i="+n2.i+" from class SameName.");
System.Console.WriteLine("<Press Enter To Quit>");
System.Console.Read();
}
}


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com