Re: can constraint generics using string or not ?

Tech-Archive recommends: Fix windows errors by optimizing your registry



SHEBERT wrote:
// CS0703: `string' is not a valid constraint. A constraint must be an
interface, a non-sealed class or a type parameter


public class C<T> where T : string
{
}


Why can't I constraint using string but I can constraint like bellow and
it accept string?

public class C<T>
{
}

except now T accept anything, and I would like string, in fact, what I
would like is this:

public class C<T> WHERE T : struct, string
{
}

I agree with Dustin, what's your point in doing this? Restricting a generic to be a type that is a sealed class removes your ability for it to be constructed from anything other than that sealed class, in this case string, so it's no longer generic.

Since this is not a valid use of generics, possibly you are trying to solve a problem for which there is a better way than generics. What is it you are trying to accomplish?

--
Tom Porterfield

.



Relevant Pages

  • Re: can constraint generics using string or not ?
    ... A constraint is just that, it says that T can ONLY derive from the types ... Even if you said just string, it would make no sense because string is ... if you do agree that the example below would apply to struct and string ... public class CWHERE T: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Whats Wrong With This Alter Table Statement?
    ... DROP {COLUMN field I CONSTRAINT indexname}} ... > String which VBA won't understands the way you wrote it in VBA code. ... > need to write VBA code to "wrap around" the SQL String so that VBA knows ...
    (microsoft.public.access.queries)
  • Re: How to implement nested dictionaries?
    ... string does not have a zero parameter ... dictionaries and I guess string does not match the new constraint! ... public class SerializableDictionary<TKey, TValue> ... XmlSerializer valueSerializer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SQL
    ... And you've showed nothing that makes it dependent on them (barring a ... constraint it will have in your "disconnected" view. ... Just a naked string. ... there is no such thing as "context typing." ...
    (comp.object)
  • Re: numbers as generics parameters
    ... Note also that the constraint to have equal strings is only an example ... type Pascal_String is new String; ... the subtype is a proper constraint. ... why generics are not a ...
    (comp.lang.ada)