can I ask about your opinion for this description that prove that structs is value type

c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
c#
/*
* in this example we have a structure and a class
* and those are done to prove the statment above
* that the struct is a value type while the class is
* refrence type
*/
public struct Point
{
public int x;
public int y;
}


class MyClass
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
/*
* in here we used the struct we inilize it with a value x = 10
* and y = 20
*/
Point p1 = new Point { x = 10, y = 20 };

// then we write the value of x to the console
// it gets (10)
Console.WriteLine(p1.x);

//we used the ModifayStruct(Point P1) and we passed the P1 struct to it
// where this method modifayes the p1.x value to 12 other than 10
ModifayStruct(p1);

// the result when writting to the console is 10
// this is happiesn becuse the struct is a value type when we passed the struct to
// a method we are passing a copy of the struct not an adress to it in the memory
Console.WriteLine(p1.x);


/*
* in here we instatiated a mew Myclass object with x feild is 10
*/
MyClass @class = new MyClass { x = 10 };

// we used the ModifayClass(MyClass obj) and passed the above object to it
// where this method modifies x feild inside of it
ModifayClass(@class);

// the result is that x got modified that becuse when we passed a refrence varible to the
// method it got passed by refrence whicj means we are passng the address of that object
// to the method
Console.WriteLine(@class.x);

}

// modifays the point structs feild x to 12
public static void ModifayStruct(Point p1)
{
p1.x = 12;
}

// modifays the point class feild x to 12
public static void ModifayClass(MyClass obj)
{
obj.x = 12;
}
}
2 Replies
Omnissiah
Omnissiah6mo ago
yeah, just a little less mangled english and i would say it's fine for me
steven preadly
steven preadlyOP6mo ago
modifiyed version
c#
public struct Point // Struct for holding x and y coordinates
{
public int x;
public int y;
}

class MyClass // Class for demonstration
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
// Create a Point struct with initial values
Point p1 = new Point { x = 10, y = 20 };

// Print the initial value of p1.x
Console.WriteLine(p1.x); // Output: 10

// Pass p1 to ModifyStruct (copy of p1 is passed)
ModifyStruct(p1);

// Print p1.x again (value remains unchanged)
Console.WriteLine(p1.x); // Output: 10 (original value preserved)

// Create a MyClass object with initial value
MyClass @class = new MyClass { x = 10 };

// Pass @class to ModifyClass (reference is passed)
ModifyClass(@class);

// Print @class.x (value is modified)
Console.WriteLine(@class.x); // Output: 12 (original value modified)
}

// Modifies the struct's x value (operates on a copy)
public static void ModifyStruct(Point p1)
{
p1.x = 12;
}

// Modifies the class's x value (operates on the original object)
public static void ModifyClass(MyClass obj)
{
obj.x = 12;
}
}
c#
public struct Point // Struct for holding x and y coordinates
{
public int x;
public int y;
}

class MyClass // Class for demonstration
{
public int x;
}

internal class Program
{
private static void Main(string[] args)
{
// Create a Point struct with initial values
Point p1 = new Point { x = 10, y = 20 };

// Print the initial value of p1.x
Console.WriteLine(p1.x); // Output: 10

// Pass p1 to ModifyStruct (copy of p1 is passed)
ModifyStruct(p1);

// Print p1.x again (value remains unchanged)
Console.WriteLine(p1.x); // Output: 10 (original value preserved)

// Create a MyClass object with initial value
MyClass @class = new MyClass { x = 10 };

// Pass @class to ModifyClass (reference is passed)
ModifyClass(@class);

// Print @class.x (value is modified)
Console.WriteLine(@class.x); // Output: 12 (original value modified)
}

// Modifies the struct's x value (operates on a copy)
public static void ModifyStruct(Point p1)
{
p1.x = 12;
}

// Modifies the class's x value (operates on the original object)
public static void ModifyClass(MyClass obj)
{
obj.x = 12;
}
}
Want results from more Discord servers?
Add your server