Structures in C#
Structures in C#
In C #, a structure is a data type of a value type. It allows you to construct a single variable keeping data related to different data types. For the construction of a system, the structure keyword is used.
To represent a record, structures are used. Suppose, in a library, you want to keep track of your books. You may want to track each book with the following attributes.
Title
Author
Subject
Subject ID
Define a Structure
You have to use the Struct Statement to describe a structure. The structure declaration describes a new category of data, with your programme having more than one participant.
Here is the way you can announce the structure of the subjects , for example.
struct subjects {
public string English ;
public string chemistry ;
public string maths ;
public int subjects _id;
};
using System;
struct subjects {
public string English ;
public string chemistry ;
public string maths ;
public int subjects _id;
};
};
public class testStructure {
public static void Main(string[] args) {
Books subject1;
Books Subject2;
Subject1.title = "Chemistry ";
Subject1.author = "RV Sen";
Subject .subject = "Organic Chemistry ";
1.subject_id =78;
Subject 2.title = "Maths ";
Subject2.author = "HC Verma ";
Subject 2.subject = "Integration ";
Subject 2.subject_id =88;
Console.WriteLine( "Subject 1 title : {0}", subject1.title);
Console.WriteLine("Subject 1 author : {0}", Subject1.author);
Console.WriteLine(“subject 1 subject : {0}", Subject1.subject);
Console.WriteLine("Subject 1 subject_i
Console.ReadKey();
}
}
C # Structures features
You used a basic framework called Books already. Structures in C # vary greatly from those in conventional C or C++. The structures of C # have the following characteristics-
Methods, fields, indexers, properties, operator methods, and events can be available for structures.
Structures may have constructors that are specified, but not destructors. However, for a structure, you can not specify a default function Object() { [native code] }. The default function Object() { [native code] } is specified automatically and can not be altered.
Structures do not, unlike groups, inherit other structures or groups.
Structures for other structures or groups can not be used as a foundation.
One or more interfaces can be implemented by a structure.
It is not possible to define structure members as abstract, virtual, or safe.
If you use the New Operator to create a structural entity, it is generated and the appropriate function Object() { [native code] } is named. Without using the new operator, structures can be instantiated, unlike classes. If the current operator is not used, the new operator is used.
Summary :
1.The structure may include architects, constants, fields, strategies, properties, indexers, operators, events, and types of nests.
2. A parameterless function Object() { [native code] } or a destructor should not be used in the framework.
3. Structure, like class, can impose interfaces.
4. Another structure or class does not inherit a structure, and it can not be the basis of a class.
5. You can not define the members of the system as abstract, enclosed, virtual or covered.