Classes and objects
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A class is a blueprint and a type: it lists the fields every object has and the methods that work on them. Follow a small bank account through memory: new makes an object on the heap with its fields at their defaults, the constructor gives it a valid start, and the variable keeps only a reference. Copy that variable and you copy the reference, so two names share one object, and the program prints 75.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01Right after new Account("Ada") gets its memory, before the constructor body runs, what do the fields hold?
new sets every field to its type's default value first (null for a string, 0 for an int). Only then does the constructor run and set Owner to "Ada".
02After var b = a; and b.Deposit(25);, what does a.Balance show (a's Balance was 50)?
Account is a class, so a holds a reference. var b = a copies that reference, not the object. Both names lead to object #1, so the deposit through b shows up through a.
03What does the variable a actually store?
A variable of a class type holds a reference to an object on the managed heap. The object itself stays on the heap; following the reference with a dot (a.Balance) reaches it.
04The program makes two Account objects. How many copies of the Deposit method's code are there?
Methods are written once in the class. Each object only has its own fields. When you call a.Deposit(50), the method runs with this set to the object a refers to.
05Which statement makes a new object on the heap?
Only new creates an object (and runs its constructor). Assigning a variable copies a reference, and calling a method changes an object that already exists.
المسرد
- Class
- A blueprint and a type: it declares the fields every object of that type has and the methods (and constructors) that work on them.
- Object (instance)
- One concrete thing made from a class with new. Each object has its own copy of the class's fields.
- Field
- A variable that belongs to each object, such as Owner or Balance.
- Constructor
- A special method with the class's name and no return type. It runs once when new creates an object and gives the object a valid start.
- new
- The operator that creates an object: it gets memory on the heap, sets every field to its default value, then runs a constructor.
- Reference
- What a variable of a class type holds: a way to find the object on the heap. Copying the variable copies the reference, not the object.
- this
- Inside a method or constructor, the object the code is running on. Balance there means this.Balance.
- Default value
- The value a field has before anything sets it: 0 for numbers, false for bool, null for reference types such as string.
الملاحظات والمصادر
- The figure writes a reference as #1 or #2. A real reference is the object's address in memory, which C# never shows you; you only follow it with the dot, as in a.Balance.
- Here a, b and c are local variables of the top-level statements, so they normally sit in the method's stack frame. Where a local lives is a runtime detail; what matters is that it holds a reference, not the object.
- The fields are public to keep the first class short. Real classes usually keep fields private and expose properties instead, so the class can protect its own data (for example, refuse a negative deposit).
- Before the constructor body runs, the new object's fields hold their default values (null for string, 0 for int), or their initial values if the field declaration gives one.
- New .NET projects enable nullable reference types, so the compiler checks that the constructor gives the non-nullable string Owner a value. This one does, so the build has no warning.
- The class lives in Account.cs and the statements in Program.cs, in the same project. With dotnet run, the program prints 75.
- Microsoft Learn: Introduction to classes
- Microsoft Learn: Objects - create instances of types
- Microsoft Learn: new operator
- Microsoft Learn: Constructors
- Microsoft Learn: Fields
- Microsoft Learn: Default values of C# types
- Microsoft Learn: The this keyword
- Microsoft Learn: Reference types
- Microsoft Learn: Expression-bodied members
- Microsoft Learn: Access modifiers
- Microsoft Learn: Fundamentals of garbage collection
اختبار يتاح بعد الخطوة الأخيرة.