将空参数传递给C#方法
有没有一种方法可以将空参数传递给C#方法(类似于C++中的空参数) 例如:将空参数传递给C#方法,c#,methods,null,C#,Methods,Null,有没有一种方法可以将空参数传递给C#方法(类似于C++中的空参数) 例如: 有可能将以下C++函数转换成C<方法: 从C#2.0开始: 您可以为此使用NullableValueTypes(比如int?)。代码如下所示: private void Example(int? arg1, int? arg2) { if(!arg1.HasValue) { //do something } if(!arg2.HasValue) {
有可能将以下C++函数转换成C<方法:
从C#2.0开始:您可以为此使用NullableValueTypes(比如int?)。代码如下所示:
private void Example(int? arg1, int? arg2)
{
if(!arg1.HasValue)
{
//do something
}
if(!arg2.HasValue)
{
//do something else
}
}
从C#2.0开始,您可以使用可为null的泛型类型nullable,在C#中,有一个速记符号,类型后跟
e、 g
对。NET中有两种类型:引用类型和值类型 引用类型(通常是类)总是由引用引用引用,因此它们支持null而不需要任何额外的工作。这意味着,如果变量的类型是引用类型,则该变量将自动成为引用 默认情况下,值类型(例如int)没有null的概念。但是,它们有一个称为Nullable的包装器。这使您能够封装不可为null的值类型并包含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.
}
//这两种类型的含义相同,是吗?只是简写而已。
私有无效示例(int?arg1,可为空的arg2)
{
if(arg1.HasValue)
DoSomething();
arg1=null;//有效。
arg1=123;//也有效。
DoSomethingWithInt(arg1);//无效!
DoSomethingWithInt(arg1.Value);//有效。
}
我认为与int*最接近的C#等价物应该是ref int?
。因为ref int?
允许被调用方法将值传递回调用方法
int*
- 可以为空
- 可以为非null并指向整数值
- 如果不为null,则可以更改值,并将更改传播到调用方
- 设置为null不会传回调用方
ref int?
- 可以为空
- 可以有一个整数值
- 值总是可以更改的,并且更改会传播到调用方
- 值可以设置为null,并且此更改也会传播到调用方
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() + "#");
}
}
}
您可以使用两种方式:int?或者可以为null,两者具有相同的行为。您可以毫无问题地进行混合,但最好的选择是使代码最干净
选项1(带?):
选项2(可为空):
你能告诉我私有无效示例(int?arg1)
和私有无效示例(int?arg1=0)
@Unbreakable的区别吗?如果你现在还没有弄清楚,第一个示例总是要求传入arg1。第二个示例允许您在不传入arg1的情况下调用该方法,这意味着它将默认为0。回答得好!您说过“这意味着,如果变量的类型是引用类型,那么该变量将自动成为引用。”但我认为这里还应该注意,当该引用类型的值为null
时,情况并非如此。有什么想法吗?
private void Example(int? arg1, int? arg2)
{
if(arg1 == null)
{
//do something
}
if(arg2 == null)
{
//do something else
}
}
// 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.
}
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
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
}
}
private void Example(int arg1 = 0, int arg2 = 1)
{
//do something else
}