c# - Constrain type to specific types -


Is it possible to disrupt a common method on specific types?

I have to write something like this:

  public T getValue & lt; T & gt; (String _attributeValue) where T: float, string {return default (t); // Some other things in reality}  

I'm mostly trying to avoid a huge switch statement inside the method or throw exceptions to specify an invalid type is.

EDIT: Ack I knew that the string is not the value type I'm sorry to start with the first two numerical types.

You do not use normal barriers to express the boundaries you are interested in Can do Generics are not meant to express variations on different types - they are meant to express variation that is integrated on type hierarchies (or implementing some interfaces).

You have some alternative options, however whatever you choose depends on the exact nature of what you are trying to do.

Use different named methods to express each operation. I use this approach when each method is really different to some, you can argue that returning a different type of value to a method is a different operation, and its own unique name.

  Float GetFloat (string attrName) {} string GetString (string attrName) {}  

to allow the type to be approximated Provide "default value" In many designs where you ask for value by name, default value. This can be overloaded to differentiate you from the methods used to overwrite (depending on the type of default value). Unfortunately, this approach is quite delicate - it breaks more easily, which believe the real values ​​for overload, which accept the numerical primitives (Int vs. uint vs. tall).

  Float GetValue (string attrName, float defaultValue) {...} string GetValue (string attrName, string defaultValue) {...}  

< Strong> Use a common method, but leave a type of time exception if the type is not one of those people that you support. Personally, I get violated the feeling of such ugly and generic - Generic should apply some hierarchy or some interface to certain types of sets. However, in some cases it is understandable (if such a particular type of support can not be supported, then we say). Another problem with this approach is that the signature of the generic method can not be estimated from any parameter, so when you call it you need to specify the desired type ... at that point it is not better (syntax From the point of comparison) names of different method.

  T getValue & lt; T & gt; (String attrName) {if (typeof (T)! = Typeof (string) || typef (t)! = Typef (float)) New NotSupportedException Throw (); Return Default (T); } // Call it by specifying the required type ... float f = getValue & lt; Float & gt; (AttrName); String s = GetValue & lt; String & gt; (AttrName);  

Use a parameter instead of return value. This approach works well, but calling it a method and a return value, because you have to first populate the populate. Get the zero (string attrName, float value out) zero GetValue (string attrName, outside string value) // examples of use: float F; GetValue (attrName, Out F); String s; GetValue (attrName, Out S);


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -