Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi every body
 
I want develop a method that accept a string parameter that this parameter can be null.
for more detail please attend to bellow code:
 
public void GetName(Nullable<string> name)
{
    if(name == null)
    {
         // do something
    }
    else
    {
        // do another something
    }
}
 
or
 
public void GetName(string? name)
{
    if(name == null)
    {
         // do something
    }
    else
    {
        // do another something
    }
}
 
but i have a error:
 
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'
 
please help me.
best regards
Posted 26-Jan-13 21:49pm
Comments
Amir Mahfoozi at 27-Jan-13 3:00am
   
http://stackoverflow.com/questions/187406/c-sharp-nullable-string-error
Reza Alipour Fard at 27-Jan-13 3:22am
   
thank you
Amir Mahfoozi at 27-Jan-13 3:31am
   
Good Luck
Amir Mahfoozi at 27-Jan-13 3:01am
   
http://stackoverflow.com/questions/2230657/help-with-c-sharp-generics-error-the-type-t-must-be-a-non-nullable-value-ty
Sergey Alexandrovich Kryukov at 27-Jan-13 3:05am
   
No matter. You have a correct answer.
—SA
Amir Mahfoozi at 27-Jan-13 3:06am
   
Thank you Sergey.
Sergey Alexandrovich Kryukov at 27-Jan-13 3:04am
   
Why would you possibly even try to create such type out of string? What's the idea?
—SA
Reza Alipour Fard at 27-Jan-13 3:21am
   
I need to a method for getting data from specific web service. in some rows some columns may be null.
Sergey Alexandrovich Kryukov at 27-Jan-13 11:59am
   
Understood and reasonable, but it does not explain you logic. Hope now you understand better how "nullability" works.
—SA
Reza Alipour Fard at 27-Jan-13 3:21am
   
thank you for attention
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Reza,
 
System.String is a reference type and is already "nullable".
 
Nullable<T> and the ? suffix are for value types
(e.g. Int32, Double, DateTime, etc)
 
Cheers,
Edo
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 27-Jan-13 3:03am
   
Correct, but you should have said that the nullable types can be constructed from any non-nullable type, which is any value type, not only predefined or primitive types, as one would mistakenly see from your answer. Importantly, it could be enumeration types or struct types as well. (I voted 4.)
—SA
Edo Tzumer at 27-Jan-13 3:41am
   
Your 4, Sergey, is praised as if it were 5+
;)
 
Cheers,
Edo
 
Sergey Alexandrovich Kryukov at 27-Jan-13 11:44am
   
:-)
Sergey Alexandrovich Kryukov at 27-Jan-13 12:14pm
   
I added another answer, but it's rather the answer to "Solution" 2.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

System.String is a reference type and is already "nullable" as EDO suggested.
 
but still you can change it to
 
public void GetName<t>(T s)
        {
            if (s == null)
            {
 
            }
        }</t>
 
so it will work as
 
GetName<string>("test");
GetName<string>(null);
GetName<someobject>(object1);</someobject></string></string>
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 27-Jan-13 11:56am
   
Doing so is not a good thing to do. As a minimum, it won't work as expected by the used of this method.
 
The check s == null only works because any object can be compared will null, but it will five false for a variable of any value type. Compare:
 

s = null // won't compile is it's not known that T is nullable

 
In other words, you reduce the problem to the initial on: it still depends on the nullability of T.
There is only one "universal" way of the check...
 
—SA
Sergey Alexandrovich Kryukov at 27-Jan-13 12:13pm
   
OK, I added the whole answer on this topic, but I'm afraid to say, this is not an answer to OP; this is an answer to you.
Please see Solution 3.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

[In response to Solution 2]
 
In my comment, I tried to explain what's wrong with it. You simply don't know generic solution, so my solution is not even directly related to your original question. The only reason for my advices is: it should "protect" you from wrong thinking shown in Solution 2. Let's see.
 
In principle, this will compile and execute:
string GetName<t>(T value) { // I improved naming and added return type according to method name
    if (value == null) { /* ... */ } // questionable check
    return value.ToString(); // for example
}</t>
 
Suppose you really need T to be generic. I case T is a value type, the expression value == null is always false, so what is really check up. The type is not nullable, so null is not really applicable, even though the check is always valid formally. To see that null is inapplicable, try
value = null; // won't compile
 
The only "universal" check you can do on all types is this
if (value == default(T)) { /* ... */ }
 
It will still check for null for generic types; for other types, it will check for some defaults: 0 for integer types, and the like.
 
—SA
  Permalink  
v2
Comments
nit_singh at 2-Feb-13 10:53am
   
Great SA..i didn't knew it specially the last lines
if (value == default(T))
thanks for ur suggestion
Sergey Alexandrovich Kryukov at 2-Feb-13 16:05pm
   
You are very welcome, and thanks,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 400
1 Maciej Los 222
2 OriginalGriff 213
3 BillWoodruff 130
4 Zoltán Zörgő 85
0 OriginalGriff 7,969
1 DamithSL 6,139
2 Sergey Alexandrovich Kryukov 5,449
3 Maciej Los 5,309
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100