07 Name Space
07 Name Space
com/
[email protected]
Ketki Acharya
From: SM VITA ATC of
CDAC
9769201036
[email protected]
om
using System;
• In this example I have 2 name
space CADac andCADbda both
namespace ConsoleApplicationdemo
{ has classs students.
class Program
{ • Due to namespace there is no
static void Main(string[] args) name collision
{
• We can easily use both class
CADac.students s1 = new CADac.students();
CADbda.students s2 = new CADbda.students();
s1dacresult(); because they both are in
}
s2.dbdaresult();
different name space
}
}
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Namespace to logically grouping of data
namespace CADac namespace CAStaff
{ {
class Students class Employee
{ {
public void dacresult() public void paysal()
{ Console.WriteLine("Result of DAC students"); { Console.WriteLine("salary given to stadd");
} }
} }
class Reportcard class Address
{ //some code { //some code
} }
} class visitingfaculty
using System;
{ //some code
using CADac; }
using CAStaff; }
namespace ConsoleApplicationdemo You can see in this example all Dac related class are in namespace CADac
{ name space
class Program And all staff related class are in CAStaff name space. So name space help us
to logically grouping of related classes. In entry point class we have added
{ namespace CADac, , CAStaff
static void Main(string[] args)
{ Students s1 = new Students(); In Dot Net frame work all class realtered to file handling is in namespace
System.io;
s1.dacresult();
Reportcard r1 = new Reportcard();
Employee e1 = new Employee();
e1.paysal();
Address a1 = new Address();
}
}
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
using static System.Console;
class Program { static void Main()
{ WriteLine(“hello”);
}
}
the scope of a using directive is limited to the file in which it
appears.
The using directive can appear:
•At the beginning of a source code file, before any namespace or
type definitions.
•In any namespace, but before any namespace or types declared
in this namespace.
USM’s Shriram Mantri Vidyanidhi Info Tech Academy