Loading...

C# Objects

Write a class named Chip with a method beep that writes ok. The method is not static on this page. In Pine, keep Main and build one chip with Chip c = new Chip();, then call c.beep();. Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: ok. The word new builds one object from the class. The variable c holds that object. You call beep on c, not on the bare class name. That one built object — and the call that uses it — is what this page teaches. Keep the starting public class named Pine.

One object from the class

A class is still the named type. An object is one value built from that type. The line new Chip() asks C# to build that value. You store it in a variable whose type is Chip, then you use the variable to reach methods that belong to each object.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Badge.show(); leftover if that static call is still all you have in Main. Delete any down recursion 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 Chip. Inside Chip, write void beep() — no static word on this header. The body is one writing line: Console.WriteLine("ok");. Keep void. The method does not return a number. There is no Main in Chip on this page. Save the file before you run.
The first boxed program shows Chip alone so you can read the type cleanly. The later boxed programs are the kept shapes with Pine building one chip, then two.

Chip with beep

csharp
using System;

public class Chip
{
    void beep()
    {
        Console.WriteLine("ok");
    }
}

Pine builds the chip, then calls beep

Pine keeps public static void Main(). Inside Main, write two lines: first Chip c = new Chip();, then c.beep();. The first line declares a variable c of type Chip and fills it with a new object. The second line calls beep on that object. The dot sits after the variable name, not after the class name alone. You are not writing Badge.show(); on this page. That was the static call from the classes lesson.

Pine builds one chip

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Chip c = new Chip();
        c.beep();
    }
}

public class Chip
{
    void beep()
    {
        Console.WriteLine("ok");
    }
}

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 expression new Chip() builds one object. That object is stored in c. The call c.beep() enters beep for that object. The method writes ok and returns. Pine then ends. The word on the screen came from the object’s method, after new created the object.
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 Chip c = new Chip(); plus c.beep(); in Pine, with beep in Chip. The screen shows ok. If the screen shows ready, a leftover Badge.show(); call is still running. If the screen shows the letters Chip, a writing line has quotes: Console.WriteLine("Chip");. 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 first statement builds one object with new Chip() and stores it in c. The second statement calls c.beep(). Inside beep, WriteLine writes ok. Then beep finishes, and Main has nothing left, so the program ends. You did not put Console.WriteLine("ok"); inside Pine on this page. You put it under Chip and asked for it through the object variable.
Think of the class as the blueprint in the file and the object as one built piece in memory for this run. Each new Chip() builds another piece. The variable names the piece so later lines can reach it. Without that name, the built piece has no handle for beep on this page.

Why the call uses c, not Chip alone

On the classes page, a static method could be called as Badge.show() with the type name. Here beep has no static word. C# expects an object first. Writing Chip.beep(); without an object does not match this beginner shape. Build with new, keep the result in a variable, then call through that variable.
If you write only new Chip(); and never store it, you still built an object, but you have no name left to call beep on. On this page keep the variable: Chip c = new Chip(); then c.beep();. The variable is how you reach the object after new finishes.
A leftover static demo can confuse Main. A file that still calls Badge.show(); is the previous lesson’s shape. For this page keep Chip and Pine and the new line. You do not need Badge here.

Two objects, two beeps

You can build more than one object from the same class. Each new Chip() makes a separate object. Each object can run beep on its own. The class text is shared; the objects are not the same value.

Pine builds two chips

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Chip a = new Chip();
        Chip b = new Chip();
        a.beep();
        b.beep();
    }
}

public class Chip
{
    void beep()
    {
        Console.WriteLine("ok");
    }
}

Run again with dotnet run. The screen should show two lines: ok, then ok. The first line came from a. The second came from b. Both used the same beep method text in Chip, but each call ran on a different object built with new.
If the screen shows only one ok, check whether both calls are present and whether both variables were built with new. Writing Chip b = a; does not build a second object; it makes b refer to the same object as a. That idea waits for later pages. On this page use two new Chip() expressions when you want two objects.
You can rename the variable from c to unit if you like: Chip unit = new Chip(); then unit.beep();. The screen stays ok when the method body still writes that word. The variable name is yours; the type name Chip and the new Chip() expression must still agree.

What this page leaves for fields

This page does not store a number or a word inside the chip yet. Fields — names that hold data on each object — are the next page. Here the object exists after new, and the instance method beep only writes. That is enough to see the split between the class as a type and one object as a built value.
Do not nest Chip inside Pine’s braces. On this page the two classes sit as siblings in the file. Methods of Chip sit inside Chip. Main sits inside Pine. You can change the written word inside beep to ping and run again. The screen should follow the string you put in Chip. Pine stays the same new and call shape.
If the build cannot find Chip while compiling Pine, check that public class Chip sits in the same file and that the class line really says Chip. If the run writes nothing, confirm Main still calls c.beep(); after the new line, and that beep still contains the WriteLine.

You can write Chip with void beep(), build one object with Chip c = new Chip();, and call c.beep(); from Pine.Main. Run with dotnet run. The screen shows ok. A second new Chip() builds another object that can beep on its own. Think of Chip as the blueprint and each new Chip() as one built piece you can name. Pine stays the door you open with dotnet run. The next page is fields, where each object begins to hold its own stored data.

🧠 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