threexmods
Programmer
Hi I wonder if somebody could point me in the right direction with this problem?
I've got the follow class, which is using an object as a paramter in order to represent a variety of types, in this example ints and datetimes.
Could somebody tell me if this is a good candidate to be moved to used generics types, so that I can avoid boxing / unboxing during the casting that is going on in the ToString method.
As you can see I'm using a generic parameter for the class but the tostring method needs to convert it to a certain type in order to format the return string value.
Can anybody give me any pointers....
Thanks, Mark
I've got the follow class, which is using an object as a paramter in order to represent a variety of types, in this example ints and datetimes.
Code:
public class MyClass
{
private object _value;
public MyClass(object value)
{
_value = value;
}
public override string ToString()
{
if (_value is int)
{
return string.Format("{0}", _value);
}
if (_value is DateTime)
{
return string.Format("'{0}'", ((DateTime)_value).ToString("dd MMM yyyy HH:mm:ss"));
}
}
}
Could somebody tell me if this is a good candidate to be moved to used generics types, so that I can avoid boxing / unboxing during the casting that is going on in the ToString method.
As you can see I'm using a generic parameter for the class but the tostring method needs to convert it to a certain type in order to format the return string value.
Can anybody give me any pointers....
Thanks, Mark