Alias names

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
 
D

Dennis Myrén

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.
 
K

Kimmo Laine

Hi Dennis,

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

Kimmo
 
D

Dennis Myrén

Kimmo,

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

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi,

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

Marcin
 
D

Dennis Myrén

Although using statements certainly is more frequently used to import
namespaces,
they may also be used to create aliases for types.
 
J

Jon Skeet [C# MVP]

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");
}
}
 
G

Guest

Hi Jon and Dennis,

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

THANKS!
Marcin
 
G

Guest

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
 
G

Guest

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*
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top