Loading...

C# Classes

A class is a named type you declare with class. Put public class Badge in the same teaching file as Pine, or keep it next to Pine as a second type. Inside Badge write a short static method show that writes ready. In Pine, keep Main and call Badge.show();. Run with dotnet run. The screen should show one line: ready. You are not counting down with recursion here. You are naming a second type and asking that type’s method to write. That second named type — a class that is not only the class that starts — is what this page teaches. Keep the starting public class named Pine.

The type sits beside the starter

Until the methods pages, almost every demo kept Main and the helper methods inside one public class named Pine. That still works. This page adds a second class on purpose. Badge is a type name. It does not need to be the program that starts. Pine is the program that starts. On this beginner shape both types can live in one file so dotnet run stays simple. What matters is the type name: Badge owns show, and Pine asks for it.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any down recursion leftover if that shape is still all you have in Main. Delete any mark leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Outside Pine’s braces — still in the same file — write public class Badge. Inside Badge, write static void show(). The body is one writing line: Console.WriteLine("ready");. Keep void on this page. The method does not return a number. It only writes a word. There is no Main in Badge on this page. That is intentional. A class can hold methods without being the starting class.
Save the file before you run. The first boxed program shows Badge alone so you can read the type cleanly. The second boxed program is the full kept shape with Pine and Badge together.

Badge type with show

csharp
using System;

public class Badge
{
    static void show()
    {
        Console.WriteLine("ready");
    }
}

Pine starts; Badge does the writing

Pine keeps public static void Main(). Inside Main, write one call: Badge.show();. That call names the type Badge, a dot, then the method show. You are not writing show(); alone inside Pine unless show also lives in Pine. On this page show belongs to Badge, so the call carries the type name.

Pine Main calls Badge.show

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Badge.show();
    }
}

public class Badge
{
    static void show()
    {
        Console.WriteLine("ready");
    }
}

Read the kept file from the top. using System; prepares short names. public class Pine is the starter. public static void Main() is where the runtime starts. The call Badge.show() enters the method in the Badge type. That method writes ready and returns. Pine then ends. The word on the screen came from Badge, not from a WriteLine written inside Pine itself. That split is the point: one class starts the program; another class owns the helper.

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 Badge.show(); in Pine plus the show method in Badge. The screen shows ready. If the screen shows 3, then 2, then 1, a leftover down(3) call is still running. If the screen shows the letters Badge, a writing line has quotes: Console.WriteLine("Badge");. You do not reinstall the .NET SDK for a screen that shows the wrong line.
Walk the run once more in order. C# starts at Pine.Main. The only statement there is Badge.show();. That call jumps into Badge’s show method. Inside show, WriteLine writes ready. Then show finishes, and Main has nothing left, so the program ends. You did not put Console.WriteLine("ready"); inside Pine on this page. You put it under Badge and asked for it by the type name.

Type beside role

Read the pairing once in a small table. Each class has a job on this page.

📊 type beside role

Type Role on this page
Badge type that holds show
Pine program that holds Main

A common mix-up is writing show(); inside Pine without the Badge. prefix when show only exists on Badge. The build then looks for show inside Pine and does not find it. Another mix-up is putting Main only on Badge and removing it from Pine while you still expect the old starter shape — keep Main on Pine for this lesson’s kept file. Leftover recursion demos can confuse Main. Clear Main down to Badge.show(); while you learn the two-type shape. You do not need down here.
Writing Console.WriteLine("Badge"); inside Pine writes the word Badge. It does not call the method on the type. Keep Badge.show(); when you want the writing line that lives in Badge.

What this page does not do yet

This page does not write new Badge(). Creating an object from a class is the next page. Here the call is Badge.show() — a static method on the type name itself. You also do not store fields on a badge yet. Fields wait for a later page in this module. The job now is only the named type beside the starter, and a call that uses that type name.
You can change the written word inside show to set and run again. The screen should follow the string you put in Badge. Pine stays the same call. That is another way to see the split: edit the type’s method, keep the starter’s call.
Do not nest Badge inside Pine’s braces. On this page the two classes sit as siblings in the file. Methods of Badge sit inside Badge. Main sits inside Pine.

You can declare public class Badge, put static void show() inside it, and call Badge.show(); from Pine.Main. Run with dotnet run. The screen shows the line written by show. A class is that named type. Think of Badge as a labeled shelf for helpers that do not need to start the program. Pine stays the door you open with dotnet run. The next page is objects, where you build a value from a class with new.

The screen holds Badge’s writing line

The file you keep writes ready when Main calls Badge.show();. The name Badge is in the call, and the letters Badge are not on the screen as a printed label unless you quoted them. Nesting Badge inside Pine 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

❓
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