C# this
Write a class Tin with an int field size and a constructor Tin(int size) that sets this.size = size;. In Pine, keep Main and build one tin with Tin t = new Tin(7);, then write t.size. Keep both types in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 7. The parameter and the field share the name size. The word this names the field on the object so the store reaches the right place. Using this when names collide is what this page teaches. Keep the starting public class named Pine.
Why the same name needs this
On the constructors page the parameter was n and the field was size, so size = n; was clear. Here both names are size. Inside the constructor, a bare size means the parameter. Writing size = size; copies the parameter onto itself and leaves the field at its default. The field on the object still needs a write. That write uses this.size on the left side.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Box b = new Box(4); leftover if that constructors call is still all you have in Main. Delete any Box 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 Tin. Inside the braces declare int size;. Then write the constructor: Tin(int size) { this.size = size; }. There is no Main in Tin on this page. Save the file before you run.
Read the assignment once more. The right side size is the value that arrived in the parameter — here 7. The left side this.size is the field on the object being built. After the line runs, that object’s size holds 7. If you drop this. and keep size = size;, the screen later shows 0 because the field never changed.
using System;
public class Tin
{
int size;
Tin(int size)
{
this.size = size;
}
}
Pine builds one tin
Pine keeps public static void Main(). Inside Main, write Tin t = new Tin(7); then Console.WriteLine(t.size);. The argument 7 becomes the constructor parameter size. The constructor stores it with this.size = size;. You do not need a separate t.size = 7; line on this page. You are not writing Box b = new Box(4); on this page. That was the constructors lesson.
using System;
public class Pine
{
public static void Main()
{
Tin t = new Tin(7);
Console.WriteLine(t.size);
}
}
public class Tin
{
int size;
Tin(int size)
{
this.size = size;
}
}
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 Tin(7) builds a Tin and calls Tin(int size) with the parameter equal to 7. The body writes this.size = size, so the object’s field becomes 7. Then Main writes t.size and the screen shows 7.
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 new Tin(7) plus the write in Pine, with this.size = size; in Tin. The screen shows 7. If the screen shows 4, a leftover new Box(4) path is still running. If the screen shows 0, the constructor body is probably size = size; without this. — put this. back on the left side.
If you add void before the constructor name, it becomes an ordinary method, not a constructor, and new Tin(7) will not run that body the way this page expects. The header must look like Tin(int size), not void Tin(int size).
this.size and size side by side
One short table keeps the two names straight while you are inside the constructor.
📊 this.size vs size
| Name | What it means in Tin(int size) |
|---|---|
size alone |
the parameter that entered the constructor |
this.size |
the field on the object being built |
this.size = size; |
store the parameter into the field |
size = size; |
does not update the field |
The table is only a reminder. The typed line you keep is this.size = size; inside the constructor, then Tin t = new Tin(7); and Console.WriteLine(t.size); in Pine. |
Compared with the constructors page
On the constructors page, Box(int n) { size = n; } worked because n and size were different words. That shape still works. This page deliberately uses the same word twice so you need this. A leftover Box demo with size = n; is a different lesson. For this page keep Tin and Pine and this.size = size;.
Writing Console.WriteLine(t); alone does not show a clean 7. Write Console.WriteLine(t.size); to see the stored number. The object reference alone is not the field value.
You can still choose a different parameter name on other days. This lesson chooses matching names on purpose. Do not mix n and this in the same Tin demo on this page. One clear path is enough: matching names, this on the field, write the field.
Two tins still stay separate
Each new Tin(...) runs the constructor again for a separate object. You can write Tin a = new Tin(2); and Tin b = new Tin(9);, then write both size fields. Each call uses this.size = size; on its own object. The two objects do not share one size slot. You do not need a second code box for that check — change the numbers in Pine once you already saw 7, or keep the single-tin demo as the file you save.
If both lines show 0, the constructor is still missing this on the left side. If the build cannot find Tin while compiling Pine, check that public class Tin sits in the same file.
What this leaves for instance methods
This page uses this only inside the constructor to store a field. A later page — instance methods — shows methods that run on an object and can read or change fields. Stay with the constructor store here. Do not invent extra methods on Tin for this lesson unless you already know them; the lesson goal is this.size = size when names match.
Do not nest Tin inside Pine’s braces. On this page the two classes sit as siblings in the file. If Pine still has Box b = new Box(4); from the constructors demo and this file has no matching Box class you intend to keep, remove that line. Keep Tin t = new Tin(7); and Console.WriteLine(t.size);. The build will not invent a Box for you when you are teaching Tin.
You can declare int size;, write Tin(int size) { this.size = size; }, and build with Tin t = new Tin(7); from Pine.Main. Run with dotnet run. The screen shows 7. The word this names the object’s field when the parameter uses the same name. You passed the number at build time, the constructor stored it with this, and the write proved the store. Think of this as a pointer to the object being built when a bare name would mean the parameter instead. Pine stays the door you open with dotnet run. The next page is instance methods, where methods on the object use fields after the object already exists.
Keep the constructor header without void. Keep this. on the field. Keep the write on t.size. Those three habits are the checkpoint for this page before you move on.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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