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

前端 未结 7 1208
忘掉有多难
忘掉有多难 2021-02-18 18:52

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 alia

7条回答
  •  梦毁少年i
    2021-02-18 19:17

    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.

提交回复
热议问题