- In particular when using Func or similar types it can be hard to communicate the meaning. Creating an aliased type like this can provide a much stronger semantic.
- Similar to 1, in methods that return simple types, such as strings, it provides a stronger semantic to the signature if the type returned informs about the 'type' of string, rather than the simple fact of it being a string
- Similar, but when creating dictionary types, if the key is a string, what is special about it?
- Because I miss this capability in languages such as F# and Haskell (not that this is as good)
Last active
August 29, 2015 14:02
-
-
Save NeilRobbins/f0234a2fda079b50c9fe to your computer and use it in GitHub Desktop.
Aliasing in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Alias | |
{ | |
protected readonly string Value; | |
protected Alias(string value) | |
{ | |
if (value == null) throw new ArgumentNullException("value"); | |
if (string.Empty.Equals(value, StringComparison.InvariantCultureIgnoreCase)) | |
throw new ArgumentException("Value must not be empty.", "value"); | |
Value = value; | |
} | |
public static implicit operator string(Alias alias) | |
{ | |
return alias.Value; | |
} | |
public override string ToString() | |
{ | |
return Value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Sql : Alias | |
{ | |
public Sql(string sql) : base(sql) | |
{} | |
public static implicit operator Sql(string sql) | |
{ | |
return new Sql(sql); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment