Presentation is loading. Please wait.

Presentation is loading. Please wait.

Namespaces Group related C# features into categories

Similar presentations


Presentation on theme: "Namespaces Group related C# features into categories"— Presentation transcript:

1 Namespaces Group related C# features into categories
Contain code that can be reused .NET framework library (FCL) contains many namespaces Must be referenced with using directive in order to be used Example: Console feature is in namespace System

2 C# namespaces- allow you to group classes as a collection
 Standard C# .NET Framework library has namespaces: System and System.Linq … Also hierarchical: System.Collections.Generic WHY? Guarantee uniqueness of class names NOTE: no logical relationship between namespaces … just physical

3 USING namespaces Can use all classes within namespace and all public classes in other classes as long as you 1. Qualify System.Collections.ArrayList 2. using namespace using System.Collections; 3. using a class in a namespace using MyNameSpace.MyClass;

4 careful with name conflicts
EXAMPLE using System.DateTime; using System.Net.HttpRequestHeader; //both have Date class SOLNs: using System.DateTime.Date; to use this one use qualifiers if you need both.

5 HOW TO ADD A CLASS TO A namespace
Create a new project of type Class Library Add code defining public class MyNameSpace { class MyClass{} } Compile to create a .dll file

6 HOW TO ADD A CLASS TO A namespace
In project using class: Add a reference to the class library in an app Right click on project in solution explorer and select Add Reference In code using class Specify a using directive using MyNameSpace;

7 Directory structure Put source files of namespace in subdirectory of project directory . (projectdirectory) ManipCN.java MyNumericsNameSpace ComplexNumber.cs Build in project directory

8 TO SHARE namespaces Need to have special directory. I called mine C:\Csharp\classes NOTE: Csharp is my BASE directory I put files ComplexNumber.java in C:\Csharp\classes\ I put ManipCN.cs in C:\Csharp


Download ppt "Namespaces Group related C# features into categories"

Similar presentations


Ads by Google