Yahya Hussein
Yahya Hussein

Reputation: 9111

Possibility of using value types as reference types in C#

Might seems odd, but is it somehow possible to use value types as reference types in C# ?

I am using a library where there is a method that needs the message type to be passed.

The thing is that it does not accept value types as message type, and all I want to receive as a message is a Guid, now if I do the following

public class GUID
    {
        public  Guid Guid { get; set; }
    }

And pass GUID instead of Guid, it is working.

This made me think, is there an alternative way of overriding this issue? and if there is not any, why is that? Is not there a real life examples when primitive types are actually objects?

Upvotes: 1

Views: 158

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062695

Assuming object doesn't work, you could perhaps use:

public sealed class Box<T> where T : struct
{
    public T Value {get;set;}
}

Then you at least don't need to define lots of types. Your guid would be Box<Guid>, obviously.

Upvotes: 5

Related Questions