Defining type aliases

One feature of Pascal I found very useful was the ability to name a data type, eg

type
 person: record
             name: string;
             age: int;
         end;

var
 me: person;
 you: person;

etc

Can you do something similar in C#? I want to be able to do something like

using complexList = List<Tuple<int,string,int>>;

complexList peopleList;
anotherList otherList;

So that if I have to change the definition of the datatype, I can do it in one place.

Does C# support a way to achieve this?

Answers


It's not excatly what you do in Pascal, but you can use the using-directive. Have a look here on how to use it

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyList = Dummy2.CompleXList;

namespace Dummy2
{
    public class Person 
    {
    }

    public class CompleXList : List<Person> 
    { 
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyList l1 = new MyList();
        }
    }
}

Yes, it's possible. You can write:

using System;
using System.Collections.Generic;

namespace ConsoleApplication12
{
    using MyAlias = List<Tuple<int, string, int>>;
}

or, if declared outside the namespace:

using System;
using System.Collections.Generic;

using MyAlias = System.Collections.Generic.List<System.Tuple<int, string, int>>;

namespace ConsoleApplication12
{
}

then use it as a type:

MyAlias test = new MyAlias();

You can create a type:

class ComplexList : List<Tuple<int,string,int>> { }

This is not strictly the same as an alias but in most cases, you shouldn't see any differences.


What about inheritance?

class ComplexList : List<Tuple<int, string, int>> {}

var complexList = new ComplexList();

This seems like a similar concept but there is no direct equivalent of a type based alias in C#, only namespace aliases.

If you want to avoid type name clashing in your code, namespace aliasing is the way to go.

If you want to make a new type which "is a" instance of another type, inheritance is one option.


From the syntax shown in the initial question, it looks like you're really just asking how to make a class in C#, and not how to alias a type.

If you want a simpler name to type than List<Tuple<int,string,int>>, and you want it to be "global" (i.e. not per-file), I would create a new class that inherited said class and declared no additional members. Like this:

public class MyTrinaryTupleList: List<Tuple<int,string,int>> { }

That gives one single location of management, and no need for additional using statements.

However, I would also take it a step further, and venture that you probably don't want a Tuple, but rather another class, such as:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int FavoriteNumber { get; set; }

    public Person() { }

    public Person(string name, int age, int favoriteNumber) 
    { 
        this.Name = name; 
        this.Age = age; 
        this.FavoriteNumber = favoriteNumber; 
    }
}

And then for your list type you can do the following:

public class PersonList: List<Person> { }

In addition, if you need other list specific helper properties or methods you can add them to the PersonList class as well.


Yes you can do that, however you need to specify the full types, i.e. the definition becomes:

using ComplexList = System.Collections.Generic.List<System.Tuple<int,string,int>>;

This is specified per file, much like the using directives for namespaces.

nitpick: Conventionally, a type in .NET is PascalCased.


Just a simple using would do:

using Foo = System.UInt16;

public class Test {
  public Foo Bar { get;set; }
}

Need Your Help

Global Windows Key Press

c# .net wpf keyboard-shortcuts

I have a simple WPF application and I need to capture F1 key pressed in Windows (Operation System), even if my WPF window is minimized, or it isn't activated.