简体   繁体   中英

What's the point of C# Method Parameters?

I'm new to coding, and I'm curious as to why Method Parameters are useful. What's the point to them when I can create a variable within the method which should execute when I call on it? What's the point of giving it that parameter?

In your example, let's say you wanted to say hello to more than just Mike. You could do this:

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

Let's take it a step further. You can create a collection of names, and say hi to any number of people

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

As you can see, the SayHi() function is reuseable because you can call it any number of times.

And for one more step further, let's say that later you realized that you didn't want to say "Hello " + name , you wanted to say, "Hello, " + name + ". How are you?" then you'd only have to change the code in one place and all the times it was used will be affected.

Adding a parameter is a way to say "This piece of reusable code is only useful as long as it has this value to use". In your sample code, the value that SayHi needs is a name because it needs to know whom to say hi to.

Parameters can be used for a couple different things. The most practical use is to avoid repetition as Phong said in their answer. For example, if I want to print a single line of red text to the console, I could do this:

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();

OR I could create a function with an input parameter for the text to display red like this:

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!");

As you can see, we turned this task from a 3 line task, to a 1 line task, which would not be possible without input parameters. This will be very useful in case we need to write in red anywhere else in the program.

Obviously this is a pretty simple example, and in more complex programs this could easily save you 100+ lines.

Another use is for organization. Let's say you have a program that takes user input and processes it in some way. Using functions with input parameters, you can put the user input gathering in one .cs file, and the processing in another.

Parameters are also really useful in constructors, which if you haven't gotten to yet, are just functions that are called when a new instance of an object is created.

What's the point of giving it that parameter?

It helps you Reuse some actions with the same steps. Don't repeat yourself

As you can see, You just pass the param then inside the method will help you dosomething ("Hello" + passedparameter)

You should read the fundamentals of OOP (object oriented programming). This will help you understand basic principles like method overloading, overriding and inheritance which are commonly used. Instead of writing multiple single-line statements you can create functions that are structured, cleanly written code. Also, following different design patterns, it allows us to give functions the ability to handle single responsibilities and make everything cleaner imo!

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

C# is Object-oriented programming language


Object-oriented programming, or OOP, is an approach to problem-solving where all computations are carried out using objects. An object is a component of a program that knows how to perform certain actions and how to interact with other elements of the program.

Once you have created objects, you want them to be able to do something. This is where methods come in. A method in object-oriented programming is a procedure associated with a class. A method defines the behavior of the objects that are created from the class. Another way to say this is that a method is an action that an object is able to perform. The association between method and class is called binding .

Consider the example of an object of the type ' person ,' created using the person class. Method s 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.

How does this differ from a method? A function is independent and not associated with a class. You can use this function anywhere in your code, and you don't need to have an object to use it.

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

If you create a variable inside the method you can only assign it with one value at the time of code writing, but if you pass it as a parameter you can dynamically assign whatever value you want when you call the method later, even a value you don't know at the time of code writing, this value can be assigned on the run time.

Also, methods are used to encapsulate some logic, let say you want a method to calculate the area of a circle which is π r2 , you know the value of the PI π because it is a constant which is almost 3.14, but you don't know the radius value r because it varies from circle to another so you need to pass it to the method as parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM