Option Type in C #

VBA (and I assume that VB) has a Variant type, which, it seems to me, takes up more memory but spans various data types.

Is there an equivalent in C #?

In the form of a window they say that I had the following, how would I change the type z so that it works fine

private void uxConvertButton_Click(object sender, EventArgs e) { int x = 10; byte j = (byte)x; upDateRTB(j); long q = (long)x; upDateRTB(q); string m = x.ToString(); upDateRTB(m); } void upDateRTB(long z) { MessageBox.Show(this,"amount; "+z); } 
+7
source share
5 answers
 void upDateRTB(object z) { MessageBox.Show(this, "amount; " + Convert.ToString(z)); } 
+12
source

If you're talking about a โ€œvariantโ€ type in C #, take a look at dynamic type .net 4.0

But to solve your problem, it would be enough to use z.ToString() in your MessageBox.Show

+7
source

The object parameter will accept everything, but if you want the variables to be strictly typed (and avoided boxing in the process), you could use generics:

 void upDateRTB<T>(T z) { MessageBox.Show(this,"amount; "+ Convert.ToString(z)); } 

The method call may remain the same, since the compiler can resolve the generic type based on this parameter.

+7
source

A dynamic keyword or object type may give you the behavior you want, but:

In this case, I would change the function to:

 void upDateRTB(string z) { MessageBox.Show(this,"amount; " + z); } 

Because you need the whole method.

+3
source

"amount; "+z implicitly calls the ToString method on z . Therefore, you can use the type object :

 void upDateRTB(object z) { MessageBox.Show(this,"amount; "+z); } 

You can also use dynamic, but I don't see the point:

 void upDateRTB(dynamic z) { MessageBox.Show(this,"amount; "+z); } 
+3
source

Source: https://habr.com/ru/post/918156/


All Articles