Declaration and Instantiation of variables

c#chris

Member
Joined
Dec 10, 2024
Messages
24
Programming Experience
1-3
Hello,
could you pls. explain the difference of the following instructions:

C#:
class Car
{
  string color = "red";
  private void SpeedUp() { // dosth}
}

Case 1:
Car ferrari;

Case2:
Car ferrari = new();

Case3:
Car ferrari {get;set;}

Case4:
Car ferrari {get;set;} = new();


What happens in the cases above, if maserati is assigned to ferrari ?

Car maserati = {get; set;} new();

ferrari = maserati;
 
This sounds very much like homework and it's not for us to cheat for you. How about you tell us what you think and we can tell you whether you're on the right track and provide pointers if you're not?
 
Actually it is not homework ;-) but I will try my best:

Case1:
No memory allocated, just a memory adress assigned (like pointer in c/c++).
ferrari = maserati -> ferrari pointer points to maserati object now ?!
I can access maserati public properties(Members via ferrari pointer.

Case2:
Memory for Object ist allocated at a specific memory location.
ferrari = maserati -> maserati objects overwrites ferrari object ?!
I can access maserati public properties(Members via ferrari object.

Case3:
For a single property based on int, bool etc. I would say this is just a shortcut.
In connection with a class I dont know, because there might be many properties inside.
Assignments are unclear.

Case4:
This is a mystery.
Assignments are unclear.
 
Firstly, I suggest that you do some reading on value types and reference types. Variables of value types contain an object while variables of reference types contain a reference to an object. When you assign one variable to another, you copy the contents of the source variable into the destination. For value type variables, you're copying an object. For reference type variables, you're copying a reference, so there are now two references to the same object. People tend to think that value types more closely represent real life but it's actually reference types that do. Let's say that I live in a house and you move in with me. My house is now your house too:
C#:
you.House = me.House;
That didn't just create a second house. There's simply two references to the same house object.

As for your cases, the first two declare fields while the last two declare properties. Under the hood, a property is a field with two accessor methods: one to get the value and one to set. Because the field is accessed by dedicated methods, you can add extra functionality over a basic field, e.g. validation and raising events. The odd numbered cases declare members without assigning them a default value while the even numbered cases initialise the members with a new object of the appropriate type.
 
Case 3 makes use of automatic properties that came in C# version 3 (?) So when you write:
C#:
Car ferrari {get;set;}
The compiler generates something like this:
C#:
Car <ferrari>k__BackingField;

Car ferrari
{
    get
    {
        return <ferrari>k__BackingField;
    }

    set
    {
        <ferrari>k__BackingField = value;
    }
}
Note, the <ferrari>k__BackingField is not accessible to your regular C# code. If you bendover backwards using reflection it may be accessible. Also note that naming convention may have changed since when the automatic properties feature was introduced to discourage people trying to use reflection.

The reason for this is because in the first few versions of C#, this boilerplate code was commonly written by people manually.
C#:
private SomeType _innerValue;

public SomeType Value
{
    get
    {
        return _innerValue;
    }

    set
    {
        _innerValue = value;
    }
}
 
Back
Top Bottom