C Basic Syntax: A detailed explanation of the structure and class differences

  • 2021-06-28 13:43:17
  • OfStack

Structures and classes are similar and can contain data members and function members, but unlike classes, structures are a value type (we can understand that there is no inheritance issue for a particular value type) and allocating data to them does not require allocating memory from the managed heap.Variables of a structure type directly contain data of that structure, while variables of a class type contain only a reference to the corresponding object.

The following summarizes the differences between structure and class under 1:

1. Structures are value types, and assigning a variable to a structure type creates a copy of the assignment.
2. The default value for a structure instance is not null, but an initial value with a default value.
3. this has a different meaning in structure and class.
4. Structures do not support inheritance (so declarative accessibility of structure members cannot be protected, protected internal, and function members in structures cannot be abstract or virtual, so the override modifier only applies to overriding methods inherited from System.ValueType) but interfaces can be implemented.
5. Instance field declarations in structures cannot contain variable initializers
6. A parameterless instance constructor cannot be declared in a structure.
7. Destructors cannot be declared in structures.

Test distinguishing feature code:


using System;
namespace StructAndClass
{
  struct SPoint
  {
    public int x, y;
    public SPoint(int x, int y)
    {
      this.x = x;
      this.y = y;
    }
  }
  class CPoint
    {
      public int x, y;
      public CPoint(int x, int y)
      {
        this.x = x;
        this.y = y;
      }
    }
  class Test
  {
    public static void Main()
    {
      SPoint sp1 = new SPoint(2, 5);
      Console.WriteLine(" structure /sp1 Initial value: ");
      Console.WriteLine("sp1.x={0}", sp1.x);
      SPoint sp2 = sp1;
      Console.WriteLine("sp1=sp2 After: ");
      Console.WriteLine("sp1.x={0}");
      Console.WriteLine("sp1.x={0}", sp1.x);
      Console.WriteLine("sp2.x={0}", sp2.x);
      sp1.x = 5;
      Console.WriteLine(" Change Again sp1 After the value of: ");
      Console.WriteLine("sp1.x={0}", sp1.x);
      Console.WriteLine("sp2.x={0}", sp2.x);
      Console.WriteLine("============================");
      CPoint cp1 = new CPoint(2,5);
      Console.WriteLine(" class /cp1 Initial value: ");
      Console.WriteLine("cp1.x={0}", cp1.x);
      CPoint cp2 = cp1;
      Console.WriteLine("cp1=cp2 After: ");
      Console.WriteLine("cp1.x={0}", cp1.x);
      Console.WriteLine("cp2.x={0}", cp2.x);
      cp1.x = 5;
      Console.WriteLine(" Change Again cp1 After the value of: ");
      Console.WriteLine("cp1.x={0}", cp1.x);
      Console.WriteLine("cp2.x={0}", cp2.x);
      Console.ReadKey();
    }
  }
}


Structures are valid even if they are not declared by the new operator. Structures cannot declare a parameterless strength constructor, but they implicitly contain a parameterless constructor: they cannot call instance function members of a structure until all fields of the structure are explicitly assigned.All fields should be assigned values in the constructor of the structure.For example:


struct DC
  {
    public int x, y;
    public int X
    {
      set
      {
        x = value;
      }
      get
      {
        return x;
      }
    }
    public int Y
    {
      set
      {
        y = value;
      }
      get
      {
        return y;
      }
    }
    public DC(int x,int y)
    {
      this.x = x;
      this.y = y;
    }
  }
  struct RDC
  {
    public int x, y;
    public int X
    {
      set
      {
        x = value;
      }
      get
      {
        return x;
      }
    }
    public int Y
    {
      set
      {
        y = value;
      }
      get
      {
        return y;
      }
    }
    public RDC(int x, int y)
    {
      this.x = x;
      this.y = y;
    }
  }
  class Test
  {
    public static void Main()
    {
      DC dc=new DC();
      dc.x = 3;
      dc.y = 5;
      Console.WriteLine(" Already paired x,y After initialization: ");
      Console.WriteLine(" Access and modification is now available dc.X={0} Value of ",dc.X);
      Console.WriteLine(" I created it here 1 individual DC Copy structure RDC/n And not right x,y Initialization will fail ");
      RDC rdc;
       rdc.y = 5;// Can be compiled through 
       rdc.X = 3;// Errors will occur here , Cannot compile through 
      Console.WriteLine("=======test over================");
     }
  }

 

Within the instance constructor of a structure, this corresponds to an out parameter of a structure type (which must be explicitly assigned internally), whereas within the instance function member of a structure, this corresponds to an ref parameter of a structure type.In both cases, this itself is equivalent to a variable, which makes it possible to modify the entire structure involved in the function member call (such as assigning this or passing this as an ref or out parameter).For example: (cite the example above)



struct DC
  {
    public int x, y;
    public int X
    {
      set
      {
        x = value;
      }
      get
      {
        return x;
      }
    }
    public int Y
    {
      set
      {
        y = value;
      }
      get
      {
        return y;
      }
    }
    public DC(int x,int y)
    {
      X= x;// This is wrong 
      Y = y;// Will prompt not give this All field assignments for objects 
    }
  }


Related articles: