I have a field variable named uid in a class. The type is int, but I want to open the possibility that I change it long or short.
With C++, typedef is the one for this purpose, but I see C# doesn't have typedef.
Is there a way to mimic typedef in C#?
I think you're trying to alias the type. If so, you can alias it via the using
alias directive. You must specify the fully qualified type (the entire namespace) when assigning an alias.
For example:
using System;
using MyAlias = System.Int32;
namespace UsingAlias
{
class Program
{
static void Main(string[] args)
{
MyAlias temp = Math.Min(10, 5);
Console.WriteLine(temp);
MyAlias result = MyAlias.Parse("42");
Console.WriteLine(result);
}
}
}
Try making a class
. It's like a typedef on steroids, and you can change the internal representation as much as you want without affecting the interface!
You could even make it a guid!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With