Do you get any operations when you restrict the generic type using T: struct?

This may be a bit of an abstract question, so apologize in advance.

I am considering generics in .NET and wondering about where T : struct constraint.

I understand that this allows you to limit the type used for the value type. My question is that without type restriction you can perform a limited number of operations on T.

Do you get the opportunity to use any additional operations when you specify where T : struct , or is this the only value in the type restriction that you can pass?

Edit

Some interesting answers so far, thanks. I guess the question I'm actually asking is that if I were to write (in a discussion on how to use generics),

"Now that you have limited enter an argument of type value, you can also do ___________________ on / with objects of this type"

Is there anything that can be put in this space? I can think of things for other restrictions, but not for this.

+7
generics c #
source share
4 answers

All that T : struct gets is the implicit constructor new() and a few obvious things related to null . Perhaps more importantly, callers cannot use classes, interfaces, or Nullable<T> .

What types of operations do you perform? For operators, try dynamic in 4.0 or MiscUtil in 3.5

+4
source share

The only thing you get compared to other possible sets of restrictions is the ability to work with values ​​of type Nullable<T> (therefore T: struct prevents the caller from passing Nullable<T> as a type parameter - it can "t be nested).

+2
source share

No, you do not get any operations on T by specifying the where T: struct restriction where T: struct generic type. You limit your subscribers to only specifying value types (with the exception of Nullable<T> value types, which are not allowed).

0
source share

One type of operation that is not guaranteed to be available with a changed class and sometimes may be available with a changed structure is the ability to copy the state of a thing using an assignment operator. Using mutating interfaces with structures is difficult, since trying to use a mutating interface in a structure in a read-only context will produce code that compiles cleanly but does not work (I really want compilers to recognize the attribute for structure and interface elements that indicate that they should not be used in read-only structures). However, there are some contexts in which such a thing may be useful.

For example, if the algorithm required to save the state of the enumerator and returned the enumerator to its stored state, and if the enumerator was a common TEnum type, limited by both struct and IEnumerator<T>, it may be possible to copy the TEnum to a local variable of that type and then later copy it back. Note that this would work with some but not all to a local variable of that type and then later copy it back. Note that this would work with some but not all struct types which implement IEnumerable ; such a technique should in practice probably only be used with interfaces that explicitly document a requirement that all legitimate struct-type implementations must exhibit value semantics ( ; such a technique should in practice probably only be used with interfaces that explicitly document a requirement that all legitimate struct-type implementations must exhibit value semantics ( IEnumerator` does not have such a documented requirement).

Please note that the fact that the code can use the ability to copy structures by value when it exists, which excludes the possibility of using a slightly different (possibly less efficient) method using the implementation of the class interface. Feature Signatures:

 void ActOnSequence<T>(T theEnumerator) where T:struct, IEnumerator<String>; void ActOnSequence(IEnumerator<String> theEnumerator); 

can coexist and be used without difficulty. The former will be called to implement the value type of the interface, while the latter will be called when implementing the heap type. Note that without the restriction of struct it would be impossible to have both of these methods in scope and automatically call the correct method.

0
source share

All Articles