This is the 21st day of my participation in the August Text Challenge.More challenges in August
Struct 💙
In C#, structs are value type data structures. It allows a single variable to store related data of various data types. The struct keyword is used to create structures.
Structures are used to represent a record. Suppose you want to track the movement of books in a library. You might want to track the following properties for each book:
- Title
- Author
- Subject
- Book ID
Defining structure
To define a structure, you must use a struct statement. The struct statement defines a new data type for the program with multiple members.
For example, the Book structure could be declared as follows:
struct Books
{
public string title;
public string author;
public string subject;
public int book_id;
};
Copy the code
The following program demonstrates the use of the structure:
The instanceusing System;
using System.Text;
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, of type Books */
Books Book2; /* Declare Book2, of type Books */
/* book 1 */
Book1.title = "C Programming";
Book1.author = "Nuha Ali";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 */
Book2.title = "Telecom Billing";
Book2.author = "Zara Ali";
Book2.subject = "Telecom Billing Tutorial";
Book2.book_id = 6495700;
/* Prints 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);
/* Prints 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(); }}Copy the code
When the above code is compiled and executed, it produces the following results:
Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
C# structure features
A simple structure called Books has been used. The structure in C# is different from that in traditional C or C++. Structures in C# have the following characteristics:
- Structures can have methods, fields, indexes, attributes, operator methods, and events.
- A structure can define a constructor, but not a destructor. However, you cannot define a no-argument constructor for a structure. The no-argument constructor (the default) is automatically defined and cannot be changed.
- Unlike classes, structures cannot inherit from other structures or classes.
- Structures cannot be used as infrastructure for other structures or classes.
- A structure can implement one or more interfaces.
- Structure members cannot be specified as abstract, Virtual, or protected.
- When you create a structure object using the New operator, the appropriate constructor is called to create the structure. Unlike classes, structures can be instantiated without using the New operator.
- If the New operator is not used, fields are assigned and objects are not used until all fields have been initialized.
Class vs structure
There are several basic differences between classes and constructs:
- Classes are reference types and structures are value types.
- The structure does not support inheritance.
- Structures cannot declare default constructors.
For the above discussion, let’s rewrite the previous example:
The instanceusing 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, of type Books */
Books Book2 = new Books(); /* Declare Book2, of type Books */
/* book 1 */
Book1.setValues("C Programming"."Nuha Ali"."C Programming Tutorial".6495407);
/* book 2 */
Book2.setValues("Telecom Billing"."Zara Ali"."Telecom Billing Tutorial".6495700);
/* Prints Book1 information */
Book1.display();
/* Prints Book2 information */Book2.display(); Console.ReadKey(); }}Copy the code
When the above code is compiled and executed, it produces the following results:
Title : C Programming Author : Nuha Ali Subject : C Programming Tutorial Book_id : 6495407 Title : Telecom Billing Author : Zara Ali Subject : Telecom Billing Tutorial Book_id : 6495700
Enumeration (Enum) 💛
Enumerations are a set of named integer constants. Enumeration types are declared using the enum keyword.
C# enumerations are value types. In other words, enumerations contain their own values and cannot inherit or pass inheritance.
Declare an enum variable.
General syntax for declaring enumerations:
enum <enum_name>
{
enumeration list
};
Copy the code
Among them,
- Enum_name Specifies the type name of the enumeration.
- The Enumeration list is a comma-separated list of identifiers.
Each symbol in an enumerated list represents an integer value, an integer value greater than the symbol that precedes it. By default, the first enumeration symbol has a value of 0. For example:
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
Copy the code
The instance
The following example demonstrates the use of an enumeration variable:
The instanceusing System;
public class EnumTest
{
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
static void Main()
{
int x = (int)Day.Sun;
int y = (int)Day.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y); }}Copy the code
When the above code is compiled and executed, it produces the following results:
Sun = 0 Fri = 5