将空参数传递给 C# 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/271588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing null arguments to C# methods
提问by Niko Gamulin
Is there a way to pass null arguments to C# methods (something like null arguments in c++)?
有没有办法将空参数传递给 C# 方法(类似于 C++ 中的空参数)?
For example:
例如:
Is it possible to translate the following c++ function to C# method:
是否可以将以下 C++ 函数转换为 C# 方法:
private void Example(int* arg1, int* arg2)
{
if(arg1 == null)
{
//do something
}
if(arg2 == null)
{
//do something else
}
}
采纳答案by Sander
Yes. There are two kinds of types in .NET: reference types and value types.
是的。.NET 中有两种类型:引用类型和值类型。
References types (generally classes) are always referred to by references, so they support null without any extra work. This means that if a variable's type is a reference type, the variable is automatically a reference.
引用类型(通常是类)总是被引用引用,因此它们支持 null 而不需要任何额外的工作。这意味着如果变量的类型是引用类型,则该变量自动成为引用。
Value types (e.g. int) by default do not have a concept of null. However, there is a wrapper for them called Nullable. This enables you to encapsulate the non-nullable value type and include null information.
默认情况下,值类型(例如 int)没有 null 的概念。但是,它们有一个名为 Nullable 的包装器。这使您能够封装不可为空的值类型并包含空信息。
The usage is slightly different, though.
不过用法略有不同。
// 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.
}
回答by Marcin K
From C# 2.0:
从 C# 2.0 开始:
private void Example(int? arg1, int? arg2)
{
if(arg1 == null)
{
//do something
}
if(arg2 == null)
{
//do something else
}
}
回答by MADMap
You can use NullableValueTypes (like int?) for this. The code would be like this:
您可以为此使用 NullableValueTypes(如 int?)。代码如下:
private void Example(int? arg1, int? arg2)
{
if(!arg1.HasValue)
{
//do something
}
if(!arg2.HasValue)
{
//do something else
}
}
回答by user35412
Starting from C# 2.0, you can use the nullable generic type Nullable, and in C# there is a shorthand notation the type followed by ?
从 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
}
}
回答by mackenir
I think the nearest C# equivalent to int*
would be ref int?
. Because ref int?
allows the called method to pass a value back to the calling method.
我认为最近的C#相当于int*
会ref int?
。因为ref int?
允许被调用方法将值传递回调用方法。
int*
int*
- Can be null.
- Can be non-null and point to an integer value.
- If not null, value can be changed, and the change propagates to the caller.
- Setting to null is not passed back to the caller.
- 可以为空。
- 可以为非 null 并指向整数值。
- 如果不为 null,则 value 可以更改,并且更改会传播到调用者。
- 设置为 null 不会传回给调用者。
ref int?
ref int?
- Can be null.
- Can have an integer value.
- Value can be always be changed, and the change propagates to the caller.
- Value can be set to null, and this change will also propagate to the caller.
- 可以为空。
- 可以有一个整数值。
- 值始终可以更改,并且更改会传播给调用者。
- 值可以设置为 null,并且此更改也将传播到调用者。
回答by ruffin
The OP's question is answered well already, but the title is just broad enough that I think it benefits from the following primer:
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() + "#");
}
}
}
output:
输出:
#1/1/0001 12:00:00 AM#
##
Done. Return to quit
回答by juancalbarran
You can use 2 ways: int? or Nullable, both have the same behavior. You could to make a mix without problems but is better choice one to make code cleanest.
您可以使用 2 种方式: int? 或 Nullable,两者具有相同的行为。您可以毫无问题地进行混合,但最好选择一种使代码最干净。
Option 1 (With ?):
选项 1(带?):
private void Example(int? arg1, int? arg2)
{
if (arg1.HasValue)
{
//do something
}
if (arg1.HasValue)
{
//do something else
}
}
Option 2 (With Nullable):
选项 2(可空):
private void Example(Nullable<int> arg1, Nullable<int> arg2)
{
if (arg1.HasValue)
{
//do something
}
if (arg1.HasValue)
{
//do something else
}
}
From C#4.0 comes a new way to do the same with more flexibility, in this case the framework offers optional parameters with default values, of this way you can set a default value if the method is called without all parameters.
从 C#4.0 开始,有一种新方法可以更灵活地完成相同的操作,在这种情况下,框架提供了带有默认值的可选参数,如果在没有所有参数的情况下调用方法,您可以通过这种方式设置默认值。
Option 3 (With default values)
选项 3(使用默认值)
private void Example(int arg1 = 0, int arg2 = 1)
{
//do something else
}