This method supposed to print the following:
Creating r1
-> Assigning r2 to r1
-> Changing values of r2
String = This is new info!, Top = 10, Bottom = 50, Left = 10, Right = 50
String = This is new info!, Top = 10, Bottom = 4444, Left = 10, Right = 50
but instead of printing string value it prints the class in which the string is located
Creating r1
-> Assigning r2 to r1
-> Changing values of r2
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 50, Left = 10, Right = 50
String = Csharp_Projects.Program+ShapeInfo, Top = 10, Bottom = 4444, Left = 10, Right = 50
this is the code:
using System;
namespace Csharp_Projects
{
static class Program
{
static void Main(string[] args)
{
ShapeInfo.Rectangle.ValueTypeContainingRefType();
}
// this class which takes an string as parameter
public class ShapeInfo
{
public string infoString;
public ShapeInfo(string info)
{
infoString = info;
}
// this struct has two fields, one is int type and the other is ShapeInfo (above) and a constructor which set the values
public struct Rectangle
{
public ShapeInfo rectInfo;
public int recTop, rectleft, rectBottom, rectRight;
public Rectangle(string info, int top, int left, int Buttom, int Right)
{
rectInfo = new ShapeInfo(info);
recTop = top;
rectBottom = Buttom;
rectRight = Right;
rectleft = left;
}
// this method print the results
public void Display()
{
Console.WriteLine("string={0},top={1},Bottom={2},"+"left={3},Right={4}",rectInfo,recTop,rectBottom,rectRight,rectleft);
}
// this method make an object and assign it to second variable and then change the values of second variable .
public static void ValueTypeContainingRefType()
{
Console.WriteLine("Creating r1");
Rectangle r1 = new Rectangle("First Rec", 10, 10, 50, 50);
Console.WriteLine("Assigning r2 to r1");
Rectangle r2 = r1;
Console.WriteLine("Change Values of r2");
r2.rectInfo.infoString = "This is new info!";
r2.rectBottom = 4444;
r1.Display();
r2.Display();
}
}
}
}
}
I really couldn't make out why it happened maybe my knowledge about C# system is not enough, what cause this?