将空参数传递给C#方法

时间:2008-11-07 09:13:10

标签: c# methods null

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

例如:

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

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

7 个答案:

答案 0 :(得分:71)

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

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

默认情况下,值类型(例如int)没有null的概念。但是,有一个名为Nullable的包装器。这使您可以封装非可空值类型并包含空信息。

但使用情况略有不同。

// 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 :(得分:6)

我认为最接近int*的C#将是ref int?。因为ref int?允许被调用的方法将值传递回调用方法。

int*

  • 可以为null。
  • 可以为非null并指向整数值。
  • 如果不为null,则可以更改值 ,并且更改会传播给调用者。
  • 设置为null不会传回给调用者

ref int?

  • 可以为null。
  • 可以有一个整数值。
  • 值可以随时更改 ,并且更改会传播给调用者。
  • 可以将值设置为null,此更改也会传播给调用者

答案 2 :(得分:5)

你可以使用NullableValueTypes(比如int?)。代码如下:

private void Example(int? arg1, int? arg2)
{
    if(!arg1.HasValue)
    {
        //do something
    }
    if(!arg2.HasValue)
    {
        //do something else
    }
}

答案 3 :(得分:3)

来自C#2.0:

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

答案 4 :(得分:3)

从C#2.0开始,您可以使用可空的泛型类型Nullable,在C#中有一个简写符号,后面跟着类型?

e.g。

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

答案 5 :(得分:2)

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

答案 6 :(得分:0)

您可以使用2种方式:int?或Nullable,都有相同的行为。你可以毫无问题地进行混音,但最好选择一个让代码最干净的选择。

选项1(有?):

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

选项2(使用Nullable):

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

从C#4.0开始,提供了一种更灵活的新方法,在这种情况下,框架提供optional parameters with default values,这样,如果在没有所有参数的情况下调用方法,您可以设置默认值。 / p>

选项3(使用默认值)

private void Example(int arg1 = 0, int arg2 = 1)
    {
        //do something else
    }
相关问题