Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create C # structure

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how to create the C# structure, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to create the C# structure. Let's take a look.

1. Structure creation

The creation of a structure needs to be declared using the struct keyword, and there can be different types of variables in the structure:

Struct role {public string name; public int age;}

Although structures can store multiple values like arrays, their data types are different, arrays belong to reference types, and structures belong to value types:

Role role1;... / / the author is lazy and omits all the assignment steps if (role1 is System.ValueType) {Console.WriteLine ("structure is a value type");} else {Console.WriteLine ("structure is a reference type");}

Note: if you want to use role1 here, first make sure that all variables in role1 have been assigned. As long as one is not assigned, it will generate an error.

2. Other types in the structure (1) array

In the structure, you can declare not only variables, but also arrays:

Struct role {public string [] food;} static void Main (string [] args) {role role1; role1.food = new string [] {"Xiaolongbao", "vegetable buns", "meat buns"};} (2) object

You can declare an object:

Struct role {public object obj;} static void Main (string [] args) {role role1; role1.obj = 123;} (3) class

You can also declare classes:

Class C1 {struct role {public c2 c2;} static void Main (string [] args) {role role1; role1.c2 = new c2 ();}} class c2 {public string name = "xxx"; public void method () {Console.WriteLine ("member methods");}} (4) structure

In addition to declaring many data types, structures, as one of the data types, can also be declared:

Struct role {public string rname; public struct skill {public string sname;}} static void Main (string [] args) {role role1; role1.rname = "Qiao Feng"; role.skill S1; s1.sname = "falling Dragon Eighteen Palms"; Console.WriteLine ("{0} is using {1}", role1.rname, s1.sname); / / Qiao Feng is using Jianglong Eighteen Palms} (5) method

In addition to declaring various data types, you can also declare methods in the structure:

Struct role {public void sayHello () {Console.WriteLine ("Hello everyone");}} static void Main (string [] args) {role role1; role1.sayHello ();} this is the end of the article on "how to create a C # structure". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to create a C# structure". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report