Loading...

C# Scope

Write a method named mark with one parameter: static void mark(int n). Inside the braces write that parameter with Console.WriteLine(n);. From Main call mark(4);. The screen should show one line: 4. The name n exists only inside mark. If you try to write n from Main, the build refuses. That boundary — which names a method can see — is what this page teaches. Keep the public class named Pine.

Names that stay inside a method

On the overloading page, several paint methods shared a name but kept separate parameter lists. Here the focus is smaller: one method, one parameter name, and where that name is allowed to appear. A parameter is a name that belongs to the method that declares it. Outside that method’s braces, the name is gone.
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any paint demo leftover if that shape is still all you have in Main. Delete any twice leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Main, still inside the class braces, write static void mark(int n). The body has one writing line: Console.WriteLine(n);. Keep void on this page. You are watching visibility, not return values. 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. mark holds the writing line that uses n.

mark writes its parameter

csharp
using System;

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

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

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 mark(4) sends 4 into the parameter n. Inside mark, Console.WriteLine(n) writes 4. When mark finishes, the name n is finished too. Main never held a name n. The number 4 crossed the call as a value. The name n did not move into Main. One line on the screen: 4.

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 mark(4); plus the mark method above. The screen shows 4. If the screen shows ok, a leftover paint("ok") call is still running. If the screen shows the letter n, a writing line has quotes: Console.WriteLine("n");. 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 mark(4) sends 4 into the parameter n. Inside mark, WriteLine(n) writes 4. When mark finishes, the name n is finished too. Main never held a name n. The number 4 crossed the call as a value. The name n did not move into Main.
If you add Console.WriteLine(n); inside Main after the call, the build reports that it cannot find the name n. That error is the lesson working: n was never declared in Main. Fix by removing that line from Main, or by declaring a different name in Main if you need a local there. Do not expect the parameter from mark to appear in Main by itself.

A local name next to the parameter

Inside mark you can also declare a new name that is not a parameter. That local name lives only between the braces of mark. A common beginner hope is that a name declared in Main is automatically visible inside every method in the class. It is not, on this shape. If Main has int room = 4; and mark tries to write room without receiving it, the build cannot find room inside mark. The fix is to pass the value: mark(room); with mark still declared as mark(int n).

Pass a Main local into mark

Declare int room = 4; inside Main. Call mark(room);. Inside mark, keep writing n. The screen still shows 4. The local name in Main is room. The parameter name in mark is n. They are different names in different places. The value 4 is what travels.

pass room into mark

csharp
using System;

public class Pine
{
    public static void Main()
    {
        int room = 4;
        mark(room);
    }

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

This is the second file. It teaches the same visibility with a local in Main. Run dotnet run. You still see one line: 4. If you change room to 9 and leave the method body alone, the screen shows 9. The method still writes whatever value arrived in n. Main can write room as well if you add Console.WriteLine(room); in Main — that name belongs there. Main still cannot write n. After you have seen the room shape, put the kept one-call file back if that is the shape you want to save: mark(4); in Main.
If mark declares its own local — for example int tag = 1; inside the braces — that tag is invisible to Main. Trying Console.WriteLine(tag); in Main fails for the same reason writing n from Main fails. The braces of mark are a wall for names declared inside them.

What the wall protects

Read the split in plain words. Names declared in Main stay in Main unless you pass their values into a call. Names declared in mark — parameters and locals — stay in mark. The call carries values, not the names themselves. Overloading from the previous page is a different skill: several methods with one shared method name. Scope is about where a variable or parameter name is allowed to appear in the source.
A leftover paint demo can sit in the same file and confuse the screen if Main still calls paint("ok"). Clear Main down to the mark call you are testing. Do not nest mark inside Main’s braces. Methods sit inside the class, next to Main.
Writing Console.WriteLine("n"); in Main writes the letter n in quotes. It does not write the parameter. Keep Console.WriteLine(n) inside mark when you want the value that arrived through the call. Writing mark(); with empty parentheses fails when the header still says mark(int n) — the call must send an int, whether a literal like 4 or a local like room.

You can write mark(int n), write n inside that method, and keep n out of Main. You can declare room in Main and pass room into mark. The screen shows the value that crossed the call. Names do not leak through the braces on their own. The next page is recursion, where a method calls itself with a smaller argument until a stop condition ends the chain.

The screen holds the value that crossed

The file you keep writes 4 when Main calls mark(4);. The name n is in the writing line inside mark, and the letter n is not on the screen as a printed label unless you quoted it. Nesting mark inside Main is not the shape this lesson builds.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

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

❓
6
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