C#创建和使用函数 [英] C# Creating and using Functions

查看:165
本文介绍了C#创建和使用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关C#编程的帮助;我是新手,来自C背景.我有一个这样的控制台应用程序:

I need help with C# programming; I am new to it and I come from C background. I have a Console Application like this:

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

它在我称为Add()的行上给了我这个错误:

It gives me this error on the line that I call Add():

非静态字段,方法或属性'Add_Function.Program.Add(int,int)'需要对象引用

An object reference is required for the non-static field, method, or property 'Add_Function.Program.Add(int, int)'

任何人都可以向我解释为什么我有这个问题.这是因为C#的体系结构与C不同,并且我称呼它的方式是错误的吗?谢谢.

Can anyone please explain to me why I have this problem. Is this because the architecture of C# is different than C, and the way I call it is wrong? Thanks.

推荐答案

注意:在C#中,术语函数"通常由术语方法"代替.出于这个问题,没有区别,所以我只使用术语功能".

其他答案已经为您提供了一种快速解决问题的方法(只需将Add设置为static函数),但是我想解释原因.

The other answers have already given you a quick way to fix your problem (just make Add a static function), but I'd like to explain why.

C#与C具有根本不同的设计范例.该范例称为面向对象编程(OOP).解释OOP和函数式编程之间的所有差异超出了此问题的范围,但这是适用于您的简短版本.

C# has a fundamentally different design paradigm than C. That paradigm is called object-oriented programming (OOP). Explaining all the differences between OOP and functional programming is beyond the scope of this question, but here's the short version as it applies to you.

使用C编写程序,您将创建一个将两个数字相加的函数,并且该函数将独立存在并且可以在任何地方调用.在C#中,大多数函数不是独立存在的.相反,它们存在于对象的上下文中.在您的示例代码中,只有类Program的实例(对象)知道如何执行Add.换句话说,您必须创建Program的实例,然后要求Program为您执行Add.

Writing your program in C, you would have created a function that adds two numbers, and that function would exist independently and be callable from anywhere. In C# most functions don't exist independently; instead, they exist in the context of an object. In your example code, only an instance (an object) of the class Program knows how to perform Add. Said another way, you have to create an instance of Program, and then ask Program to perform an Add for you.

人们使用static关键字为您提供的解决方案围绕该设计.使用static关键字就像是在说:嘿,我正在定义的函数不需要任何上下文/状态,只需调用它即可."由于您的Add函数非常简单,因此很有意义.随着您开始更深入地研究OOP,您会发现您的功能变得更加复杂,并且依赖于了解它们的状态/上下文.

The solutions that people gave you, using the static keyword, route around that design. Using the static keyword is kind of like saying, "Hey, this function I'm defining doesn't need any context/state, it can just be called." Since your Add function is very simple, this makes sense. As you start diving deeper into OOP, you're going to find that your functions get more complicated and rely on knowing their state/context.

我的建议:拿起一本OOP书籍,准备好将您的大脑从功能编程转换为OOP编程.您正在兜风.

My advice: Pick up an OOP book and get ready to switch your brain from functional programming to OOP programming. You're in for a ride.

这篇关于C#创建和使用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆