使用可为空的字符串模式开发方法 [英] Develop a method with a nullable string patameter

查看:73
本文介绍了使用可为空的字符串模式开发方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧



我想开发一个接受字符串参数的方法,该参数可以为null。

了解更多细节请参阅以下代码:



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
    }
}





我有一个错误:



类型''string''必须是非可空值类型才能在通用类型或方法中将其用作参数''T'' 'System.Nullable< t>''



请帮帮我。

best r egards



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

推荐答案

嗨Reza,



System.String 是一个引用类型,已经是可空的。



Nullable< T> 后缀用于值类型

(例如Int32,Double,DateTime等)



干杯,

Edo
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


System.String 是一个引用类型,已经像EDO建议的那样可以为空。



但你仍然可以将其更改为



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>


[回应解决方案2]



在我的评论中,我试图解释它有什么问题。你根本就不知道通用解决方案,所以我的解决方案甚至与你原来的问题没有直接关系。我建议的唯一原因是:它应该保护你免受解决方案2中显示的错误思考。让我们看看。



原则上,这将编译并执行:

[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>





假设您确实需要 T 是通用的。我的情况 T 是一个值类型,表达式 value == null 总是为false,所以真正检查的是什么。该类型不可为空,因此即使检查始终正式有效,null也不适用。要查看 null 不适用,请尝试



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对于泛型类型,仍会检查 null ;对于其他类型,它将检查一些默认值:0表示整数类型等。



-SA



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


这篇关于使用可为空的字符串模式开发方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆