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

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

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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