Defining type aliases
C#TypesC# Problem Overview
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?
C# Solutions
Solution 1 - C#
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();
Solution 2 - C#
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();
}
}
}
Solution 3 - C#
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.
Solution 4 - C#
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.
Solution 5 - C#
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.
Solution 6 - C#
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
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.
Solution 7 - C#
Just a simple using would do:
using Foo = System.UInt16;
public class Test {
public Foo Bar { get;set; }
}