Loading...

C# Parameters

Write a method named greet that takes one word in. Inside the parentheses write string name. In the body write hi, a space, and then name with Console.WriteLine("hi " + name);. From Main call greet("lena");. The screen should show one line: hi lena. The letters lena are not hard-coded inside the method. They arrive through the call and sit in the name name for that pass. That hand-off is what this page teaches. Keep the public class named Pine.

A name inside the parentheses

On the methods page the parentheses after ring stayed empty. The writing line was always ding. Here the parentheses hold a typed name so each call can send a different value. That typed name is a parameter. The value you write in the call is the argument for that pass.
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any ring(); demo with empty parentheses if that is all Main still holds and you are ready for this page’s shape. Delete any foreach leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Main, still inside the class braces, the header starts with static void on this beginner shape. Between the parentheses sit string and then name — the type first, then the parameter name. C# writes the type string with a lowercase s on this shape. The body uses name without quotes around it, joined to the fixed text "hi " with a plus. Quotes around "hi " mark the fixed letters. The bare name marks the value that arrived from the call. Save the file before you run. The full first program is in the next box.

The file you keep

This is the version you run first. Main holds the call with a value inside the parentheses. greet holds the writing line that uses that value.

greet takes a string

csharp
using System;

public class Pine
{
    public static void Main()
    {
        greet("lena");
    }

    static void greet(string name)
    {
        Console.WriteLine("hi " + name);
    }
}

Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. The call greet("lena"); sends the text into the parameter. static void greet(string name) holds Console.WriteLine("hi " + name);. One line on the screen: hi lena.

Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep greet("lena"); plus the greet method above. The screen shows hi lena. If the screen shows only ding, an old ring(); call is still running. If the screen shows the letters name, a writing line has quotes around that word: Console.WriteLine("name");. You do not reinstall the .NET SDK for a screen that shows the wrong line.

Walk one full run in order. C# starts at Main. The call greet("lena"); sends the text lena into the parameter name. Inside greet, WriteLine builds hi, a space, and then that value, so the screen shows hi lena. Then greet finishes and Main ends. You did not write Console.WriteLine("hi lena"); inside Main. You wrote the fixed part once under greet and passed lena from the call.
The parameter is empty until the call fills it. On the first call, name holds lena for that pass only. On a later call with "sam", the same parameter name holds a different value. You are not creating a second method for the second word; you are sending a new argument into the same header.

Same method, different words

Change only Main so it calls greet twice with two different arguments. Leave the greet method itself alone.

greet with two arguments

csharp
using System;

public class Pine
{
    public static void Main()
    {
        greet("lena");
        greet("sam");
    }

    static void greet(string name)
    {
        Console.WriteLine("hi " + name);
    }
}

This is the second file. It teaches the same method with two calls. Run dotnet run. The screen should show two lines: hi lena, then hi sam. The first call fills name with lena. The second call fills name with sam. You did not copy the WriteLine under greet. You changed only the values inside the calls.
If you call greet() with empty parentheses while the header still says string name, the build writes an error — the call must supply a string. If you leave an old ring(); with empty parentheses from the methods page in the same Main, that call does not match greet, and a leftover ring method may still write ding beside your hi lines. Clear Main to the greet calls this lesson needs. After you have seen two lines, put the kept one-call file back if that is the shape you want to save: one greet("lena"); in Main.

A whole number as the value

Parameters are not only for text. Write a second method that takes an int named n and writes it. Call it from Main with whole numbers such as show(9); and show(4);.

show takes an int

csharp
using System;

public class Pine
{
    public static void Main()
    {
        show(9);
        show(4);
    }

    static void show(int n)
    {
        Console.WriteLine(n);
    }
}

This is the third file. It is not the file you keep for the greet shape. Run dotnet run. The screen should show two lines: 9, then 4. No quotes wrap 9 or 4 in the call — they are int values, not text. Inside show, Console.WriteLine(n) writes the number that arrived. After you have seen those two lines, put the greet demo back. Both versions are valid C#. They teach the same idea: the call sends a value, the parameter holds it for the body.
Read the int run once more. The first call sets n to 9 and writes 9. The second call sets n to 4 and writes 4. The name n is declared once in the header as int n. Each call gives it a new value for that pass. Passing a quoted word into show, such as show("nine");, does not match int on this shape — the build writes an error. Passing a bare number into greet, such as greet(3);, does not match string — the build writes an error again. Match the argument type to the parameter type.

A common mix-up is writing the parameter’s name as text: Console.WriteLine("name");. That writes the letters n-a-m-e. It does not write lena. Keep Console.WriteLine("hi " + name) without quotes around name when you want the argument’s letters on the screen. Another mix-up is declaring the parameter in Main instead of in the method header. The typed name belongs inside greet(...) or show(...), not inside Main’s parentheses on this page. Empty () from the ring page is not enough once the header asks for string name.

void still means nothing comes back through the call. You can put string name or int n inside a method’s parentheses, pass a matching value in the call, and write that value from the body. Two calls with two arguments write two different lines without copying the method. The next page is return, where a method hands a value back to Main instead of only writing inside the body.

The screen holds the argument

The file you keep writes hi lena when Main calls greet("lena");. The name name is in the writing line without quotes around it. Quotes around name write letters, not the argument. Empty () on the call do not match a header that asks for string name.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top