Loading...

C# Fields

Put an int field named fill inside a class Cup. In Pine, keep Main and build one cup with Cup c = new Cup();, store a number with c.fill = 3;, then write that field with Console.WriteLine(c.fill);. Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 3. The field holds data on that one object. You reach it through the variable that names the object. Storing and reading that value on the object is what this page teaches. Keep the starting public class named Pine.

A place for data on the object

On the objects page, beep only wrote. It did not keep a number for later. A field is a name declared inside the class that each object can hold its own value in. After new builds the object, you assign the field through the object variable, then you can write it to the screen.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any c.beep(); leftover if that Chip call is still all you have in Main. Delete any Chip class 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 Cup. Inside the class braces, write one field line: int fill;. There is no method on this first box. There is no Main in Cup on this page. Save the file before you run.
The first boxed program shows Cup alone so you can read the field cleanly. The second boxed program is the kept shape with Pine setting and writing fill.

Cup with fill field

csharp
using System;

public class Cup
{
    int fill;
}

Pine assigns fill, then writes it

Pine keeps public static void Main(). Inside Main, write three lines in order: Cup c = new Cup();, then c.fill = 3;, then Console.WriteLine(c.fill);. The first line builds the object. The second stores 3 in that object’s fill. The third writes the stored number. You are not calling c.beep(); on this page. That was the objects lesson.

Pine sets and prints fill

csharp
using System;

public class Pine
{
    public static void Main()
    {
        Cup c = new Cup();
        c.fill = 3;
        Console.WriteLine(c.fill);
    }
}

public class Cup
{
    int fill;
}

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. new Cup() builds one object and stores it in c. The assignment c.fill = 3 writes 3 into that object’s field. WriteLine(c.fill) reads the same field and writes 3. The number on the screen is the value stored on the object, not a hard-coded print of a string like "ok".
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 the three lines above in Pine, with the fill field in Cup. The screen shows 3. If the screen shows ok, a leftover c.beep(); call is still running. If the screen shows the letters Cup, a writing line has quotes: Console.WriteLine("Cup");. 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 Cup() and stores it in c. The second stores 3 in c.fill. The third writes that field. Then Main ends. The field name fill is declared once in the class. Each object built from Cup gets its own slot for that name. When you write c.fill, you are naming the object first, then the slot on that object. That order — object, then field — is the shape this page keeps.

Class, object, and field side by side

Read the three names once in a small table. The field line always goes through an object on this page.

📊 class object field

Name What it is on this page
Cup the class / type beside Pine
c one object built with new Cup()
c.fill the int stored on that object

Writing Cup.fill = 3; without an object does not match this beginner shape. The field belongs to each object after new. Build first, then assign through the variable. Writing only new Cup(); without storing the result also leaves you no name for fill. Keep Cup c = new Cup(); then c.fill = 3;.
A leftover objects demo can confuse Main. A file that still calls c.beep(); with a Chip class is the previous lesson. For this page keep Cup and Pine and the fill field. You do not need beep here.
If you write Console.WriteLine(c); by itself, the screen will not show a clean 3. It prints a default object text. To see the stored number, write Console.WriteLine(c.fill);.

Two cups keep separate fills

You can build two objects from Cup. Each has its own fill. Changing one does not change the other.
In Pine, you can write Cup a = new Cup(); and Cup b = new Cup();, then a.fill = 2; and b.fill = 9;, then write a.fill and b.fill. After dotnet run, the screen should show 2 on one line and 9 on the next. Both objects share the class text that declares fill, but each object holds its own number.
If both writes show the same value when you meant two different numbers, check that you assigned both fields and that you did not write Cup b = a;. That line makes b refer to the same object as a, so both names share one fill. On this page use two new Cup() expressions when you want two separate fills.
You do not need a third boxed program for that practice. Edit Pine carefully and run again. Then you can restore the single-cup box if you want the quiz’s first shape.

What constructors will add later

This page assigns the field after new with a separate line: c.fill = 3;. A later page — constructors — will show another way to set starting values when the object is built. Stay with the plain field and the assignment after new here. Do not invent a constructor method on this page.
Default values exist for fields you never assign: an int field starts at 0. If you write c.fill before any assignment, the screen can show 0. This page still teaches the assignment you write yourself, so keep c.fill = 3; before the write in the main demo.
Do not nest Cup inside Pine’s braces. On this page the two classes sit as siblings in the file. The field sits inside Cup. Main sits inside Pine. If the build cannot find Cup while compiling Pine, check that public class Cup sits in the same file. If the screen shows 0 when you expected 3, the assignment line may be missing or may run after the write — put c.fill = 3; above Console.WriteLine(c.fill);.

You can declare int fill; inside Cup, build Cup c = new Cup();, assign c.fill = 3;, and write c.fill from Pine.Main. Run with dotnet run. The screen shows 3. Two objects can hold two different fills. Think of fill as a labeled slot on each cup you build with new. Pine stays the door you open with dotnet run. The next page is constructors, where you begin setting values at the moment new builds the object.

🧠 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