In C #, the structure is determined by the keyword struct.
The keyword Struct allows you to define structures that contain different types of data. The structure can also include constructors, constants, fields, methods, properties, indexers, events, and more.
Access_Modifier struct structure_name
{
// Block of code
}
A structure is a type of value and a set of variables of different data types in a block. It almost looks like a class because they are both custom data types and both contain many different data types. C # allows you to use predefined data types. However, the user sometimes wants to define their own data types, also known as custom data types. Although it belongs to a value type, it can be changed as needed and is therefore also called a custom data type.
public struct Student
{
public string Name;
}
class Tech
{
static void Main(string[] args)
{
Student student;
student.Name = "shahzad";
Console.WriteLine(student.Name);
}
}
shahzad
In C # the user can copy one structure object to another with the "=" (Assignment) operator.
Structure_object = structure_object;
public struct Student
{
public string Name;
}
class Tech
{
static void Main(string[] args)
{
Student student;
Student student1;
student.Name = "shahzad";
student1 = student;
Console.WriteLine(student.Name);
Console.WriteLine(student1.Name);
}
}
shahzad
shahzad
Data members of the student structure begin to use student, and data member values can be copied from student to student1 using "=" (assignment operator).
C # allows one structure to be reported to another structure, and this concept is called nesting structure.
public struct Rollnumber
{
public int RNumber;
}
public struct Student
{
public string Name;
public Rollnumber rollnumber;
}
class Tech
{
static void Main(string[] args)
{
Student student;
student.Name = "shahzad";
student.rollnumber.RNumber = 15;
Console.WriteLine(student.Name);
Console.WriteLine(student.rollnumber.RNumber);
}
}
shahzad
15
Class | Structure |
---|---|
Classes are a kind of reference | value type |
All reference types are arranged in a smaller memory | stack memory used to assign the value type. |
The class has unlimited functionality | Struct has limited functionality. |
commonly used in large programs | Used in the small program |
Classes can be constructive or destructive | A structure does not have a constructor or destroyer without parameters |