Understanding Value and Referance

Understanding Structs

The keyword struct is used to create a user-defined type that contains a small of related fields.
Structs are value type as opposed to classes, which are a reference type.

Define a struct

Public Struct Point
{
      public double x,y;
}

Struct can contain 
  • Contractors
  • Methods
  • Properties

Structs cannot inherit from another class or structs.


Understanding Memory Allocation

After you enter a value or text into a cell, you can modify it in a number of ways.
In particular, you can remove the contents completely, enter a different value to replace what was there, or alter what you have entered.

When you create a value type for example int, a named memory is created.
If you didn't assign any value to the variable then the default value is null.
When you assign the value, the memory address is identified by the memory variable name and updated.

When you create a reference type, for example, a string.
a memory location is created that will identify by name.

However, the location will not contain the actual value rather the variable will store the memory address of the location. 



Over here we have two examples one class Rectangle which is a reference type, and one struct which is a value type.



 When creating a new Point p1 and initializing value to p1 
p1.x = 10;
p2.y = 20;

and when creating another point p2 we saying p2 = to p1 which means that p2 has the same value as p1 so 
p2.x = 10;
p2.y = 20;

but after the 
p2.x = 100;

We give p2.x a new value and it only affects p2.x the value of p1 is remain unaffected.

  
rect1 is an instance of Rectangle with italicizing Length and Width

Length = 10.0,
Width = 20.0;

Let's create another instance of Rectangle ad call it rect2 and assign the same value like rect1. 
so at this point 
rect2 = rect1,
now let's check what happened when we give the length of rect2 a new value for example 100.
rect2.Length = 100;

Now, rect2.Length is 100 but also rect1.Length is 100,
because rect1 and 2 are reference type both of them referring to the same address so any changes in the properties of one them will make a change in the other.



13. You are reviewing a C# program that contains the following class:
public class Rectangle
{
public double Length {get; set;}
public double Width { get; set; }
}
The program executes the following code as part of the Main method:
Rectangle r1, r2;
r1 = new Rectangle { Length = 10.0, Width = 20.0 };
r2 = r1;
r2.Length = 30;
Console.WriteLine(r1.Length);
What will be the output when this code is executed?
a) 10
b) 20
c) 30
d) 40

Difficulty: Medium
Section Reference: Understanding Values and References
The class Rectangle is a reference type, and the content of variable r1 is actually a reference to a memory location that holds a Rectangle object. So, after the r2 = r1; statement,
both r1 and r2 point to the same memory location and in turn the same Rectangle object. In other words, there is only one rectangle object in memory, and both r1 and r2 are referring to it. When the Length property is modified, the change applies to both objects r1 and r2.

You are reviewing a C# program. The program contains the following class:
public struct Rectangle
{
public double Length {get; set;}
public double Width { get; set; }
}
The program executes the following code as part of the Main method:
Rectangle r1, r2;
r1 = new Rectangle { Length = 10.0, Width = 20.0 };
r2 = r1;
r2.Length = 30;
Console.WriteLine(r1.Length);
What will be the output when this code is executed?
a) 10
b) 20
c) 30
d) 40

Answer: a
Difficulty: Medium
Section Reference: Understanding Values and References
The struct is a value rather than a reference type, so both r1 and r2 maintain their own copies of data. So, after the r2 = r1; statement, both r1 and r2 point to different memory locations. When the Length property for r2 object is modified, the change doesn’t affect the object r1.

Comments

Popular posts from this blog

Understanding Encapsulation

Understanding Polymorphism

Understanding Static Members