Example analysis of C structure characteristics

  • 2021-08-12 03:22:21
  • OfStack

In this paper, the characteristics of C # structure are described with examples. Share it for your reference. The details are as follows:

Definition of structure:

Structures can also be defined separately like Class 1.


class a{};
struct a{};

Structures can also be preceded by a control accessor.


public struct student{};
internal struct student{};

You cannot define an obj object using an student structure if the structure student does not have the declarative class program of publice or internal

The object obj cannot call the element x if the element of the structure student does not have the declaration of public
Because the default structure name and element name are of type *******

Procedures:


using System;
public struct student
{
  public int x;
};
class program
{
 public static void Main()
 {
  student obj=new student();
  obj.x=100;  
 }
};

Static members can also be defined in the structure as in the class. When you use it, you must use the class name or structure name to call an instance that does not belong to it. When you declare it, you can define it directly.

Procedures:


using System;
public struct student
{
  public static int a = 10;
};
class exe
{
 public static void Main()
 {
  Console.WriteLine( student.a = 100);
 }
};

Or:


using System;
class base
{
 public struct student
 {
  public static int a = 10;
 };
}
class exe
{
 public static void Main()
 {
  Console.WriteLine( base.student.a = 100);
 }
};

You can define constructors in a structure to initialize members, but you cannot override the default parameterless constructor and default parameterless destructor

Procedures:


public struct student
{
  public int x;
  public int y;
  public static int z;
  public student(int a,int b,int c)
  {
   x=a;
   y=b;
   student.z=c;
  }
};

Member functions can be defined in structures.

Procedures:


public struct student
{
  public void list()
  {
   Console.WriteLine(" This is the constructed function ");
  }
};

Objects of a structure can be created using the new operator (obj) or a single element assignment can be created directly (obj2). This is different from classes because classes can only create objects using new

Procedures:


public struct student
{
  public int x;
  public int y;
  public static int z;
  public student(int a,int b,int c)
  {
   x=a;
   y=b;
   student.z=c;
  }
};
class program
{
 public static void Main()
 {
 student obj=new student(100,200,300);
 student obj2;
 obj2.x=100;
 obj2.y=200;
 student.z=300;
 }
}

When using class objects and functions, reference passing is used, so the fields change
When using structure objects and functions, value passing is used, so the fields do not change

Procedures:


using System;
class class_wsy
{
 public int x;
}
struct struct_wsy
{
 public int x;
}
class program
{
 public static void class_t(class_wsy obj)
 {
  obj.x = 90;
 }
 public static void struct_t(struct_wsy obj)
 {
  obj.x = 90;
 }
 public static void Main()
 {
  class_wsy obj_1 = new class_wsy();
  struct_wsy obj_2 = new struct_wsy();
  obj_1.x = 100;
  obj_2.x = 100;
  class_t(obj_1);
  struct_t(obj_2);
  Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
  Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
  Console.Read();
 }
}

The results are:


class_wsy obj_1.x=90
struct_wsy obj_2.x=100

I hope this article is helpful to everyone's C # programming.


Related articles: