Click here to Skip to main content
12,503,526 members (72,231 online)
Rate this:
 
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 20:49pm
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
   
No matter. You have a correct answer.
—SA
Amir Mahfoozi 27-Jan-13 3:06am
   
Thank you Sergey.
   
Why would you possibly even try to create such type out of string? What's the idea?
—SA
Reza Alipour Fard 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 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 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
   
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 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
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>
  Permalink  
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
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 2-Feb-13 10:53am
   
Great SA..i didn't knew it specially the last lines
if (value == default(T))
thanks for ur suggestion
   
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160919.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2016
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