Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
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<t>'

please help me.
best regards
Posted
Comments
Amir Mahfoozi 27-Jan-13 3:00am    
http://stackoverflow.com/questions/187406/c-sharp-nullable-string-error
Reza Alipour Fard 27-Jan-13 3:22am    
thank you
Amir Mahfoozi 27-Jan-13 3:31am    
Good Luck
Amir Mahfoozi 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 27-Jan-13 3:05am    
No matter. You have a correct answer.
—SA

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
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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
Joezer BH 27-Jan-13 3:41am    
Your 4, Sergey, is praised as if it were 5+
;)

Cheers,
Edo

Sergey Alexandrovich Kryukov 27-Jan-13 11:44am    
:-)
Sergey Alexandrovich Kryukov 27-Jan-13 12:14pm    
I added another answer, but it's rather the answer to "Solution" 2.
—SA
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>
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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 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
[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:
C#
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
C#
value = null; // won't compile


The only "universal" check you can do on all types is this
C#
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
 
Share this answer
 
v2
Comments
nit_singh 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900