Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Namespace

380 views

Published on

Published in: Education, Technology, Design
  • Be the first to comment

  • Be the first to like this

Namespace

  1. 1. Namespace • A namespace defines a declarative region that provides a way to keep one set of names separate from another. • Names declared in one namespace will not conflict with the same names declared in another. • The namespace used by the .NET Framework library (which is the C# library) is System. • This is why you have included near the top of every program. using System;
  2. 2. • Namespaces are important because there has been an explosion of variable, method, property, and class names over the past few years. • These include library routines, third-party code, and your own code. Without namespaces, all of these names would compete for slots in the global namespace and conflicts would arise. • For example, if your program defined a class called Finder, it could conflict with another class called Finder supplied by a third-party library that your program uses.
  3. 3. • the I/O classes are defined within a namespace subordinate to System called System.IO . There are many other namespaces subordinate to System that hold other parts of the C# library. • A namespace is declared using the namespace keyword. The general form of namespace is shown here: namespace name { // members }
  4. 4. namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  5. 5. class Program { static void Main(string[] args) { int i; Counter.CountDown cd1 = new Counter.CountDown(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  6. 6. • Some important aspects of this program warrant close examination. • since CountDown is declared within the Counter namespace, when an object is created, CountDown must be qualified with Counter, as shown here: • Counter.CountDown cd1 = new Counter.CountDown(10);
  7. 7. Namespaces Prevent Name Conflicts namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  8. 8. namespace Counter2 { class CountDown { public void Count() { Console.WriteLine("This is Count() in the " + "Counter2 namespace."); } } }
  9. 9. class NSDemo2 { static void Main() { Counter.CountDown cd1 = new Counter.CountDown(10); Counter2.CountDown cd2 = new Counter2.CountDown(); int i;
  10. 10. do { i = cd1.Count(); Console.Write(i + " "); } while(i > 0); Console.WriteLine(); cd2.Count(); } } 10 9 8 7 6 5 4 3 2 1 0 This is Count() in the Counter2 namespace.
  11. 11. using • As you would expect, using can also be employed to bring namespaces that you create into view. • There are two forms of the using directive. The first is shown here: using name;
  12. 12. • name specifies the name of the namespace you want to access. This is the form of using that you have already seen. • All of the members defined within the specified namespace are brought into view and can be used without qualification. • A using directive must be specified at the top of each file, prior to any other declarations, or at the start of a namespace body.
  13. 13. using Counter; namespace Counter { class CountDown { int val; public CountDown(int n) { public int Count() { if (val > 0) return val--; else return 0; } } } val = n; }
  14. 14. class Program { static void Main(string[] args) { int i; CountDown cd1 = new CountDown(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  15. 15. A Second Form of using • The using directive has a second form that creates another name, called an alias, for a type or a namespace. This form is shown here: using alias = name; • Alias becomes another name for the type (such as a class type) or namespace specified byname. • Once the alias has been created, it can be used in place of the original name.
  16. 16. using MyCounter = Counter.CountDown; namespace Counter { class CountDown { int val; public CountDown(int n) { val = n; public int Count() { if (val > 0) return val--; else return 0; } } } }
  17. 17. class Program { static void Main(string[] args) { int i; MyCounter cd1 = new MyCounter(10); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); } }
  18. 18. • OnceMyCounter has been specified as another name for Counter.CountDown, it can be used to declare objects without any further namespace qualification. • For example, in the program, this line MyCounter cd1 = new MyCounter(10); • creates a CountDown object.
  19. 19. Namespaces Are Additive • There can be more than one namespace declaration of the same name. • This allows a namespace to be split over several files or even separated within the same file.
  20. 20. Namespaces Are Additive using Counter; namespace Counter{ class CountDown { int val; public CountDown(int n) public int Count() { if (val > 0) return val--; else return 0; } }} { val = n; }
  21. 21. namespace Counter { class CountDo { public void CountDo1() { Console.WriteLine("this is second namespace class"); } } }
  22. 22. class Program{ static void Main(string[] args) {int i; CountDown cd1 = new CountDown(10); CountDo cd11 = new CountDo(); cd11.CountDo1(); do { i = cd1.Count(); Console.Write(i + " "); } while (i > 0); Console.WriteLine(); }}
  23. 23. Namespaces Can Be Nested • One namespace can be nested within another. using System; namespace NS1 { class ClassA { public ClassA() { Console.WriteLine("constructing ClassA"); } }
  24. 24. namespace NS2 { // a nested namespace class ClassB { public ClassB() { Console.WriteLine("constructing ClassB"); } } } } class NestedNSDemo { static void Main() {
  25. 25. • NS1.ClassA a = new NS1.ClassA(); • // NS2.ClassB b = new NS2.ClassB(); // Error!!! NS2 is not in view • NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // this is right • } • }
  26. 26. Using the :: Namespace Alias Qualifier • Although namespaces help prevent name conflicts, they do not completely eliminate them. • One way that a conflict can still occur is when the same name is declared within two different namespaces, and you then try to bring both namespaces into view. • For example, assume that two different namespaces contain a class called MyClass.
  27. 27. • If you attempt to bring these two namespaces into view via using statements, MyClass in the first namespace will conflict with MyClass in the second namespace, causing an ambiguity error. • In this situation, you can use the :: namespace alias qualifier to explicitly specify which namespace is intended.
  28. 28. • The :: operator has this general form: namespace-alias:: identifier • Here, namespace-alias is the name of a namespace alias and identifier is the name of a member of that namespace. • The trouble is that both namespaces, Counter and AnotherCounter, declare a class called CountDown, and both namespaces have been brought into view.
  29. 29. • Thus, to which version of CountDown does the preceding declaration refer? The :: qualifier was designed to handle these types of problems. • To use the :: , you must first define an alias for the namespace you want to qualify. Then, simply qualify the ambiguous element with the alias.
  30. 30. using Counter; using AnotherCounter; // Give Counter an alias called Ctr. using Ctr = Counter; namespace Counter { class CountDown { int val;
  31. 31. public CountDown(int n) { val = n; } }} namespace AnotherCounter { class CountDown { int val; public CountDown(int n) { val = n; } }
  32. 32. class AliasQualifierDemo { static void Main() { // Here, the :: operator // tells the compiler to use the CountDown // that is in the Counter namespace. Ctr::CountDown cd1 = new Ctr::CountDown(10); // ... }} • The use of the :: qualifier removes the ambiguity because it specifies that the CountDown in Ctr (which stands for Counter) is desired, and the program now compiles.

×