1

Suppose I have some relatively complex generic object that could be simplified as

class A<T>
{
   static void foo(T x) { }
}

To use foo, one must repeatedly use the class name with the generic.

A<MyLongTypeNameIsNotFunToType>.foo(x);

It would be very nice if it was possible to simplify this:

alias AA = A<MyLongTypeNameIsNotFunToType>;
AA.foo(x);

Only thing I can think of is to create a wrapper class

class B : A<MyLongTypeNameIsNotFunToType> { }

which allows me to access A's static members through B. One problem is that this requires a public constructor, which, in my case, is not possible. Obviously I could wrap A's static members in B but that seems highly redundant.

[I tried using, but note that I need to access the static members so I need the static type. Using doesn't seem to work with static types. error: ___ is a type which isn't valid in the given context]

Any elegant solution?

AbstractDissonance
  • 1
  • 2
  • 16
  • 31
  • 1
    Possible duplicate of [alias keyword (like typedef) in C#?](http://stackoverflow.com/questions/844651/alias-keyword-like-typedef-in-c) – Jannik Feb 15 '16 at 05:55
  • @Jannik No. Seems people are not paying attention to the *static* part of the question. – AbstractDissonance Feb 15 '16 at 05:57
  • "but note that I need to access the static members so I need the static type" -> you can still use "using". I wonder where this doesn't work? This does seem to be a dupe so I'm deleting my answer soon. – Tyress Feb 15 '16 at 06:14

3 Answers3

0

To make an alias of a Type you do the following where your namespaces are specified

using S = MyLongTypeNameIsNotFunToType;

Then to use it you can do the following

//A<S>.foo(new S());
A<S>.foo(x);

You can also continue to change A to the following

using S = MyLongTypeNameIsNotFunToType;
using AA = A<MyLongTypeNameIsNotFunToType>;

And then in your code you can go and do

AA.foo(x);

Or

A<S>.foo(x);

Here is a copy and paste into a console application sample

namespace ConsoleApplication3
{
    using B = MyLongTypeNotFunToType;
    using C = A<MyLongTypeNotFunToType>;
    public class A<T>
    {
        public static void foo(T foo)
        {

        }
    }

    public class MyLongTypeNotFunToType
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
          var x = new B();
          A<B>.foo(x);
          C.foo(x);
        }
    }
}
Donald Jansen
  • 1,937
  • 4
  • 22
  • 41
0

I have two solutions:

  1. use using in the class file as all others have mentioned..

Class definition:

class A<T>
{
    public static void foo(T x) { }
}

Usage:

using Alias = A<MyLongTypeNameIsNotFunToType>;

public class Dummy
{
    public Dummy(MyLongTypeNameIsNotFunToType x)
    {
        Alias.foo(x);
    }
}
  1. Define the class A in different way such that type T is resolved implicitly meaning type of parameter x will resolve type T automatically (at compile-time).

My reason for going with this option will be that you have static method otherwise you can implement class A<T> same way as List<T> and use the solution 1 given above.

Class definition:

class A // <-- removed the <T> from class and added in the method declaration
{
    public static void foo<T>(T x) { }
}

Usage:

public class Dummy
{
    public Dummy(MyLongTypeNameIsNotFunToType x)
    {
        A.foo(x);
    }
}

I hope you see the simplicity in the solution 2.

Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
-1

Use using to alias your type or namespace.

using   identifier = namespace-or-type-name;

So you can do this.

using LongType = A<MyLongTypeNameIsNotFunToType>;

You can read more about usage and details here

Update :

It would be very nice if it was possible to simplify this:

alias AA = A<MyLongTypeNameIsNotFunToType>;
AA.foo(x);

Yes, you can achieve this with below code.

using newname = A<int> ;
newname.foo(10);

Check the Demo

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35