Type alias

M

Maxim

Hello, everybody!

I'd like to make global type alias.

If I used c++ I would write the following code to create a global type
alias:
typedef SomeType MyAlias;

I've found that using keyword could be used to make type/namespace aliases.

using MyAlias = SomeTypeOrNamespace;

But such an alias applies only to current module and should be at the top of
it (not allowed inside class definition).

So is there any possibility to make a global type alias in C#?

Thanks in advance.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

No other than that, why you use it s much? Personally I have never used it
 
M

Maxim

Thanks for answering, Ignacio
No other than that, why you use it s much?

I don't. I'm new to C# and .NET and trying to figure out how it works.
I'm implementing WebService client now and proxy classes have very lengthy
names. And code looks confusing. So I decided to use some type aliases to
make it more readable.
 
M

Mike Schilling

Maxim said:
Hello, everybody!

I'd like to make global type alias.

If I used c++ I would write the following code to create a global type
alias:
typedef SomeType MyAlias;

I've found that using keyword could be used to make type/namespace
aliases.

using MyAlias = SomeTypeOrNamespace;

But such an alias applies only to current module and should be at the top
of
it (not allowed inside class definition).

So is there any possibility to make a global type alias in C#?

No.


I like the above using syntax, though I usually won't do any renaming:

using ClassName = Namespace1.Namespace2.ClassName;

This documents exactly what classes I'm using, and helps anyone who's
reading the code to find ClassName.

Oddly, the equivalent Java syntax

import p1.p2.ClassName;

is standard, and

import p1.p2.*;

is considered lazy, where in C#, class-level using statements are almost
unheard of.
 
J

Jon Skeet [C# MVP]

I don't. I'm new to C# and .NET and trying to figure out how it works.
I'm implementing WebService client now and proxy classes have very lengthy
names. And code looks confusing. So I decided to use some type aliases to
make it more readable.

The thing is, it makes it *less* readable for other people - and to you
in a few months' time. Performing a mental translation from one type
name to another is harder work than just reading the longer name in the
first place, IMO.
 
L

Lucian Wischik

Jon Skeet said:
The thing is, it makes it *less* readable for other people - and to you
in a few months' time. Performing a mental translation from one type
name to another is harder work than just reading the longer name in the
first place, IMO.

I disagree. When I write

Dictionary<MainQueueIdentifier, StackPointer> queueReference = new
Dictionary<MainQueueIdentifier, StackPointer>();

the verbosity is so much that it gets in the way. The only way I have
to make it shorter is to use aliases (yuck, don't work outside the
current file, aren't used by intellisense) or to define my own class:

class QueueReference : Dictionary<MainQueueIdentifier, StackPointer>
{
... copy all of the constructors
}

This class hasn't helped at all. It's done exactly the same job as a
type-alias-directive would do, but at the cost of twenty extra lines
of code.
 
J

Jon Skeet [C# MVP]

Lucian said:
I disagree. When I write

Dictionary<MainQueueIdentifier, StackPointer> queueReference = new
Dictionary<MainQueueIdentifier, StackPointer>();

the verbosity is so much that it gets in the way.

I agree that it's somewhat ugly - although as I prefer to use
interfaces where possible, I'd have different types on the left hand
side and the right hand side anyway - but I personally prefer that to
having to look up the meaning of something elsewhere.

I guess it's a personal preference.

Jon
 

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