Last post May 03, 2013 03:56 AM by Khuda Bux Mari Balcoh
Feb 14, 2012 08:17 AM|Khuda Bux Mari Balcoh|LINK
Can any one tell me what is difference b/w
Feb 14, 2012 10:05 AM|gerrylowry|LINK
Khuda Bux Mari Balcoh
a useful resource for questions like this and c# grammar in general is available online from Lucien Wischik:
his c. 4.5 minute training video, worth watching imho, is here:
http://blogs.msdn.com/b/lucian/archive/2010/04/19/grammar.aspx actually takes you to two grammars, one for c#, the other for vb.
View (open) his link
http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/1817.csharp4.html online ... you can download (save) it to your hard disk but the downloaded version, at least for me, does not preserve
the very useful pop-up windows.
see http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx "Using Namespaces (C# Programming Guide)"
using Co = Company.Proj.Nested; // define an alias to represent a namespace
above, Co is a namespace alias.
the :: operator is the namespace alias qualifier because it
qualifies the namespace.
study this http://msdn.microsoft.com/en-us/library/c3ay4x3d.aspx "How to: Use the Global Namespace Alias (C# Programming Guide)"
and http://msdn.microsoft.com/en-us/library/htccxtad.aspx ":: Operator (C# Reference"
May 03, 2013 03:56 AM|Khuda Bux Mari Balcoh|LINK
In the article, ".NET Namespaces", I described how namespaces can be used to help organise your code. The article also examines the use of
namespace aliases, which allow replacement of namespace names with aliases to prevent ambiguous definitions. In larger projects, particularly those that employ many third-party libraries, these name collisions can pose a problem.
The following code shows an example of an ambiguous declaration. In this sample, two namespaces exist, each containing a
class named "Test". As both namespaces are referenced in a basic
using statement, the attempt to create an instance of Test from within the Main
method is ambiguous and the program will not compile.
<div class="code"> <div> <div id="highlighter_621435" class="syntaxhighlighter nogutter csharp">
Test test =
is the first namespace!"
is the second namespace!"
</div> </div> </div>
The simple way to solve this problem is to use the fully qualified name of the Test class to be instantiated. However, if the namespace name is very long, this can reduce the readability of your code. Instead, you may decide to use an alias for each of the
two additional namespaces. To do so, you could change the two using directives as follows:
<div class="code"> <div> <div id="highlighter_402135" class="syntaxhighlighter nogutter csharp">
first = FirstNamespace;
second = SecondNamespace;
Now the creation of the Test object can be changed to use the alias, causing the code to compile correctly.
<div class="code"> <div> <div id="highlighter_705311" class="syntaxhighlighter nogutter csharp">
first.Test test =
The problem described above can become more complicated. If a third namespace were added and this had a name matching a current namespace alias, a new opportunity for ambiguity arises. For example, try adding this third namespace at the foot of the above
<div class="code"> <div> <div id="highlighter_743568" class="syntaxhighlighter nogutter csharp">
is yet another namespace!"
With this namespace in place, the code again fails to compile, even though the Test object's declaration is now fully qualified. This is due to the new collision between the "first" alias and the "first" namespace. You could resolve this by changing the
namespace alias name. Alternatively, if you are developing using the .NET framework version 2.0 or later, you can use the
namespace alias qualifier. The qualifier appears as two colons (::) with the namespace or alias name to the left and the class to be used to the right.
If we use this syntax when declaring the Test object, we can specify that the "first" alias is to be used, creating an object from FirstNamespace. Change the declaration as follows and execute the code to see that this indeed happens:
<div class="code"> <div> <div id="highlighter_998500" class="syntaxhighlighter nogutter csharp">
first::Test test =
NB: The above sample is for example purposes only. In this situation you would be advised to change the alias name.
If a namespace has no alias, the namespace alias qualifier may still be used to identify it. In this case, the alias to the left of the qualifier symbol should be specified as "global", referencing the global or default namespace. The class name to the right
of the qualifier can then be fully qualified. In the above example, to reference the "first" namespace, change the Test object declaration as follows:
<div class="container"> <div class="line number1 index0 alt2">global::first.Test test =
new global::first.Test();</div> </div>
global::first.Test test =