Home > database >  Need clarity over what happens when you cast an object to value or reference type compared to using
Need clarity over what happens when you cast an object to value or reference type compared to using

Time:01-23

So have this property Value in a class. I need an interchangeable type of property for Value as wont know the type until it arrives from the DB and can be either reference or value type (see examples at bottom)

What I'm not clear about is once an object or Dynamic variable has been cast once for that instance of the class does it retain that info or do I need to keep casting it every time it Gets or Sets ? Very confused as to the best way to do this.

My hope was to find a way to set Value type only once per instance of the class its in and using either object or dynamic it retains that type for the life of the class, and does not need to be recast each time its "get" or "set"

Is that possible.

thanks for any help

Perhaps I should do this

object _value = Convert.ChangeType(value, (typecast_var_goes_here));
or
Dynamic _value = Convert.ChangeType(value, (typecast_var_goes_here));

once in the constructor of the class and eliminate the casting that occurs in the property getter and setter ?

or I could use

private object _value; 
public object Value
    {
        get
        {
            return Convert.ChangeType(_value, typecast_var_goes_here);
        }
        set
        {
          _value = Convert.ChangeType(value, typecast_var_goes_here);
                     
        }
    }

or could use

private dynamic _value;
public dynamic Value
    {
        get
        {
            return Convert.ChangeType(_value, (typecast_var_goes_here));
        }
        set
        {
          _value = Convert.ChangeType(value, typecast_var_goes_here);
                     
        }
    }

CodePudding user response:

If you have an object of type object, you need to cast it to something else each time you want to use it. You should use the is operator or a switch to check the type. Once you have a variable of the correct type you may be able to keep it to avoid further casting.

A potential option would be to use generics:

public class MyClass<T>{
    public T MyValue {get;set;}
}

This lets you keep type information and avoids casting, but MyClass<int> would now be a completely different type from ``MyClass`, so this pattern may tend to 'spread' thru the code base.

I would recommend trying to refactor the code if you feel the need to use object. This generally makes the code more difficult to read and understand, since you lack any static type information. Using dynamics is even worse in my opinion, you are not only voluntarily disabling static type checking, you also get a fairly hefty runtime penalty.

A somewhat better option would be to use a empty interface on all the possible types the object could have. That way you could use something like the visitor pattern to keep type safety.

CodePudding user response:

I try to answer but I don't know if I understood your question correctly.
It is the basis of OOP: if the variable is not declared static, then depending on the instance of the class it will maintain its context, so you do not have to do the conversion every time.
With dynamic you can avoid doing the conversion since the type will be taken at runtime

  • Related