Is there a way to define C# strongly-typed aliases of existing primitive types like `string` or `int`?
Asked Answered
R

7

22

Perhaps I am demonstrating my ignorance of some oft-used feautre of C# or the .NET framework, but I would like to know if there is a natively-supported way to create a type alias like EmailAddress which aliases string but such that I can extend it with my own methods like bool Validate()?

I know of the using x = Some.Type; aliases but these are not global nor do they provide type safety, i.e. one could swap out an ordinary string for the using alias in the current file. I would like my EmailAddress to be its own type, independent and not interchangeable with the string type that it shadows.

My current solution is to generate public sealed partial EmailAddress : IEquatable<EmailAddress>, IXmlSerializable classes with a T4 template generating the boilerplate implicit string conversion operators and other such things. This is fine with me for now and gives me a lot of flexibility but at the back of my mind it seems silly that I have to generate such copious amounts of boilerplate code to do something as simple as creating a strong type alias.

Maybe this is not possible other than with code generation, but I am curious if others have attempted something similar with their designs and what your experiences have been. If nothing else, perhaps this could serve as a good use-case for such an alias feature in a hypothetical future version of C#. Thanks!

EDIT: The real value that I want out of this is to be able to get type safety with primitive types that represent different types/formats for data. For instance, an EmailAddress and a SocialSecurityNumber and a PhoneNumber, all of which use string as their underlying type but which are not interchangeable types in and of themselves. I think this gets you much more readable and self-documenting code, not to mention added benefits of more method overload possibilities that are less ambiguous.

Railroad answered 17/12, 2009 at 23:39 Comment(1)
It seems you have at least a reasonable C# knoledgde so my comment may seem stupid, but what you want is called "type hierarchy" and the guys who coded the String class wanted to prevent you from using this "OO feature" so they made String class sealed, that's why you won't be able to do what you want. The best approach is this you are on now: Make your own type and an implicit convertion to String.Retroflex
C
5

If you look at the .NET Framework System.Uri is the closest example that is similar to an email address. In .NET the pattern is to wrap something in a class and add constraints that way.

Adding strong typing that adds additional constraints to simple types is an interesting language feature that I believe some functional language has. I can't recall the name of the language which would let you add dimensional units such as feet to your values and do a dimensional analysis on your equations to ensure that the units matched.

Chicle answered 18/12, 2009 at 0:28 Comment(3)
This seems like the best way to go. I'm not well versed in the functional languages but I do know they have some nifty type systems that let you compose much nicer than an object-oriented one does.Railroad
I believe the Ada language has the feature you describe, or a similar one.Patriarchy
Go and Haskell have such constraints as well (I'm sure about Go, Haskell I don't know well).Breannabreanne
N
5

Some background on why string is sealed:

From http://www.code-magazine.com/Article.aspx?quickid=0501091 :

Rory: Hey Jay, you mind if I ask you a couple questions? I'm already curious about some things. First of all, and this was brought up at one of the MSDN events I did this week, why is String sealed? Note: for VB.NET programmers, Sealed = NotInheritable.

Jay: Because we do a lot of magic tricks in String to try and make sure we can optimize for things like comparisons, to make them as fast as we possibly can. So, we're stealing bits off of pointers and other things in there to mark things up. Just to give you an example, and I didn't know this when I started, but if a String has a hyphen or an apostrophe in it [then] it sorts differently than if it just has text in it, and the algorithm for sorting it if you have a hyphen or an apostrophe if you're doing globally-aware sorting is pretty complicated, so we actually mark whether or not the string has that type of behavior in it.

Rory: So, what you're saying is that in the string world, if you didn't seal String there would be a whole lot of room for wreaking a lot of havoc if people were trying to subclass it.

Jay: Exactly. It would change the entire layout of the object, so we wouldn't be able to play the tricks that we play that pick up speed.

Here is the CodeProject article that you probably have seen before:

http://www.codeproject.com/KB/cs/expandSealed.aspx

So yeah, implicit operator is your only solution.

Nisus answered 18/12, 2009 at 0:5 Comment(4)
Thanks for the information about string being sealed but that's not directly relevant.Railroad
I know :) It's just I personally try to find out why the things that shit me are the way they are.Nisus
There are times it would be helpful to be able to quasi-inherit a type from something like string even if the derived type had no access to any non-public members of the underlying class, nor override any of its members, nor add any fields. If such types could add new interfaces--even if they couldn't have any members, that could be helpful for defining things like deeply-immutable interfaces (e.g. if one could declare a type DeeplyImmutableInt that behaved just like an int but inherited IAmDeeplyImmutable, and do likewise for other built-in types, one could then...Nobukonoby
...use generic constraints to enforce deep immutability). Even if one couldn't add even empty interfaces, being able to define types X:Z and Y:Z such that widening identity conversions would exist from X to Z, and narrowing identity conversions from Z to X or Z to Y, could be helpful. Also, being able to define a types which should support bidirectional identity conversion to/from a certain base type (e.g. P::Q and R::S) could be helpful, especially if a compiler would permit conversion from P to R under the same circumstances as conversion from Q to S.Nobukonoby
C
5

If you look at the .NET Framework System.Uri is the closest example that is similar to an email address. In .NET the pattern is to wrap something in a class and add constraints that way.

Adding strong typing that adds additional constraints to simple types is an interesting language feature that I believe some functional language has. I can't recall the name of the language which would let you add dimensional units such as feet to your values and do a dimensional analysis on your equations to ensure that the units matched.

Chicle answered 18/12, 2009 at 0:28 Comment(3)
This seems like the best way to go. I'm not well versed in the functional languages but I do know they have some nifty type systems that let you compose much nicer than an object-oriented one does.Railroad
I believe the Ada language has the feature you describe, or a similar one.Patriarchy
Go and Haskell have such constraints as well (I'm sure about Go, Haskell I don't know well).Breannabreanne
R
2

Does the System.Net.Mail.MailAddress class fit your needs, or at least "help"?

EDIT: It's not explicitly IEquatable or ISerializable, but you could easily enough add those in your own wrapper.

Resurrectionist answered 17/12, 2009 at 23:51 Comment(2)
No, I intended to ask for general solution, not the specific case of an email address. I am also doing things like generating int wrappers to create strongly-typed identifiers for self-documenting purposes and flexibilty in method overload definitions. For example I have Staff GetStaff(StaffID id) and Staff GetStaff(TeacherID id) both defined on the same interface. Both methods do different queries against the underlying data source depending on which identifier type is provided. Teacher is-a Staff and a TeacherID of value 1 does not represent the same entity as StaffID with value 1.Railroad
Understood, I misinterpreted your question as asking for this specific case.Resurrectionist
R
2

It seems you have at least a reasonable C# knoledgde so my answer may seem stupid, but what you want is called "type hierarchy" and the guys who coded the String class wanted to prevent you from using this "OO feature" so they made String class sealed, that's why you won't be able to do what you want. The best approach is this you are on now: Make your own type and an implicit convertion to String.

Retroflex answered 18/12, 2009 at 0:29 Comment(0)
W
1

I guess I do not get why you want to have both strong types AND implicit string conversion at the same time. For me, one rules out the other.

I tried to solve the same problem for ints (you mention int in the title, but not in the question). I found that declaring an enum gives you a type-safe integer which needs to be explicitly cast from/to int.

Update

Enums may not be intended for open sets, but can still be used in such a way. This sample is from a compilation experiment to distinguish between the ID columns of several tables in a database:

    enum ProcID { Unassigned = 0 }
    enum TenderID { Unassigned = 0 }

    void Test()
    {
        ProcID p = 0;
        TenderID t = 0; <-- 0 is assignable to every enum
        p = (ProcID)3;  <-- need to explicitly convert

        if (p == t)  <-- operator == cannot be applied
            t = -1;  <-- cannot implicitly convert

        DoProc(p);
        DoProc(t);   <-- no overloaded method found
        DoTender(t);
    }

    void DoProc(ProcID p)
    {
    }

    void DoTender(TenderID t)
    {
    }
Winthorpe answered 18/12, 2009 at 0:15 Comment(1)
In this case I meant 'strongly typed' as in having its own distinct type apart from string and not directly interchangeable. Apologies for the term overload :). Enums don't help much for the int case since they define their own scope and aren't inteded to be used over an open set of cases (vs. a closed set of enumeration values).Railroad
S
1

I think you want to use extension methods. They allow you to extend a classes functionality without creating a new derived type.

Slave answered 18/12, 2009 at 0:19 Comment(1)
Extension methods would be attractive if I could create the distinct type alias and define the extension method on the alias instead of the base string type. I want the compiler to do type safety checking to make sure someone doesn't accidentally try to copy an EmailAddress into a PhoneNumber or something crazy/accidental like that.Railroad
R
1

I made this class to cover identical needs. This one is for the type "int" (I also have one for "string"):

public class NamedInt : IComparable<int>, IEquatable<int>
{
    internal int Value { get; }

    protected NamedInt() { }
    protected NamedInt(int val) { Value = val; }
    protected NamedInt(string val) { Value = Convert.ToInt32(val); }

    public static implicit operator int (NamedInt val) { return val.Value; }

    public static bool operator ==(NamedInt a, int b) { return a?.Value == b; }
    public static bool operator ==(NamedInt a, NamedInt b) { return a?.Value == b?.Value; }
    public static bool operator !=(NamedInt a, int b) { return !(a==b); }
    public static bool operator !=(NamedInt a, NamedInt b) { return !(a==b); }

    public bool Equals(int other) { return Equals(new NamedInt(other)); }
    public override bool Equals(object other) {
        if ((other.GetType() != GetType() && other.GetType() != typeof(string))) return false;
        return Equals(new NamedInt(other.ToString()));
    }
    private bool Equals(NamedInt other) {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(Value, other.Value);
    }

    public int CompareTo(int other) { return Value - other; }
    public int CompareTo(NamedInt other) { return Value - other.Value; }

    public override int GetHashCode() { return Value.GetHashCode(); }

    public override string ToString() { return Value.ToString(); }
}

And to consume it in your case:

public class MyStronglyTypedInt: NamedInt {
    public MyStronglyTypedInt(int value) : base(value) {
        // Your validation can go here
    }
    public static implicit operator MyStronglyTypedInt(int value) { 
        return new MyStronglyTypedInt(value);
    }

    public bool Validate() {
        // Your validation can go here
    }
}

If you need to be able to serialize it (Newtonsoft.Json), let me know and I'll add the code.

Rhizomorphous answered 8/12, 2015 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.