Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism on type of Variables in C#

Tags:

c#

I have a variable in a Class with the name of Id:

public class myClass
{
    GUID Id;
}

How can I have polymorphism on the Id variable to Get either an int type or a GUID type?

I want something like this, but it only gets GUID or int:

EDIT:

public class myClass
{
    Guid || int Id;
}

I want it so that the int or Guid is defined on initialization of the class.

like image 285
Mohammadreza Avatar asked Nov 22 '25 15:11

Mohammadreza


1 Answers

You could make the class generic:

public class MyThing<T>
  where T : struct
{
  public T MyProperty { get; set; }
}

Which then can be used the following way:

MyThing<GUID>
MyThing<int>

But to give you a valid answer, it would certainly help to know what you are trying to achieve.

like image 68
vc 74 Avatar answered Nov 24 '25 06:11

vc 74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!