There is a feature in .net that is not commonly remembered and it is Data Type aliasing.
What it means is that u give a data type a different name.
I can see two uses for it:
1) Enhance code readability:
Instead of having very long type declarations such as when using generic types inside generic types, u can assign an alias to that declaration. Here is an example:
// Aliasing
using HashOfHashes =
Dictionary<string, Dictionary<string, List<string>>>;
public class AliasExample
{
HashOfHashes m_hash;
public AliasExample()
{
m_hash = new HashOfHashes();
}
public HashOfHashes MyHash
{
get
{
return m_hash;
}
}
}
2) Alias data types that are prone to change:
An example is an identifier property of class. It can save a lot find&replace when changing code like changing the identifier of a member from int to Guid and it better shows intent. For example:
// Aliasing
using MemberIdentidfier = Guid;
public class MemberWithAlias
{
private MemberIdentidfier m_id;
private string m_name;
public MemberWithAlias
(MemberIdentidfier id)
{
m_id = id;
}
public MemberIdentidfier Id
{
get { return m_id; }
}
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
No comments:
Post a Comment