C# Tutorial

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:Structures are created using the struct keyword.Interfaces can be implemented using interfaces.Structures have default, no parameter constructors that cannot be replaced. They initialize all the fields to default valuesThere is no destructor definition for structures.There is no concept of inheritance in structures.There is no specification for structure members such as virtual, protected, abstract etc.Structure DefinitionA 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 = 35Now 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 vs ClassesStructures are similar to classes but lighter versions of them. Some of the differences of structures and classes are given as follows:StructuresClassesStructures 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 etcStructures 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.

Structures in C#

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:

1. Structures are created using the struct keyword.
2. Interfaces can be implemented using interfaces.
3. Structures have default, no parameter constructors that cannot be replaced. They initialize all the fields to default values
4. There is no destructor definition for structures.
5. There is no concept of inheritance in structures.
6. There is no specification for structure members such as virtual, protected, abstract etc.

Structure Definition

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 vs Classes

Structures are similar to classes but lighter versions of them. Some of the differences of structures and classes are given as follows:

StructuresClasses
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.

Kshitiz

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

Ed

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

Swift Tutorial

Introduction to Swift Tutorial
13542
Swift Tutorial

Introduction to Swift Tutorial

R Programming

Python Tutorial
5394
Python Tutorial

Python Tutorial