What's the difference between C# class and structure?
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.