Get the App
SLTechnology News&Howtos  ›  Development  › 

What's the difference between C# class and structure?

Shulou Source: shulou.com Published: 2022-06-01 02:00:50 10月02日 Update

This article mainly introduces the relevant knowledge of "what's the difference between C# class and structure". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what's the difference between C# class and structure" can help you solve the problem.

Vs-like structure

There are several basic differences between classes and structures:

The class is a reference type, and the structure is a value type.

Structure does not support inheritance.

Structure cannot declare a default constructor.

In response to the above discussion, let's rewrite the previous example:

Using System

Using System.Text

Struct Books

{

Private string title

Private string author

Private string subject

Private int book_id

Public void setValues (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 (); / * declare Book1 with type Books * /

Books Book2 = new Books (); / * declare Book2 with type Books * /

/ * book 1 details * /

Book1.setValues ("C Programming"

"Nuha Ali", "C Programming Tutorial", 6495407)

/ * book 2 details * /

Book2.setValues ("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: Zara AliSubject: Telecom Billing TutorialBook_id: 6495700 that's all about "what's the difference between C# classes and structures?" Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Structure differences types categories knowledge information industry utility differences code content functions examples practicality practice articles methods more cases knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi MySQL Docker Huawei