Alias names

  • Thread starter Thread starter Kimmo Laine
  • Start date Start date
K

Kimmo Laine

Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine
 
The alias will, unfortunately, be effective only in the physichal file where
it was defined.
Since we have nothing like
#include "a.cs"
there is no way around this.
You will have to add the using statement to any file where you want to use
that alias.
 
Hi Dennis,

how does the System.Int32 alias "int" work around this limitation?

Kimmo
 
Kimmo,

The primitive types defined in C# are known by the compiler
and is therefore treated in a special way.
 
Hi,

AFAIK "using alias" works with "namespace" only.
So you should not use it for "class".

Marcin
 
Although using statements certainly is more frequently used to import
namespaces,
they may also be used to create aliases for types.
 
Marcin Grzebski said:
AFAIK "using alias" works with "namespace" only.
So you should not use it for "class".

Nope, that's not true. For instance:

using FooBar = System.Console;

public class Test
{
static void Main()
{
FooBar.WriteLine ("Hello");
}
}
 
Hi Jon and Dennis,

Upsss....
My ignorance was a step before .NET docs :-(

THANKS!
Marcin
 
Hi,

Some time ago i tried something like this:

using System.Console;

public class Test
{
static void Main()
{
Console.WriteLine ("Hello");
}
}

....but it does not work, without alias (now i can see):

using Console=System.Console;

JAVA have support for class (and packages) "import", and i miss
this feature in C#.

Marcin
 
VB.NET has this "feature" as well.

I wouldn't implement such a thing in my code. For instance:

using System.Console;
..
..
..
WriteLine("Hello green world!");
..
..



Now how would instantly know, when reviewing the code, that i mean
Console.WriteLine and not Debug.WriteLine??

Maybe for console and debug this is fine, but for our domain objects?
*shrug*
 
Hi Patrik,
I wouldn't implement such a thing in my code. For instance:

using System.Console;
.
.
.
WriteLine("Hello green world!");

Now how would instantly know, when reviewing the code, that i mean
Console.WriteLine and not Debug.WriteLine??

Maybe for console and debug this is fine, but for our domain objects?
*shrug*

In JAVA class "import" (C# using) means that this class is directly
available, like in the code below:

So, you cannot access directly to its "static" methods (fields,...)
WriteLine("Hello green world!");

but you can use it name without its namespaces (packages).

Only difference in JAVA's "import" is that all classes from within
package are declared by "*" e.g.:

import java.util.*;

means that you can access all classes from package "java.util"
but:

import java.util.ArrayList;

means that only class "ArrayList" is available to use in your code.

So, its a most common way (in Java coding) to imports all needed
classes, besides imports its packages.

As i wrote before i can use alias same with class name in C#,
but it is the option only, e.g.:

using ArrayList=System.Collection.ArrayList;

Regards
Marcin
 
Back
Top