A structure is a data type in C# that combines different data variables into a single unit. The keyword struct is used to create the structures.
Some of the salient points about structures are as follows:
A new structure requires a structure definition. It starts with the struct keyword and contains the structure name. Then the structure body is provided within curly braces. It may contain multiple data variables of different types.
The syntax of a structure definition is given as follows:
struct NameOfStructure { // Member variables }
In the above syntax, the keyword struct is used which is followed by the NameOfStructure. Then the structure body is provided.
A program that demonstrates structures is given as follows:
Source Code: Program that demonstrates structures in C#
using System; namespace StructureDemo { struct Student { public int rno; public string name; public double marks; }; public class Test { public static void Main(string[] args) { Student s1; Student s2; Student s3; s1.rno = 1; s1.name = "Mary"; s1.marks = 75.5; s2.rno = 2; s2.name = "Jack"; s2.marks = 98.0; s3.rno = 3; s3.name = "Susan"; s3.marks = 35.0; Console.WriteLine("Student Details"); Console.WriteLine("Roll Number = {0}", s1.rno); Console.WriteLine("Name = {0}", s1.name); Console.WriteLine("Marks = {0}", s1.marks); Console.WriteLine("Roll Number = {0}", s2.rno); Console.WriteLine("Name = {0}", s2.name); Console.WriteLine("Marks = {0}", s2.marks); Console.WriteLine("Roll Number = {0}", s3.rno); Console.WriteLine("Name = {0}", s3.name); Console.WriteLine("Marks = {0}", s3.marks); } } }
The output of the above program is as follows:
Student Details Roll Number = 1 Name = Mary Marks = 75.5 Roll Number = 2 Name = Jack Marks = 98 Roll Number = 3 Name = Susan Marks = 35
Now let us understand the above program.
The structure student is defined using the keyword struct. It contains 3 data variables representing the Roll number, Name and Marks of the student. The code snippet for this is as follows:
struct Student { public int rno; public string name; public double marks; };
The function main() contains 3 structure objects i.e. s1, s2 and s3. Then the values data are assigned to the structure objects. Finally, the student details are printed. The code snippet for this is as follows:
public static void Main(string[] args) { Student s1; Student s2; Student s3; s1.rno = 1; s1.name = "Mary"; s1.marks = 75.5; s2.rno = 2; s2.name = "Jack"; s2.marks = 98.0; s3.rno = 3; s3.name = "Susan"; s3.marks = 35.0; Console.WriteLine("Student Details"); Console.WriteLine("Roll Number = {0}", s1.rno); Console.WriteLine("Name = {0}", s1.name); Console.WriteLine("Marks = {0}", s1.marks); Console.WriteLine("Roll Number = {0}", s2.rno); Console.WriteLine("Name = {0}", s2.name); Console.WriteLine("Marks = {0}", s2.marks); Console.WriteLine("Roll Number = {0}", s3.rno); Console.WriteLine("Name = {0}", s3.name); Console.WriteLine("Marks = {0}", s3.marks); }
Structures are similar to classes but lighter versions of them. Some of the differences of structures and classes are given as follows:
Structures | Classes |
---|---|
Structures are a data type in C# that are of value type. | Classes are a data type in C# that are of reference type. |
There is no support for inheritance in structures. | There is support for inheritance in classes. |
Structures are either stored in a stack or they are inline. | Classes are stored on a heap. |
Structures can be instantiated without using the new operator. | Classes require the new operator for instantiation. |
Structures have default and non-parameter constructors that can be replaced by the user. | Classes support constructors and can have default constructors, parameterized constructors, copy constructors etc |
Structures are used for small data that is not intended to be modified after the structure is created. | Classes are used for complex data or data that is intended to be modified after the class is created. |
There cannot be abstract structures. | There are abstract classes that are used in inheritance. |
Avery good write-up. Please let me know what are the types of C# libraries used for AI development.
very satisfied!!
Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).
Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz
The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation
Leave a Reply
Your email address will not be published. Required fields are marked *