Skip to content

Instantly share code, notes, and snippets.

@NeilRobbins
Created October 12, 2012 17:06
Show Gist options
  • Save NeilRobbins/3880277 to your computer and use it in GitHub Desktop.
Save NeilRobbins/3880277 to your computer and use it in GitHub Desktop.
Type aliasing in C#
/// <summary>
/// Effectively an alias of string, but with a stronger semantic
/// </summary>
/// <remarks>
/// Can still pass it to something (eg a screen element, or a db, that expects a string), but get to call it
/// by a semantically richer type name everywhere, especially where it may be returned (returned things not
/// being named, except indirectly by the method name)
/// </remarks>
public class FooId
{
private readonly string fooId;
public FooId(string fooId)
{
this.fooId= fooId;
}
private string Id
{
get { return fooId; }
}
public static implicit operator string(FooId fooId)
{
return fooId.Id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment