Sunday, June 21, 2020

C# namespace | nested namespace in c# | C# - Namespaces

C# namespace
C# namespace

It is a logical collection of classes, methods,variables,events,properties etc.
so we can say that namespace in c# (C Sharp) is Logical unit of Classes,methods or function and properties.



You know, namespace is just like of someone address let ramu is from India, ok so here we know that ramu belongs from india and india having 
multiple state,multiple city,multiple District,multiple taluka or multiple village.
similary when we are talkig about namespace in c# so first of all something are predefined by Microsoft which is logical bind in single unit called namespace.

In C# thire are multiple namespace in dotnet 2.0 version having 26 namespace and in these namespace thousands of classes are available.

let see:
System -- it is also namespace in c# and in this namespace having multiple classes, like Console class,AppContext class,AppDomain Class,
Activator class,Arry class,Attribute class,ConsoleCancelEventArg Class etc.. 
Namespace having also exist namespace called sub namespace. if we are taking about sub namespace of System so system namespace having multiple 
sub namespace like Collection namespace,Text namespace ,Linq namespace,Threading namespace,CodeDom Namespace,Configuration namespace,ComponentModel
namespace,Data,Dynamic,IO,Media,Net etc..

Namespace having also consist event(it is also called delegate. about delegate I discuss in latter article.). So in namespace having events are
Action,AppDomainInitializer,AssemblyLoadEventHandler.
C# namespace | nested namespace in c# | C# - Namespaces
C# namespace | nested namespace in c# | C# - Namespaces 


For Interview:
Namespace is Logical collection of namespace,classes,Events, Methods,Struct,Enum etc.
By the 'using' keyword we are going to add or use namespace so that we are able to access all the classes and methods,properties,events in our 
applications.


As per above discussion all namespace are predefine by the microsoft, but we are also create custome namespace suppose we have develope One Library also called dll
so first of all see herichery

                        Namespcae
                            |
                          Class/Struct/Enum/Event
                             |
                           Methods/Function/Variables

how to use namespace in C#
By the 'Using' keyword like
using System;

I hope Namspace topic, it is clear all of you:

#region By:Dheeraj Kumar Jha (dotnetshala)


class ClsEmployee
{
    public static void Main()
    {
        Console.WriteLine("Welcome to dotnerShala");  // here we have got error in console because address of console is not exist.
        // that's why we have to need use namespace called system beacuse console class belong from System namespace.
    }
}
#endregion



#region By:Dheeraj Kumar Jha (dotnetshala)
using System;

class ClsEmployee
{
    public static void Main()
    {
        Console.WriteLine("Welcome to dotnerShala");
    }
}
#endregion
C# namespace | nested namespace in c# | C# - Namespaces
C# namespace | nested namespace in c# | C# - Namespaces 


As per above example Colnsole is a Static class which comes under system namespace. Inside the Console class having multiple static methods
like Read(),ReadLine(),Write(),WriteLine(),SetIn(),SetOut()  etc.

by the namespace keyword we are going to create custom namespace.
namespace Demo{
   class ClsEmployee{

}
class ClsHR
{

}
}
In above example demo namespace contain two class so that namespace can contain multiple classes.




No comments:

Post a Comment

If you have any query kindly let me know.

Blazor drawback| drawback of blazor| Disadvantage of blazor in c#

  Blazor drawback| drawback of blazor| Disadvantage of blazor in c# While Blazor offers many advantages, it also has a few drawbacks to cons...