Search code examples
c#typedefusing

Using alias inside a class instead of declaring a new class


I have a complex event args class in a webservice

public partial class PROJECT.WCF.MyActionCompletedEventArgs 
{
...
};

Now I have a class that wraps this web service in some way

public class WebserviceCtrl
{
...

Now I want to add my own handler with the same event args already defined. So I want a using alias (like a typedef in C++) inside the class like this:

public class WebserviceCtrl
{
  using ActionCompletedEventArgs = PROJECT.WCF.MyActionCompletedEventArgs;
  ...

But I get a syntax error directly in front of the using statement. I can use this using statement directly in front of the class declaration (inside a namespace). It doesn't matter where I place this statement inside the class definition.

I know about the possibilities of declaring my own ActionCompletedEventArgs class, but in this case I have to provide specific constructors and forces me to write specific constructors that aren't "needed", if I just use an alias...
So I don't want to declare my own class!

I already studied the C# using documentation, but I could find any information about this.

My question is, why can't I use using classx = classy; inside my class?


Solution

  • Aliases only apply to the current file (or the current namespace scope within the current file), and they do not exist in the compiled output. Just like the other using statements that "include" a namespace.

    There's no reason to place them elsewhere at all, it would just make code harder to read.