C# nullable string error

Mike Fielden picture Mike Fielden · Oct 9, 2008 · Viewed 134.9k times · Source
private string? typeOfContract
{
  get { return (string?)ViewState["typeOfContract"]; }
  set { ViewState["typeOfContract"] = value; }
}

Later in the code I use it like this:

typeOfContract = Request.QueryString["type"];

I am getting the following error at the declaration of typeOfContract line stating:

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'

Any ideas? Basically, I want to make sure that "type" exists in the QueryString before performing an action.

Answer

Joe picture Joe · Oct 9, 2008

System.String is a reference type and already "nullable".

Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.