C#创建和使用函数
我需要C#编程方面的帮助;我是新手,来自C语言背景。我有这样一个控制台应用程序:C#创建和使用函数,c#,function,C#,Function,我需要C#编程方面的帮助;我是新手,来自C语言背景。我有这样一个控制台应用程序: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Add_Function { class Program { static void Main(string[] args) { int a; int
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Add_Function
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
//why can't I not use it this way?
c = Add(a, b);
Console.WriteLine("a + b = {0}", c);
}//END Main
public int Add(int x, int y)
{
int result = x + y;
return result;
}//END Add
}//END Program
}//END Add_Function
public static int Add(int x, int y)
它在我调用Add()的行中给出了这个错误:
非静态字段、方法或属性“Add_Function.Program.Add(int,int)”需要对象引用
谁能给我解释一下为什么我有这个问题。这是因为C#的体系结构不同于C,而我称之为C的方式是错误的吗?谢谢。构建错误告诉您的是,您必须拥有
程序的实例或使添加静态。此代码给您一个错误,因为您的添加函数需要是静态的:
static public int Add(int x, int y)
在C#中,操作实例的函数(非静态)和不操作实例的函数(静态)之间存在区别。实例函数可以调用其他实例函数和静态函数,因为它们具有对实例的隐式引用。相反,静态函数只能调用静态函数,否则它们必须显式提供一个实例来调用非静态函数
由于publicstaticvoidmain(string[]args)
是静态的,因此它调用的所有函数也需要是静态的。您应该将添加
函数设置为静态的:
static public int Add(int x, int y)
{
int result = x + y;
return result;
} //END Add
static
表示函数不依赖于类实例。因此,您无需创建Program
class的类实例即可调用它
或者您应该在程序
类的实例中创建,然后在此实例上调用Add
。像这样:
Program prog = new Program();
prog.Add(5,10);
只需添加如下static
关键字,即可使您的Add
功能static
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Add_Function
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
//why can't I not use it this way?
c = Add(a, b);
Console.WriteLine("a + b = {0}", c);
}//END Main
public int Add(int x, int y)
{
int result = x + y;
return result;
}//END Add
}//END Program
}//END Add_Function
public static int Add(int x, int y)
因为您的函数是实例或非静态函数,所以应该首先创建一个对象
Program p=new Program();
p.Add(1,1)
注:在C#中,术语“函数”通常被术语“方法”取代。因为这个问题没有区别,所以我只使用“函数”一词
其他答案已经为您提供了解决问题的快速方法(只需使用Add
astatic
函数),但我想解释一下原因
C#与C有着根本不同的设计范式。这种范式被称为面向对象编程(OOP)。解释OOP和函数式编程之间的所有差异超出了这个问题的范围,但这里有一个适用于您的简短版本
用C编写程序时,您将创建一个添加两个数字的函数,该函数将独立存在,并且可以从任何地方调用。在C#中,大多数函数不是独立存在的;相反,它们存在于对象的上下文中。在示例代码中,只有类程序
的实例(对象)知道如何执行添加
。换句话说,您必须创建程序
的实例,然后要求程序
为您执行添加
人们给你的解决方案,使用static
关键字,围绕着这个设计。使用static
关键字有点像在说,“嘿,我定义的这个函数不需要任何上下文/状态,它可以被调用。”因为你的Add
函数非常简单,这很有意义。当您开始深入OOP时,您会发现您的函数变得更加复杂,并且依赖于了解它们的状态/上下文
我的建议是:拿起一本面向对象的书,准备好把你的大脑从函数式编程切换到面向对象编程。你要搭便车了 出现错误的原因是,如果要在main之前创建函数原型,则在main中使用add函数后定义add函数,并在其上方使用public int add(int x,int y)
或者您可以将整个Add
函数复制并粘贴到main之上,因为main是编译器开始执行的地方,所以在使用函数之前声明和定义函数是没有意义的,希望能有所帮助:D你必须使你的函数像这样是静态的
namespace Add_Function
{
class Program
{
public static void(string[] args)
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
//why can't I not use it this way?
c = Add(a, b);
Console.WriteLine("a + b = {0}", c);
}
public static int Add(int x, int y)
{
int result = x + y;
return result;
}
}
}
Program programinstance = new Program();
您可以执行Program.Add而不是Add
你也可以像这样做一个新的程序实例
namespace Add_Function
{
class Program
{
public static void(string[] args)
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
//why can't I not use it this way?
c = Add(a, b);
Console.WriteLine("a + b = {0}", c);
}
public static int Add(int x, int y)
{
int result = x + y;
return result;
}
}
}
Program programinstance = new Program();
注:在C#中,术语“函数”通常被术语“方法”取代。对于这个问题,没有区别,所以我只使用术语“函数”
那不是真的。您可以阅读(func类型+Lambda表达式),(匿名函数“使用委托类型”),(操作类型+Lambda表达式),(谓词类型+Lambda表达式)。等等…等等。。。
这会奏效的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
Func<int, int, int> funcAdd = (x, y) => x + y;
c=funcAdd.Invoke(a, b);
Console.WriteLine(Convert.ToString(c));
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
INTA;
int b;
INTC;
Console.WriteLine(“输入'a'的值:”);
a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(“输入'b':');
b=Convert.ToInt32(Console.ReadLine());
Func funcAdd=(x,y)=>x+y;
c=funcAdd.Invoke(a,b);
Console.WriteLine(Convert.ToString(c));
}
}
}
这与c#无关,而是关于OOP以及类如何与静态函数和成员函数一起工作。您试图从静态方法(没有实例)调用成员函数(需要对象的实例)。谢谢大家。这真的很有帮助。谢谢你,我一直都很感激能找到问题根源的答案。在C#中,函数和方法是两码事。例如,看看和。