Can an open type be a structure?

I have some types that are structures for performance reasons and have some commonality. I would like to know if I can reorganize them as open-type structures - and if I can expect any problems, if I can.

+4
source share
2 answers

Yes, you can. Some of the common types provided by the framework library, such as KeyValuePair<TKey, TValue> , are really structs .

+5
source

UPDATE: Apparently, with "open type" you don't mean the same thing as defining the C # specification of "open type". You mean "generic type."

Yes, structures can be shared.

To answer the second part of your question: I do not know what “problems” you have in mind. Can you give me an example of what you consider problematic?


Original answer, assuming you really asked about open types:

The C # specification is clear:

• A type defines an open type.

• An array type is an open if type and only if its element type is an open type.

• A constructed type is an open type if and only if one or more of its type is an open type. a constructed nested type is an open type if and only if one or more of its type arguments or type arguments of its containing type (s) is an open type.

So you go. A struct type is a public type if and only if it or its private type is a typical public type for one of its type arguments. For instance:

 struct S<T> { S<T> s1; // open struct type Nullable<S<T>> s2; // another open struct type } class O<V> { class C<U> where U : struct { struct R { U u; // open type constrained to be value type R r1; // open struct type; this is O<V>.C<U>.R O<V>.C<S<U>>.R r2; // another open struct type. O<int>.C<S<V>>.R r3; // another open struct type } } } 

We can continue to create open types of structures all day.

+4
source

Source: https://habr.com/ru/post/1333082/


All Articles