Namespaces are used to organize your code.it is the way to organize .NET Framework Class Library into a logical grouping according to their functionality, usability as well as category they should belong to, or we can say Namespaces are logical grouping of types for the purpose of identification.
A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces.By placing code in different sub-namespaces, you can keep your code organized.
The example:
namespace india.CSharp.Namespaces
{
public class Hello
{
public string GetMessage()
{
return "Hello, world";
}
}
}
Namespaces are hierarchical, and the name india.CSharp.Namespaces is actually shorthand for defining a namespace named arun that contains a namespace named CSharp that itself contains a namespace named Namespaces, as in:
namespace india
{
namespace CSharp
{
namespace Namespaces
{....}
}
}
The using keyword has two uses:
Create an alias for a namespace (a using alias).
Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).
Implement the "using" Directive
Next, we'll write a console application that uses the Hello class. We could just use the fully qualified name for the class-india.CSharp.Namespaces.Hello-but this name is quite long and unwieldy. An easier way is to use a "using" directive, which makes it possible to use all of the types in a namespace without qualification. If you would like to call methods without typing their fully qualified name, you can implement the "using" directive.
using sysem;
using india.CSharp.Namespaces;
class Hello
{
static void Main()
{
Hello m = new Hello ();
System.Console.WriteLine(m.GetMessage());
}
}
"using System", is the same "using" directive you have seen in every program in this article. It allows you to type the method names of members of the "System" namespace without typing the word "System" every time. In class Hello(), "Console" is a class member of the "System" namespace with the method "WriteLine". It's fully qualified name is "System.Console.WriteLine(...)".
Note that the two occurrences of Hello are shorthand for india.CSharp.Namespaces.Hello. C# also enables the definition and use of aliases. Such aliases can be useful in situation in which name collisions occur between two libraries, or when a small number of types from a much larger namespace are being used.
Using alias directives
A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.
using identifier = namespace-or-type-name ;
Within member declarations in a compilation unit or namespace body that contains a using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given namespace or type.
For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using A = N1.N2.A;
class B: A {}
}
Here, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B
derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then
referencing R.A:
namespace N3
{
using R = N1.N2;
class B: R.A {}
}
The identifier of a using-alias-directive must be unique within the declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.
Using namespace directives
A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.
using-namespace-directive:using namespace-name ;
Within member declarations in compilation unit or namespace body that contains a using-namespace-directive, the types contained in the given namespace can be referenced directly.
For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using N1.N2;
class B: A {}
}
Here, within member declarations in the N3 namespace, the type members of N1.N2 are directly available, and thus class N3.B derives from class N1.N2.A. Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the underlying declaration space of the compilation unit or namespace, but rather affects only the compilation unit or namespace body in which it appears.
The namespace-name referenced by a using-namespace-directive is resolved in the same way as the namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the same compilation unit or namespace body do not affect each other and can be written in any order.
Conclusion:C# programs are organized using namespaces. Using directives are provided to facilitate the use of namespaces. From this article we can understand the need and usage of Namespaces in classes.The Namespaces can hold other types also as follows:
Classes,Structures,Interfaces,Enumerations,Delegates.Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system-a way of presenting program elements that are exposed to other programs.
.Net Application Domain
An application domain is a virtual process and is used to isolate applications from one another. Each application domain has their own virtual address space which scopes the resources for the application domain using that address space.
Each application running within its main process boundaries and its application domain boundaries. All objects created within the same application scope are created within the same application domain. Multiple application domains can exist in a single operating system process. An application running inside one application domain cannot directly access the code running inside another application domain.
System.AppDomain is the main class you can use to deal with application domains.
.Net Code Access Security
The .NET Security Model provides code access permissions and code identity permissions. Code Access Security is the part of the .NET security model that determines whether or not the code is allowed to run, and what resources it can use when it is running. Code Access Security policy uses evidence to help grant the right permissions to the right assembly.
An administrator can configure Code Access Security policy to restrict the resource types that code can access and the other privileged operations it can perform. Code Access Security allows code to be trusted to varying degrees depending on where the code originates and on other aspects of the code's identity. Code Access Security can also help minimize the damage that can result from security vulnerabilities in your code.
A good way to organize your namespaces is via a hierarchical system. You put the more general names at the top of the hierarchy and get more specific as you go down. This hierarchical system can be represented by nested namespaces.By placing code in different sub-namespaces, you can keep your code organized.
The example:
namespace india.CSharp.Namespaces
{
public class Hello
{
public string GetMessage()
{
return "Hello, world";
}
}
}
Namespaces are hierarchical, and the name india.CSharp.Namespaces is actually shorthand for defining a namespace named arun that contains a namespace named CSharp that itself contains a namespace named Namespaces, as in:
namespace india
{
namespace CSharp
{
namespace Namespaces
{....}
}
}
The using keyword has two uses:
Create an alias for a namespace (a using alias).
Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive).
Implement the "using" Directive
Next, we'll write a console application that uses the Hello class. We could just use the fully qualified name for the class-india.CSharp.Namespaces.Hello-but this name is quite long and unwieldy. An easier way is to use a "using" directive, which makes it possible to use all of the types in a namespace without qualification. If you would like to call methods without typing their fully qualified name, you can implement the "using" directive.
using sysem;
using india.CSharp.Namespaces;
class Hello
{
static void Main()
{
Hello m = new Hello ();
System.Console.WriteLine(m.GetMessage());
}
}
"using System", is the same "using" directive you have seen in every program in this article. It allows you to type the method names of members of the "System" namespace without typing the word "System" every time. In class Hello(), "Console" is a class member of the "System" namespace with the method "WriteLine". It's fully qualified name is "System.Console.WriteLine(...)".
Note that the two occurrences of Hello are shorthand for india.CSharp.Namespaces.Hello. C# also enables the definition and use of aliases. Such aliases can be useful in situation in which name collisions occur between two libraries, or when a small number of types from a much larger namespace are being used.
Using alias directives
A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.
using identifier = namespace-or-type-name ;
Within member declarations in a compilation unit or namespace body that contains a using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given namespace or type.
For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using A = N1.N2.A;
class B: A {}
}
Here, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B
derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then
referencing R.A:
namespace N3
{
using R = N1.N2;
class B: R.A {}
}
The identifier of a using-alias-directive must be unique within the declaration space of the compilation unit or namespace that immediately contains the using-alias-directive.
Using namespace directives
A using-namespace-directive imports the types contained in a namespace into the immediately enclosing compilation unit or namespace body, enabling the identifier of each type to be used without qualification.
using-namespace-directive:using namespace-name ;
Within member declarations in compilation unit or namespace body that contains a using-namespace-directive, the types contained in the given namespace can be referenced directly.
For example:
namespace N1.N2
{
class A {}
}
namespace N3
{
using N1.N2;
class B: A {}
}
Here, within member declarations in the N3 namespace, the type members of N1.N2 are directly available, and thus class N3.B derives from class N1.N2.A. Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the underlying declaration space of the compilation unit or namespace, but rather affects only the compilation unit or namespace body in which it appears.
The namespace-name referenced by a using-namespace-directive is resolved in the same way as the namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the same compilation unit or namespace body do not affect each other and can be written in any order.
Conclusion:C# programs are organized using namespaces. Using directives are provided to facilitate the use of namespaces. From this article we can understand the need and usage of Namespaces in classes.The Namespaces can hold other types also as follows:
Classes,Structures,Interfaces,Enumerations,Delegates.Namespaces are used both as an "internal" organization system for a program, and as an "external" organization system-a way of presenting program elements that are exposed to other programs.
.Net Application Domain
An application domain is a virtual process and is used to isolate applications from one another. Each application domain has their own virtual address space which scopes the resources for the application domain using that address space.
Each application running within its main process boundaries and its application domain boundaries. All objects created within the same application scope are created within the same application domain. Multiple application domains can exist in a single operating system process. An application running inside one application domain cannot directly access the code running inside another application domain.
System.AppDomain is the main class you can use to deal with application domains.
.Net Code Access Security
The .NET Security Model provides code access permissions and code identity permissions. Code Access Security is the part of the .NET security model that determines whether or not the code is allowed to run, and what resources it can use when it is running. Code Access Security policy uses evidence to help grant the right permissions to the right assembly.
An administrator can configure Code Access Security policy to restrict the resource types that code can access and the other privileged operations it can perform. Code Access Security allows code to be trusted to varying degrees depending on where the code originates and on other aspects of the code's identity. Code Access Security can also help minimize the damage that can result from security vulnerabilities in your code.
No comments :
Post a Comment