On the difference between structure and class in C

  • 2021-11-24 02:43:29
  • OfStack

1.

Structure: Value type, stored on the stack, located in the logical area of memory of the computer Class: Reference type, stored in the heap and located in different logical locations in the computer memory

2.

Smaller data usage structure; When a structure value is passed to a method, the entire data structure is passed; Passing a class is actually passing a reference to an object, that is, only the memory address; To modify the structure, what changes is the copy of the structure, which is the definition of how the value type works: passing a copy of the value; Passing a reference to the class itself means modifying the value in the class, which actually changes the original object;

3. Code examples

1. New PointClass. cs


namespace StructAndClass
{
 internal class PointClass
 {
 public PointClass(int x, int y)
 {
  X = x;
  Y = y;
 }
 public int X { get; set; }
 public int Y { get; set; }
 }
}

2. New PointStruct. cs


namespace StructAndClass
{
 internal struct PointStruct
 {
 public int X { get; set; }
 public int Y { get; set; }
 public PointStruct(int x, int y)
 {
  X = x;
  Y = y;
 }
 }
}

3.Program.cs


using System;
namespace StructAndClass
{
 internal class Program
 {
 private static void Main(string[] args)
 {
  Console.WriteLine("PointStruct =====");
  var pStruct = new PointStruct(10, 10);
  Console.WriteLine(" Initial value: x={0} , y={1}", pStruct.X, pStruct.Y);
  ModifyPointStruct(pStruct);
  Console.WriteLine(" Call  ModifyPointStruct()  Value after: x={0} , y={1}", pStruct.X, pStruct.Y);
  Console.WriteLine();
  Console.WriteLine("PointClass =====");
  var pClass = new PointClass(10, 10);
  Console.WriteLine(" Initial value: x={0} , y={1}", pClass.X, pClass.Y);
  ModifyPointClass(pClass);
  Console.WriteLine(" Call  ModifyPointClass()  Value after: x={0} , y={1}", pClass.X, pClass.Y);
  Console.Read();
 }
 private static void ModifyPointStruct(PointStruct point)
 {
  Console.WriteLine(" Invoke the method: ModifyPointStruct");
  point.X = 20;
  point.Y = 20;
  Console.WriteLine(" Modified to the value: x={0}, y={1}", point.X, point.Y);
 }
 private static void ModifyPointClass(PointClass point)
 {
  Console.WriteLine(" Invoke the method: ModifyPointClass");
  point.X = 20;
  point.Y = 20;
  Console.WriteLine(" Modified to the value: x={0}, y={1}", point.X, point.Y);
 }
 }
}

4. Outcome:

[Analysis]

When ModifyPointStruct (PointStruct point) is called, only a copy of the structure is modified, so the original structure has not changed;   

When ModifyPointClass (PointClass point) is called, the object modified is the original object, because the parameter is passed a reference address that points to the original object

4. Summary

Structures are value types and are passed on the stack, and every time a method is used to modify them, only a copy of the structure is made;

As for the class, you pass a reference to the memory address and modify the initial value


Related articles: