C #: declaring a constant variable for the data type to use

Is it possible to somehow define a constant that says which data type to use for certain variables, similar to generics? So in a particular class, I would have something like the following:

MYTYPE = System.String;

// some other code here

MYTYPE myVariable = "Hello";

From the principle, it should do the same as generics, but I do not want to write a data type every time the constructor for this class is called. It should simply ensure that the same data type is used for two (or more) variables.

+5
source share
4 answers

Well, you can use the directive using:

using MYTYPE = System.String;

However, this is not like typedef - in particular, this code is now completely correct.

MYTYPE x = "hello";
string y = "there";
x = y;

, .

, , :

, .

?

, , .

+7

using:

using MyType = System.String;

, . , ( "Owin" ..), .

+4

You can use an alias:

using System;
using MYTYPE = System.String;

class Program
{
    static void Main()
    {
        MYTYPE f = "Hello";
        Console.WriteLine(f);
    }
}
+2
source

Yes, using the alias directive .

edit: beat for a couple of seconds.

+1
source

All Articles