Вы находитесь на странице: 1из 28

Session 6

Total.net / Programming in C# / Session 6 / 1 of 28

Session Objectives
Explain Namespaces Create custom Namespaces Discuss types of Namespace naming Create alias for Namespaces Use namespace directives Implement .net base classes libraries
Total.net / Programming in C# / Session 6 / 2 of 28

Namespaces
Used to avoid naming conflicts Elements designed to help you organize your code.

Reduces complexity when code is to be re-used for some other application.

Total.net / Programming in C# / Session 6 / 3 of 28

Declaring a Namespace (1)


Declaration similar to classes Syntax -

Total.net / Programming in C# / Session 6 / 4 of 28

Declaring a Namespace (2)

Total.net / Programming in C# / Session 6 / 5 of 28

Declaring a Namespace (3)

Total.net / Programming in C# / Session 6 / 6 of 28

Nested Namespaces (1)


Declaring another namespace within a namespace.

Total.net / Programming in C# / Session 6 / 7 of 28

Shortcut to declare namespaces -

Nested Namespaces (2)

Total.net / Programming in C# / Session 6 / 8 of 28

Access Modifiers & Namespaces


Namespaces are public implicitly. Namespaces cannot be protected, private or internal.

Total.net / Programming in C# / Session 6 / 9 of 28

To use a class inside its namespace, we need to specify the class name. To use a class outside a namespace it has to be referred by using its fully qualified name.

Qualified Naming (1)

Syntax - Namespace.class name

Total.net / Programming in C# / Session 6 / 10 of 28

Qualified Naming (2)

Total.net / Programming in C# / Session 6 / 11 of 28

Qualified Naming (3)

Total.net / Programming in C# / Session 6 / 12 of 28

using namespace Directives (1)


With qualified naming the code gets longer and confusing.

With using namespaces long confusing names can be made short and meaningful.

Total.net / Programming in C# / Session 6 / 13 of 28

using namespace Directives (2)

Total.net / Programming in C# / Session 6 / 14 of 28

Ambiguous Names (1)

This leads to an error due to the ambiguity in class name.

Total.net / Programming in C# / Session 6 / 15 of 28

Ambiguous Names (2)

Total.net / Programming in C# / Session 6 / 16 of 28

using alias Directives


using alias directives provides us with the facility to pull just one class from the namespace into the scope.

using alias_name = fully qualified path to the namespace or class.


Total.net / Programming in C# / Session 6 / 17 of 28

Base Class Is a Library (1) that you can mass of huge pre-written code easily incorporate and use in your code.
Base Class Library is shared among all .net supported languages. Classes in the BCL are categorized into namespaces based on their functionality.

Total.net / Programming in C# / Session 6 / 18 of 28

Base Class Library (2)


Most commonly used namespaces -

Total.net / Programming in C# / Session 6 / 19 of 28

System.Array namespace (1)


Provides us with classes and methods for manipulating arrays.

Total.net / Programming in C# / Session 6 / 20 of 28

System.Array namespace (2)


Output

Total.net / Programming in C# / Session 6 / 21 of 28

System.Array namespace (3)


Other methods of the System.Array class -

Total.net / Programming in C# / Session 6 / 22 of 28

System.Threading namespace (1)


To implement multi-threading into our programs. Running one or more instances of either the same program or parts of the program simultaneously.

Total.net / Programming in C# / Session 6 / 23 of 28

System.Threading namespace (2)


Example -

Total.net / Programming in C# / Session 6 / 24 of 28

Synchronization of Threads
Example -

Total.net / Programming in C# / Session 6 / 25 of 28

For operations on files.

System.IO namespace (1)

Total.net / Programming in C# / Session 6 / 26 of 28

System.IO namespace (2)

Total.net / Programming in C# / Session 6 / 27 of 28

Other notable classes -

System.IO namespace (3)

Total.net / Programming in C# / Session 6 / 28 of 28

Вам также может понравиться