简体   繁体   中英

Polymorphism on type of Variables in 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.

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.

You can use a generic class.

public class MyClassName<TId>
{
  public TId Id { get; set; }
}

And use it like this:

For int:

var myObject = new MyClassName<int>();

For Guid:

var myObject = new MyClassName<Guid>();

I am not sure why you would want to do that, but if you must, how about:

public class
{
    object Id;
}

or

public class
{
   private int _intId;
   private GUID _guidId;

   public int GetIntegerGuid()
   { 
       return _intId;
   }

   public int GetGuid()
   { 
       return _guidId;
   }
}

You cannot do that in this case as polymorphism is something related to the object inheritance

Polymorphism is the ability for an object to change its public interface according to how it is being used. When using inheritance, polymorphism is achieved when an object of a derived class is substituted where an instance of its parent class is expected. This uses a process known as upcasting. With upcasting, the object of the more specialised class is implicitly cast to the base class type required.

more info here

What you want is a union in C or a discriminated union type in F# ; neither is possible to be defined in a nice syntax in C# .

This code provides you a generic class for using in the like scenarios:

public class Either<A, B>
{
    A _a;
    B _b;
    bool _isLeft;

    private Either() { }
    private Either(A a, B b, bool isLeft)
    {
        _isLeft = isLeft;

        if (_isLeft)
        {
            _a = a;
            return;
        }

        _b = b;
    }
    public Either(A a) : this(a, default(B), true) { }
    public Either(B b) : this(default(A), b, false) { }

    public A Left { get { return _a; } }
    public B Right { get { return _b; } }
    public bool HasLeft { get { return _isLeft; } }
}

You need to check HasLeft to see which value is actually there. One could use just an object field and some casting which I prefer to avoid in a statically typed language. There are other possible solutions too (using dynamic or struct ) but this (IMHO) will do.

Note: One other way is to derive each case class from a base class . The bad thing (IMHO again) about that is you have to provide a separate overloaded functions for each case class .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM