I'm trying to define a pair of type aliases at the top of my C# program. This is a short example of what I'm trying to do:
using System;
using System.Collections.Generic;
namespace Foo {
using TsvEntry = Dictionary<string, string>;
using Tsv = List<TsvEntry>;
}
When I try to compile this using mcs 3.2.8.0, I get the following error message:
foo.cs(6,19): error CS0246: The type or namespace name `TsvEntry' could not be found. Are you missing an assembly reference?
Is it possible to use using
aliases within other aliases in C#, or am I missing something about the way using
statements work?
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
After language 'B', Dennis Ritchie came up with another language which was based upon 'B'. As in alphabets B is followed by C and hence he called this language as 'C'.
Check documentation for this question:
https://msdn.microsoft.com/en-us/library/aa664765(v=vs.71).aspx
It says:
The order in which using-alias-directives are written has no significance, and resolution of the namespace-or-type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other using-directives in the immediately containing compilation unit or namespace body. In other words, the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing compilation unit or namespace body had no using-directives. In the example
namespace N1.N2 {} namespace N3 { using R1 = N1; // OK using R2 = N1.N2; // OK using R3 = R1.N2; // Error, R1 unknown }
the last using-alias-directive results in a compile-time error because it is not affected by the first using-alias-directive.
Technically, you cannot do it same namespace, but if you do alias in namespace 1, and do alias for this alias in a nested namespace, it will work:
namespace N1
{
namespace N12 { }
}
namespace N2
{
using R1 = N1;
namespace N2
{
using R2 = R1.N12;
}
}
I am not really sure it's worth using aliases in your specific example, consider using them as rare as you can, mostly for resolving namespace conflicts.
Just as an addition to the other answers.
If you really want to continue your way, nesting the namespaces would be the way to go. Since for the second namespace, the first using would then be defined.
The following would work since you are in a deeper namespace:
namespace N1
{
using TsvEntry = Dictionary<string, string>;
namespace N1.N2
{
using Tsv = List<TsvEntry>;
}
}
Please note that although it is working, i wouldn't recommend using this kind of code structure since you can end up with a deeply nested code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With