C#方法参数有什么意义?

What's the point of C# Method Parameters?

我是编码新手,我很好奇方法参数为何有用。当我可以在我调用它时应该执行的方法中创建一个变量时,对他们来说有什么意义呢?给它这个参数有什么意义?

What's the point of giving it that parameter?

它可以帮助您 Reuse 使用相同的步骤完成一些操作。 Don't repeat yourself

如您所见,您只需传递参数,然后在方法内部将帮助您做一些事情("Hello" + 传递的参数)

在您的示例中,假设您不仅仅想向 Mike 打招呼。 你可以这样做:

SayHi("Mike");
SayHi("Amy");
SayHi("Sam");

让我们更进一步。您可以创建一个名字集合,并向任意数量的人打招呼

List<string> people = new List<string> { "Mike", "Amy", "Sam", "John", "Mindy" };
foreach (string person in people) 
{
    SayHi(person);
}

如您所见,SayHi() 函数是可重复使用的,因为您可以调用它任意多次。

更进一步,假设后来你意识到你不想说 "Hello " + name,你想说 "Hello, " + name + ". How are you?" 那么你只需要更改一个地方的代码,它的所有使用次数都会受到影响。

加个参数就是说"This piece of reusable code is only useful as long as it has this value to use"。在您的示例代码中,SayHi 需要的值是一个名称,因为它需要知道向谁打招呼。

您应该阅读 OOP(面向对象编程)的基础知识。这将帮助您理解常用的方法重载、覆盖和继承等基本原理。无需编写多个单行语句,您可以创建结构化、编写清晰的代码的函数。此外,遵循不同的设计模式,它使我们能够赋予函数处理单一职责的能力,并使一切变得更干净!

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/object-oriented-programming

C# is Object-oriented programming language


面向对象编程或 OOP 是一种解决问题的方法,其中所有计算都使用对象进行。对象是程序的一个组件,它知道如何执行某些操作以及如何与程序的其他元素交互。

创建对象后,您希望它们能够执行某些操作。这就是方法的用武之地。面向对象编程中的方法是与 class 关联的过程。方法定义了从 class 创建的对象的行为。另一种说法是,方法是对象能够执行的操作。方法和class之间的关联称为绑定

Consider the example of an object of the type 'person,' created using the person class. Methods associated with this class could consist of things like walking and driving. Methods are sometimes confused with functions, but they are distinct. A function is a combination of instructions that are combined to achieve some result. A function typically requires some input (called arguments/parameters) and returns some results. For example, consider the example of driving a car. To determine the mileage, you need to perform a calculation using the distance driven and the amount of fuel used. You could write a function to do this calculation. The arguments going into the function would be distance and fuel consumption, and the result would be mileage. Anytime you want to determine the mileage, you simply call the function to perform the calculation.

这与方法有何不同?函数是独立的,不与 class 相关联。您可以在代码中的任何地方使用这个函数,而且您不需要有一个对象来使用它。

参数可用于多种不同的用途。最实际的用途是避免 Phong 在他们的回答中所说的重复。例如,如果我想在控制台上打印一行红色文本,我可以这样做:

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("This is some red text");
Console.ResetColor();

Console.WriteLine("This is some boring white text");

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("And this is some more red text!");
Console.ResetColor();

或者我可以创建一个带有输入参数的函数,使文本显示为红色,如下所示:

static void WriteRed(string output){
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine(output);
    Console.ResetColor();
}

WriteRed("This is some red text");
Console.WriteLine("This is some boring white text");
WriteRed("And this is some more red text!");

如您所见,我们将此任务从 3 行任务变成了 1 行任务,如果没有输入参数,这是不可能的。如果我们需要在程序的其他地方用红色书写,这将非常有用。

显然这是一个非常简单的示例,在更复杂的程序中,这可以轻松地为您节省 100 多行代码。

另一个用途是组织。假设您有一个程序接受用户输入并以某种方式处理它。使用带输入参数的函数,您可以将用户输入收集放在一个 .cs 文件中,将处理放在另一个文件中。

参数在构造函数中也非常有用,如果您还没有接触过,它只是在创建对象的新实例时调用的函数。

What's the point to them when I can create a variable within the method which should execute when I call on it

如果你在方法内部创建一个变量,你只能在写代码的时候给它赋一个值,但是如果你把它作为参数传递,你可以在以后调用方法时动态地分配你想要的任何值,即使是你在写代码时不知道的值,这个值也可以在运行时赋值。

另外,方法是用来封装一些逻辑的,假设你想要一个方法来计算一个圆的面积π r2,你知道圆周率的值π,因为它是一个常数,几乎是 3.14,但您不知道半径值 r,因为它因圆而异,因此您需要将其作为参数传递给方法。