亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 结构

C#结构

在本教程中,您将通过示例了解 C# 中的结构。

struct(结构)就像C#中的一个类,用于存储数据。但是,与类不同,结构是值类型。

假设我们要存储一个人的姓名和年龄。我们可以创建两个变量:name年龄 并存储价值。

但是,假设我们要存储多个人的相同信息。

在这种情况下,为个人创建变量可能是一项乏味的任务。为了克服这个问题,我们可以创建一个存储 name 的结构体 和 年龄 .现在,每个人都可以使用这个结构体。


在 C# 中定义结构

在 C# 中,我们使用 struct 定义结构的关键字。例如,

struct Employee  {
  public int id;
}

这里,id 是结构内的一个字段。结构体也可以包含方法、索引器等。


声明结构变量

在我们使用结构之前,我们首先需要创建一个结构变量。我们使用带有变量的结构名称来声明结构变量。例如,

struct Employee {
  public int id;
}
...

// declare emp of struct Employee
Employee emp;

在上面的示例中,我们创建了一个名为 Employee 的结构体 .在这里,我们声明了一个变量 emp 结构 Employee .


访问 C# 结构

我们将结构变量与 . 一起使用 运算符来访问结构的成员。例如,

struct Employee {
  public int id;
}
... 
// declare emp of struct Employee
Employee emp;

// access member of struct      
emp.id = 1;

在这里,我们使用了变量 emp 结构 Employee. 运算符访问 Employee 的成员 .

emp.id = 1;

这将访问 id struct Employee 的字段 .

注意 :原始数据类型,如 int , bool , float 是 C# 中的预定义结构。


示例:C# 结构

using System;
namespace CsharpStruct {
 
  // defining struct
  struct Employee {
    public int id;

    public void getId(int id) {
      Console.WriteLine("Employee Id: " + id);
    }
  }
 
  class Program {
    static void Main(string[] args) {
 
      // declare emp of struct Employee
      Employee emp;
      
      // accesses and sets struct field
      emp.id = 1;

      // accesses struct methods
      emp.getId(emp.id);

      Console.ReadLine();
    }
  }
}

输出

Employee Id: 1

在上面的程序中,我们创建了一个名为 Employee 的结构体 .它包含一个字段 id 和一个方法 getId() .

程序内部 类,我们声明了一个变量 emp 结构 Employee .然后我们使用 emp 变量来访问类的字段和方法。

注意 :我们也可以使用 new 实例化一个结构体 关键词。例如,

Employee emp = new Employee();

在这里,这一行调用结构的无参数构造函数,并用默认值初始化所有成员。


C# struct 中的构造函数

在 C# 中,结构还可以包含构造函数。例如,

struct Employee {

  public int id;

  // constructor 
  public Employee(int employeeId) {
   id = employeeId
  }
}

在这里,我们创建了一个参数化构造函数Employee() 带参数 employeeId .

注意 :我们无法在 C# 9.0 或更低版本中创建无参数构造函数。


示例:C# 结构中的构造函数

using System;
namespace CsharpStruct {
 
  // defining struct
  struct Employee {
    public int id;
    
    public string name;

    // parameterized constructor
    public Employee(int employeeId, string employeeName) {
   
      id = employeeId;
      name = employeeName;
    }
  }
 
  class Program {
    static void Main(string[] args) {
 
      // calls constructor of struct
      Employee emp = new Employee(1, "Brian");

      Console.WriteLine("Employee Name: " + emp.name);
      Console.WriteLine("Employee Id: " + emp.id);

      Console.ReadLine();
    }
  }
}

输出

Employee Name: Brian
Employee Id: 1

在上面的示例中,我们在 Employee 中创建了一个参数化构造函数 结构。在构造函数中,我们已经分配了字段的值:id名称 .

注意这一行,

Employee emp = new Employee(1, "Brian");

就像在 C# 类中一样,我们使用 new 关键字来调用构造函数。这里,1“布赖恩” 是传递给构造函数的参数,它们被分配给参数 employeeID员工姓名 分别。”

注意 :我们必须在参数化构造函数中为 struct 的每个字段赋值。例如,

// error code
public Employee(int employeeID, employeeName) {
  id = employeeID;
}

在这里,我们没有为 name 赋值 场地。所以代码会产生错误。


C# 结构中的属性

我们还可以在 C# 结构中使用属性。例如,

using System;
namespace CsharpStruct {
 
  // defining struct
  struct Employee {
    public int id;
    
    // creates property
    public int Id {

      // returns id field
      get {
        return id;
      }

      // sets id field
      set {
        id = value;
      }
    }
  }
 
  class Program {
    static void Main(string[] args) {
 
      // calls the constructor of struct
      Employee emp = new Employee();

      emp.Id = 1;
      Console.WriteLine("Employee Id: " + emp.Id);

      Console.ReadLine();

    }
  }
}

输出

Employee Id: 1

在上面的例子中,我们有 Id Employee 中的属性 结构。

get 方法返回 id 字段和 set 方法将值分配给 id 字段。


C#中类和结构的区别

在 C# 中,类和结构看起来很相似。但是,它们之间存在一些差异。

类是引用类型,而结构是值类型。例如,

using System;
namespace CsharpStruct {
 
  // defining class
  class Employee {
    public string name;

  }
 
  class Program {
    static void Main(string[] args) {

      Employee emp1 = new Employee();
      emp1.name = "John";

      // assign emp1 to emp2
      Employee emp2 = emp1;
      emp2.name = "Ed";
      Console.WriteLine("Employee1 name: " + emp1.name);

      Console.ReadLine();
    }
  }
}

输出

Employee1 name: Ed

在上面的例子中,我们给 emp1 赋值 到 emp2 . emp2 object 指的是与 emp1 相同的对象 .因此,emp2 中的更新 更新 emp1 的值 自动。

这就是为什么类是引用类型 .

与类相反,当我们将一个结构变量分配给另一个结构变量时,结构的值被复制到分配的变量中。所以更新一个结构变量不会影响另一个。例如,

using System;
namespace CsharpStruct {
 
  // defining struct
  struct Employee {
    public string name;

  }
 
  class Program {
    static void Main(string[] args) {

      Employee emp1 = new Employee();
      emp1.name = "John";

      // assign emp1 to emp2
      Employee emp2 = emp1;
      emp2.name = "Ed";
      Console.WriteLine("Employee1 name: " + emp1.name);
      
      Console.ReadLine();
    }
  }
}

输出

Employee1 name: John

当我们给 emp1 赋值时 到 emp2 , 一个新值 emp2 被建造。这里,emp1 的值 被复制到 emp2 .所以,改变 emp2 不影响 emp1 .

这就是为什么 struct 是 值类型 .

此外,结构中不能继承,而继承是 C# 类的一个重要特性。


C语言

  1. 本月最佳员工比尔瑞恩!
  2. C结构
  3. C 结构体和指针
  4. C 结构和功能
  5. 如何避免在竞争激烈的“未来工作”中失败
  6. C++ 结构与示例
  7. 制造机器人能否提高员工安全?
  8. 为什么是时候改进员工排班
  9. 要降低风险,请考虑重新检查员工背景
  10. 在 COVID-19 中加强员工敬业度的四种方法
  11. DVIRC 欢迎新员工,提拔有价值的员工
  12. 令人兴奋的新兴技术类别:员工生产力分析