Value types and reference types
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Every C# type is a value type or a reference type, and the difference shows up the moment you assign one variable to another. Follow an eight-line program through memory: copying an int copies the value, so the two variables stay independent; copying a reference to a Box object copies only the arrow, so two variables share one object and a change made through one is seen through the other. The program prints 5 9 9.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01After int a = 5; int b = a; b = 9; what is the value of a?
int is a value type, so int b = a; copies the value 5 into b's own slot. Changing b later does not touch a.
02Box is a class. After var p = new Box { N = 5 }; var q = p; how many Box objects exist?
Only new makes an object. var q = p; copies the reference, so p and q are two arrows to the same single object.
03Continuing: q.N = 9; Console.WriteLine(p.N); prints what?
q.N follows q's reference to the one Box and sets its N to 9. p refers to the same object, so p.N is 9 too.
04Which of these is a reference type?
string is a class (System.String), so it is a reference type. int, bool and double are value types. Strings can never be changed, though, so copying a string variable still behaves like copying a value.
05What does a variable of a reference type actually hold?
A reference-type variable holds a reference to an object (or null, meaning no object). The object itself lives on the heap, which the runtime manages.
المسرد
- Value type
- A type whose variables hold the value itself, such as int, double, bool, char or any struct. Assigning one copies the whole value.
- Reference type
- A type whose variables hold a reference to an object, such as any class, string or an array. Assigning one copies only the reference.
- Reference
- What a reference-type variable holds: the runtime's way to find one object on the heap. Drawn as an arrow. It can also be null: no object.
- Object
- One instance of a class, made with new. Several variables can refer to the same object.
- Stack frame
- The memory a method uses for its local variables while it runs. Here it holds a, b, p and q.
- Heap
- Memory for objects, managed by .NET. The garbage collector frees objects that nothing refers to any more.
- Field
- A variable that belongs to an object, such as N in class Box { public int N; }.
- Interpolated string
- A string starting with $ in which each {expression} is replaced by its value, such as $"{a} {b}".
الملاحظات والمصادر
- The C# language only promises the copy rules. Where data lives is the runtime's choice: here the four locals sit in the method's stack frame (or in CPU registers), and the Box lives on the managed heap. A value that is part of an object, like N, lives inside that object.
- A reference is not the object and not something you can do arithmetic on. The runtime uses it to find the object; the garbage collector may even move objects and update every reference for you.
- string is a reference type, but string objects can never be changed, so copying a string variable always behaves like copying a value.
- The lists of examples are not complete. Enums and record structs are value types too; interfaces, delegates and records (record class) are reference types too.
- A reference-type variable can also hold null: no arrow at all. Using q.N when q is null throws a NullReferenceException.
- The program is real C#. Put the eight lines in Program.cs of a new console project and dotnet run prints 5 9 9.
- Microsoft Learn: Value types (C# reference)
- Microsoft Learn: Reference types (C# reference)
- Microsoft Learn: The C# type system
- Microsoft Learn: Classes (reference types)
- Microsoft Learn: Structure types (value types)
- Microsoft Learn: Integral numeric types (int is System.Int32)
- Microsoft Learn: Built-in reference types (string)
- Microsoft Learn: Object and collection initializers
- Microsoft Learn: String interpolation using $
- Microsoft Learn: Fundamentals of garbage collection (the managed heap)
- ECMA-334: C# Language Specification
اختبار يتاح بعد الخطوة الأخيرة.