就像问题所述,我有一个类字段,它需要作为参数传递给一个方法,命名空间BasicMath正在另一个文件中使用,代码如下所述。
namespace BasicMath
{
class Operation
{
private static float number1 = 1;
public static float Add(Operation.number1, float number2)
{
return Operation.number1 + number2;
}
}
}
使用BasicMAth的地方:
using System;
using BasicMath;
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is the first number you want to operate on");
float number1 = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("What is the second number you want to operate on");
float number2 = Convert.ToSingle(Console.ReadLine());
Console.WriteLine("What is the operation you want to do on these numbers\n add(+), subtract(-),divide(/),multiply(*)");
string operation = Console.ReadLine();
if(operation == "+" || operation == "add"){
Console.WriteLine(Operation.Add(number1,number2));
}
我希望能够使用类字段number1
作为add
的参数,我知道要使用类字段,必须指定类,该字段在中引用,在本例中,类是operation
,该类中的字段是number1
。
不客气。
namespace BasicMath
{
class Operation
{
private static float number1 = 1;
public static float Add(float number2)
{
return number1 + number2;
}
}
}