Namespaces are used in C# to keep one set of names separated from another. This is done to organize the classes so that they are easy to handle. If there are two classes with the same names in different namespaces, they do not conflict with one another.
The syntax for namespace definition is given as follows:
namespace NamespaceName { // code }
Here,
“NamespaceName” is the name of the namespace.
Namespaces are further explained using the following example:
Source Code: Program to implement namespaces in C#
using System; namespace FirstNamespace { public class ClassOne { public void func1() { Console.WriteLine("This is First Namespace"); } } } namespace SecondNamespace { public class ClassTwo { public void func2() { Console.WriteLine("This is Second Namespace"); } } } public class NamespaceDemo { public static void Main() { Console.WriteLine("Namespaces in C#"); FirstNamespace.ClassOne var1 = new FirstNamespace.ClassOne(); SecondNamespace.ClassTwo var2 = new SecondNamespace.ClassTwo(); var1.func1(); var2.func2(); } }
The output of the above program is:
Namespaces in C# This is First Namespace This is Second Namespace
There is the provision for nested namespaces in C#. This means that one namespace can be defined within another.
The syntax for nested namespac is as follows:
// outer namespace namespace Namespace1 { // Code // inner namespace namespace Namespace2 { // Code } }
A program that further demonstrates nested namespaces is as follows:
Source Code: Program to implement namespaces in C#
using System; using outernamespace; using outernamespace.innernamespace; // This is the outer namespace namespace outernamespace { public class outerclass { public void func1() { Console.WriteLine("Outer namespace"); } } // This is the inner namespace namespace innernamespace { public class innerclass { public void func2() { Console.WriteLine("Inner namespace"); } } } } public class Demo { public static void Main(string[] args) { Console.WriteLine("Nested namespaces in C#"); // Object for outer namespace outerclass var1 = new outerclass(); // Object for inner namespace innerclass var2 = new innerclass(); // calling functions var1.func1(); var2.func2(); } }
The output of the above program is as follows:
Nested namespaces in C# Outer namespace Inner namespace
As an alternative of Packages in Java, the C# language has namespace.
The main usage of packages in Java is to control access, to prevent naming conflicts and also to make searching and usage of interfaces, classes, annotations and enumerations easier.
Packages can be defined using the keyword package followed by the package name.
package packagename;
Here, “packagename” is the name of the package
You have to use import statement to mention the specific type of package:
package p1.p2
A namespace provides a way to keep one set of names separate from another. If there are two classes with the same names in different namespaces, they do not conflict with one another.
A namespace definition begins with the keyword namespace followed by the namespace name:
namespace MyNamespace;
Here, “MyNamespace” is the name of the namespace.
Package cannot be nested, but we can easily create nested namespace. One source file in Java can only have one package statement.
We can define an alias for the namespace in C# whereas this is not possible in packages.
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *