C# 创建值直接位于函数参数内的列表
而不是做:C# 创建值直接位于函数参数内的列表,c#,C#,而不是做: void MyFunction(List<float> list) { // do something } List<float> myList = new List<float>(); myList.Add(3.0); myList.Add(4.0); MyFunction(myList); void MyFunction(列表) { //做点什么 } List myList=新列表(); myList.Add(3.0); myLis
void MyFunction(List<float> list)
{
// do something
}
List<float> myList = new List<float>();
myList.Add(3.0);
myList.Add(4.0);
MyFunction(myList);
void MyFunction(列表)
{
//做点什么
}
List myList=新列表();
myList.Add(3.0);
myList.Add(4.0);
MyFunction(myList);
有没有办法做这样的事情呢
MyFunction(new List<float>{3.0,4.0}());
MyFunction(新列表{3.0,4.0}());
这意味着我将所有初始化都直接保存在函数参数本身中。你真的很接近了。删除最后一对括号(并将双打转换为浮点数):
MyFunction(新列表{3.0f,4.0f});
你真的很接近了。删除最后一对括号(并将双打转换为浮点数):
MyFunction(新列表{3.0f,4.0f});
你真的很接近了。删除最后一对括号(并将双打转换为浮点数):
MyFunction(新列表{3.0f,4.0f});
你真的很接近了。删除最后一对括号(并将双打转换为浮点数):
MyFunction(新列表{3.0f,4.0f});
添加有关最酷的参数的信息
关键字。它允许您执行以下操作:
void MyFunction(params float[] list)
{
// do something
}
MyFunction(new float[] { 3.0f, 4.0f }); // valid call
MyFunction(3.0f, 4.0f); // also valid
MyFunction(32f); // also valid
MyFunction(); // also valid, beware! list will be an empty array
MyFunction(null); // also valid, beware! list will be null
注意,这样做:
newfloat[]{3.0f,4.0f}
就是所谓的集合初始值设定项。无论是否将其作为参数发送,都可以使用它。其他好处是使用关键字params
的好处,这实际上只是使它更短、更易于使用。添加有关最酷的params
关键字的信息。它允许您执行以下操作:
void MyFunction(params float[] list)
{
// do something
}
MyFunction(new float[] { 3.0f, 4.0f }); // valid call
MyFunction(3.0f, 4.0f); // also valid
MyFunction(32f); // also valid
MyFunction(); // also valid, beware! list will be an empty array
MyFunction(null); // also valid, beware! list will be null
注意,这样做:
newfloat[]{3.0f,4.0f}
就是所谓的集合初始值设定项。无论是否将其作为参数发送,都可以使用它。其他好处是使用关键字params
的好处,这实际上只是使它更短、更易于使用。添加有关最酷的params
关键字的信息。它允许您执行以下操作:
void MyFunction(params float[] list)
{
// do something
}
MyFunction(new float[] { 3.0f, 4.0f }); // valid call
MyFunction(3.0f, 4.0f); // also valid
MyFunction(32f); // also valid
MyFunction(); // also valid, beware! list will be an empty array
MyFunction(null); // also valid, beware! list will be null
注意,这样做:
newfloat[]{3.0f,4.0f}
就是所谓的集合初始值设定项。无论是否将其作为参数发送,都可以使用它。其他好处是使用关键字params
的好处,这实际上只是使它更短、更易于使用。添加有关最酷的params
关键字的信息。它允许您执行以下操作:
void MyFunction(params float[] list)
{
// do something
}
MyFunction(new float[] { 3.0f, 4.0f }); // valid call
MyFunction(3.0f, 4.0f); // also valid
MyFunction(32f); // also valid
MyFunction(); // also valid, beware! list will be an empty array
MyFunction(null); // also valid, beware! list will be null
注意,这样做:newfloat[]{3.0f,4.0f}
就是所谓的集合初始值设定项。无论是否将其作为参数发送,都可以使用它。其他好处是使用关键字params
,这实际上只是使其更短、更易于使用。您可以使用a或a
您的示例无法编译,因为存在禁止的隐式强制转换,精度从double降低到float
您需要精度低于目标类型的数字文字、精确类型数字文字或显式转换
以下示例显示了方法参数中的列表和参数数组初始化:
[TestMethod]
public void TestMethod1()
{
// Original code with correct constant type.
MyFunction(new List<float> { 3.0f, 4.0f });
// Using an overload with a params array argument.
MyFunction(3.0f, 4.0f);
// Various float constant flavours.
MyFunction(3f, 3.0f, .0f, 3e-10f, (float)3);
// Implicit cast to float from anything with lesser precision or an explicit cast to float.
MyFunction((byte)1, (short)1, (int)1, 1, (long)1, 1L, (float)1);
}
void MyFunction(List<float> list)
{
throw new NotImplementedException();
}
void MyFunction(params float[] args)
{
throw new NotImplementedException();
}
[TestMethod]
公共void TestMethod1()
{
//具有正确常量类型的原始代码。
MyFunction(新列表{3.0f,4.0f});
//使用带有params数组参数的重载。
MyFunction(3.0f、4.0f);
//各种各样的香味。
MyFunction(3f,3.0f,.0f,3e-10f,(浮动)3);
//从精度较低的任何对象隐式转换为浮点,或显式转换为浮点。
MyFunction((字节)1,(短)1,(int)1,1,(长)1,1L,(浮点)1);
}
void MyFunction(列表)
{
抛出新的NotImplementedException();
}
void MyFunction(参数float[]args)
{
抛出新的NotImplementedException();
}
您可以使用或
您的示例无法编译,因为存在禁止的隐式强制转换,精度从double降低到float
您需要精度低于目标类型的数字文字、精确类型数字文字或显式转换
以下示例显示了方法参数中的列表和参数数组初始化:
[TestMethod]
public void TestMethod1()
{
// Original code with correct constant type.
MyFunction(new List<float> { 3.0f, 4.0f });
// Using an overload with a params array argument.
MyFunction(3.0f, 4.0f);
// Various float constant flavours.
MyFunction(3f, 3.0f, .0f, 3e-10f, (float)3);
// Implicit cast to float from anything with lesser precision or an explicit cast to float.
MyFunction((byte)1, (short)1, (int)1, 1, (long)1, 1L, (float)1);
}
void MyFunction(List<float> list)
{
throw new NotImplementedException();
}
void MyFunction(params float[] args)
{
throw new NotImplementedException();
}
[TestMethod]
公共void TestMethod1()
{
//具有正确常量类型的原始代码。
MyFunction(新列表{3.0f,4.0f});
//使用带有params数组参数的重载。
MyFunction(3.0f、4.0f);
//各种各样的香味。
MyFunction(3f,3.0f,.0f,3e-10f,(浮动)3);
//从精度较低的任何对象隐式转换为浮点,或显式转换为浮点。
MyFunction((字节)1,(短)1,(int)1,1,(长)1,1L,(浮点)1);
}
void MyFunction(列表)
{
抛出新的NotImplementedException();
}
void MyFunction(参数float[]args)
{
抛出新的NotImplementedException();
}
您可以使用或
您的示例无法编译,因为存在禁止的隐式强制转换,精度从double降低到float
您需要精度低于目标类型的数字文字、精确类型数字文字或显式转换
以下示例显示了方法参数中的列表和参数数组初始化:
[TestMethod]
public void TestMethod1()
{
// Original code with correct constant type.
MyFunction(new List<float> { 3.0f, 4.0f });
// Using an overload with a params array argument.
MyFunction(3.0f, 4.0f);
// Various float constant flavours.
MyFunction(3f, 3.0f, .0f, 3e-10f, (float)3);
// Implicit cast to float from anything with lesser precision or an explicit cast to float.
MyFunction((byte)1, (short)1, (int)1, 1, (long)1, 1L, (float)1);
}
void MyFunction(List<float> list)
{
throw new NotImplementedException();
}
void MyFunction(params float[] args)
{
throw new NotImplementedException();
}
[TestMethod]
公共void TestMethod1()
{
//具有正确常量类型的原始代码。
MyFunction(新列表{3.0f,4.0f});
//使用带有params数组参数的重载。
MyFunction(3.0f、4.0f);
//各种各样的香味。
MyFunction(3f,3.0f,.0f,3e-10f,(浮动)3);
//从精度较低的任何对象隐式转换为浮点,或显式转换为浮点。
MyFunction((字节)1,(短)1,(int)1,1,(长)1,1L,(浮点)1);
}
void MyFunction(列表)
{
抛出新的NotImplementedException();
}
void MyFunction(参数float[]args)
{
抛出新的NotImplementedExcepti