C # Tutorial C # structure (struct)

Source: Internet
Author: User

C # structure (struct)

In C #, structs are value type data structures. It enables a single variable to store relevant data for various data types. The struct keyword is used to create structures.

A structure is used to represent a record. Suppose you want to track the dynamics of books in a library. You may want to track the following properties for each book:

Title

Author

Subject

Book ID

Defining structures

In order to define a structure, you must use a struct statement. A struct statement defines a new data type with multiple members for a program.

For example, you can declare the book structure in the following way:

struct books{public   string title;   public string author;   public string subject;   public int book_id;};

The following program demonstrates the use of structs:

Using System;   struct books{public string title;   public string author;   public string subject;  public int book_id;};        public class teststructure{public static void Main (string[] args) {Books Book1;        /* Declare BOOK1, type Book */Books Book2;      /* Declaration BOOK2, type Book */* Book 1 Details */book1.title = "C programming";       Book1.author = "Nuha Ali";      Book1.subject = "C programming Tutorial";      book1.book_id = 6495407;      /* Book 2 Details */book2.title = "Telecom billing";      Book2.author = "Zara Ali";      Book2.subject = "Telecom billing Tutorial";      book2.book_id = 6495700;      /* Print BOOK1 Information */Console.WriteLine ("Book 1 title: {0}", Book1.title);      Console.WriteLine ("Book 1 Author: {0}", Book1.author);      Console.WriteLine ("Book 1 Subject: {0}", Book1.subject);      Console.WriteLine ("Book 1 book_id: {0}", book1.book_id);      /* Print BOOK2 Information */Console.WriteLine ("Book 2 title: {0}", Book2.title); ConsolE.writeline ("Book 2 Author: {0}", Book2.author);      Console.WriteLine ("Book 2 Subject: {0}", Book2.subject);             Console.WriteLine ("Book 2 book_id: {0}", book2.book_id);   Console.readkey (); }}

When the above code is compiled and executed, it produces the following results:

Book 1 title:c programmingbook 1 Author:nuha alibook 1 subject:c programming Tutorialbook 1 Book_id:6495407book 2 title:telecom Billingbook 2 Author:zara alibook 2 subject:telecom billing Tutorialbook 2 book_id:6495700

Features of C # architecture

You have used a simple structure called Books. The structure in C # differs from the structure in traditional C or C + +. The structure in C # has some features:

Structs can have methods, fields, indexes, properties, operator methods, and events.

A struct can define a constructor, but it cannot define a destructor. However, you cannot define a default constructor for the struct. The default constructor is automatically defined and cannot be changed.

Unlike classes, structs cannot inherit from other structures or classes.

Structs cannot be the infrastructure of other structures or classes.

A struct can implement one or more interfaces.

Struct members cannot be specified as abstract, virtual, or protected.

When you create a struct object using the New operator, the appropriate constructor is called to create the structure. Unlike classes, structs can be instantiated without the use of the NEW operator.

If you do not use the New operator, the field is assigned only after all the fields have been initialized and the object is used.

Class VS Structure

Classes and structs have the following basic differences:

A class is a reference type, and a struct is a value type.

The structure does not support inheritance.

Structs cannot declare a default constructor.

For the above discussion, let's rewrite the previous example:

Using System;   struct books{private string title;   private string author;   private string subject;   private int book_id;      public void GetValues (String t, string A, string s, int id) {title = T;      Author = A;      Subject = s;   book_id = ID;      public void Display () {Console.WriteLine ("title: {0}", title);      Console.WriteLine ("Author: {0}", Author);      Console.WriteLine ("Subject: {0}", Subject);   Console.WriteLine ("book_id: {0}", book_id);  }}; public class teststructure{public static void Main (string[] args) {Books Book1 = new Books ();/* Declaration Book1, type B Ook */Books BOOK2 = new Books (); /* Declaration BOOK2, type Book */* Book 1 Details */book1.getvalues ("C Programming", "Nuha Ali", "C programming Tutoria      L ", 6495407);      /* Book 2 Details */book2.getvalues ("Telecom billing", "Zara Ali", "Telecom billing Tutorial", 6495700);      /* Print BOOK1 information */Book1.display (); /* Print BOOK2 information */Book2.display ();      Console.readkey (); }}

When the above code is compiled and executed, it produces the following results:

TITLE:C Programmingauthor:nuha alisubject:c Programming tutorialbook_id:6495407title:telecom Billingauthor:zar A alisubject:telecom billing tutorialbook_id:6495700

The above is the "C # tutorial" C # structure (struct) content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.