1.Declaring a non nullable reference type variable:
Someclass v;
will it allocate a memory to store the reference value(it is 0), which refers to null?
Declaring a nullable reference type variable:
Someclass? v;
2.will it do the same thing as above or no any memory allocated at all?
Loading
Leo QiaoPosted Nov 29, 2024, 6:16 AM
In the c# specification:
The run-time processing of a simple assignment of the form
x = ywith typeTis performed as an assignment toxofywith typeT, which consists of the following recursive steps:xis evaluated if it wasn’t already.xis classified as a variable,yis evaluated and, if required, converted toTthrough an implicit conversion (§10.2).xis an array element of a reference_type, a run-time check is performed to ensure that the value computed foryis compatible with the array instance of whichxis an element. The check succeeds ifyisnull, or if an implicit reference conversion (§10.2.8) exists from the type of the instance referenced byyto the actual element type of the array instance containingx. Otherwise, aSystem.ArrayTypeMismatchExceptionis thrown.yis stored into the location given by the evaluation ofx, and is yielded as a result of the assignment.xis classified as a property or indexer access:yis evaluated and, if required, converted toTthrough an implicit conversion (§10.2).xis invoked with the value resulting from the evaluation and conversion ofyas its value argument.yis yielded as the result of the assignment.xis classified as a tuple(x1, ..., xn)with arityn:yis deconstructed withnelements to a tuple expressione.tis created by convertingetoTusing an implicit tuple conversion.xiin order from left to right, an assignment toxioft.Itemiis performed, except that thexiare not evaluated again.tis yielded as the result of the assignment.Leo QiaoPosted Nov 26, 2024, 10:09 PM
Now, I think:
1.Declaration must allocate memory to variable according to the type of variable. If it doesn't, you can't assign a value to it. Because no memory to hold the value. Assignment(=) does not allocate memory for its left operand except the left operand is immutable type.
2.Declaring variables of non-nullable reference type and variables of nullable reference are the same thing. Because 'null' is the default value fo non-nullable reference type(it is a history of c# language). They all allocate memory for variables, one for reference, one for object.Now, the "object" has not been constructed, it is just a block of all-bits-zero memory, and is treated as "null".
3."null" is a value, which like a state flag.As a value, it occupies memory.
Jaish MathewsPosted Nov 26, 2024, 1:57 PM
Sangeetha SPosted Nov 26, 2024, 11:33 AM
Declaring a non-nullable reference type variable:
Someclass v;, memory is allocated to store the reference itself, but at this point, it doesn't point to any actual object. The reference is initially set tonull, meaning it doesn't reference any valid instance ofSomeclass. So, yes, memory is allocated to hold the reference (which is currentlynull).Declaring a nullable reference type variable:
Someclass? v;, it behaves similarly. Memory is allocated for the reference, and it can also initially point tonull. In this case, the nullable type allows you to explicitly indicate that the variable can hold a null value, which is useful for situations where an object may or may not be present.Leo QiaoPosted Nov 26, 2024, 7:52 AM
In your sample code, if "SomeClass v" does not allocate any memory for "v", the assignment "v=new SomeClass()" could not assign any value to a variable to which is no memory allocated. Operator= should not allocate memory for a variable.
Amit MohantyPosted Nov 26, 2024, 5:41 AM
When you declare a non-nullable reference type, as Someclass v; it does not allocate memory for the reference value or for the object immediately.
Here v, is declared but not initialized. Since it can't be null, the compiler asks for one before using it by directly assigning a value and assigning an object to it or usage of a constructor. The reference itself (a pointer or handle) will occupycop memory once it is allocated, but declaring it alone doesn't occupy any memory immediately. However if this declaration happens in a method, the compiler would reserve space for it on the stack based on conditions.
In Someclass? v; the variable v can be null, which means it can either point to an object or have no value.
Like the above, v is declared but not initialized. The only difference here is that the compiler does not require initializing before it's used and the declaration of the variable does not directly allocate memory for the reference or an object. The memory will be allocated when the variable is used-for example, when it's initialized or passed into a method.
In above code v gets a memory allocation when assigned new Someclass() and vNull only allocates memory for the reference itself, which holds null.