将空参数传递给C#方法

40

有没有办法向 C# 方法传递空参数(类似于 c++ 中的 null 参数)?

例如:

是否可以将以下 C++ 函数转换为 C# 方法:

private void Example(int* arg1, int* arg2)
{
    if(arg1 == null)
    {
        //do something
    }
    if(arg2 == null)
    {
        //do something else
    }
}
7个回答

92

是的,在.NET中有两种类型:引用类型和值类型。

引用类型(通常是类)总是通过引用引用,因此它们支持null而无需任何额外的工作。这意味着如果一个变量的类型是引用类型,那么该变量自动成为引用。

值类型(例如int)默认情况下没有null的概念。但是,对于它们有一个包装器叫Nullable,可以封装不可为空的值类型并包含null信息。

然而,使用方式略有不同。

// Both of these types mean the same thing, the ? is just C# shorthand.
private void Example(int? arg1, Nullable<int> arg2)
{
    if (arg1.HasValue)
        DoSomething();

    arg1 = null; // Valid.
    arg1 = 123;  // Also valid.

    DoSomethingWithInt(arg1); // NOT valid!
    DoSomethingWithInt(arg1.Value); // Valid.
}

1
请问您能告诉我 private void Example(int? arg1)private void Example(int? arg1 = 0) 的区别吗? - Unbreakable
@Unbreakable 如果你还没有弄清楚,第一个例子总是需要传入arg1。第二个例子允许你在不传入arg1的情况下调用该方法,这意味着它将默认为0。 - user1779775
好的回答!你说:“这意味着如果一个变量的类型是引用类型,那么该变量就自动成为引用。”但我认为在这里还应该注意到,当引用类型的值为null时,并不是这种情况。你有什么想法吗? - Zimano

9
我认为最接近C#中 int* 的是 ref int?。因为 ref int? 允许被调用的方法将值传回到调用方法。 int*
  • 可以为空。
  • 可以非空并指向整数值。
  • 如果非空,则值可以更改,且更改会传播到调用方。
  • 设置为空不会传递给调用方
ref int?
  • 可以为空。
  • 可以有一个整数值。
  • 值始终可以更改,且更改会传播到调用方。
  • 值可以设置为空,此更改也将传播到调用方

8
您可以使用可空值类型(例如 int?)来实现此功能。代码将如下所示:
private void Example(int? arg1, int? arg2)
{
    if(!arg1.HasValue)
    {
        //do something
    }
    if(!arg2.HasValue)
    {
        //do something else
    }
}

5

从C# 2.0开始:

private void Example(int? arg1, int? arg2)
{
    if(arg1 == null)
    {
        //do something
    }
    if(arg2 == null)
    {
        //do something else
    }
}

3

OP的问题已经得到了很好的回答,但是标题太过宽泛,我认为以下入门指南会有所帮助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consolePlay
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.test(new DateTime());
            Program.test(null);
            //Program.test(); // <<< Error.  
            // "No overload for method 'test' takes 0 arguments"  
            // So don't mistake nullable to be optional.

            Console.WriteLine("Done.  Return to quit");
            Console.Read();
        }

        static public void test(DateTime? dteIn)
        {
            Console.WriteLine("#" + dteIn.ToString() + "#");
        }
    }
}

输出:

#1/1/0001 12:00:00 AM#
##
Done.  Return to quit

3

从C# 2.0开始,您可以使用可空泛型类型Nullable,在C#中有一种简写方式,即在类型后面加上?

例如:

private void Example(int? arg1, int? arg2)
{
    if(arg1 == null)
    {
        //do something
    }
    if(arg2 == null)
    {
        //do something else
    }
}

3
你可以使用两种方式:int? 或者 Nullable,它们的行为是相同的。你可以混合使用,但是最好选择一种以使代码更加清晰。
选项1(带有?):
private void Example(int? arg1, int? arg2)
    {
        if (arg1.HasValue)
        {
            //do something
        }
        if (arg1.HasValue)
        {
            //do something else
        }
    }

选项二(可为空):

private void Example(Nullable<int> arg1, Nullable<int> arg2)
    {
        if (arg1.HasValue)
        {
            //do something
        }
        if (arg1.HasValue)
        {
            //do something else
        }
    }

从C#4.0开始,有一种新的方法可以更加灵活地完成相同的操作,即框架提供了带默认值的可选参数,这样你就可以在方法被调用时设置默认值,即使没有传入所有参数。
选项3(带默认值)
private void Example(int arg1 = 0, int arg2 = 1)
    {
        //do something else
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接