Defining & Using a Structure
You define a structure using the following syntax:
index.cs
1234567struct structureName { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }
For-example you can define a structure for storing some student data:
index.cs
12345678struct Student { public int id; public float age; public string name; public string course; public char grade; }
Now for storing data you need to create an instance (also called an object) using this structure. A structure is basically a custom datatype, and hence it can be treated as such. So for creating a new object of the structure, you simply use the following syntax:
index.cs
1structName variableName;
You can create multiple objects of the Student
class, all of which can store data:
index.cs
123Student student1; Student student2; Student student3;
Each object you created has the set of fields defined in the Student
structure and you can store and access data from those fields using the variableName.fieldName
syntax. For example you can access and output the name
field of student2
:
index.cs
1Console.WriteLine(student2.name);
When an empty object is created, the fields take up zero values depending upon their datatypes:
string
- empty string""
;char
- empty character''
;int
-0
;float
-0.0
;bool
-false
.
Therefore, the student2.name
will give an empty output.
You can store data in it using the assignment operator (=
):
index.cs
1student2.name = "Alex";
This way you can store data in all of the three objects:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839using System; struct Student { public int id; public float age; public string name; public string course; public char grade; } class ConsoleApp { static void Main(string[] args) { Student student1; student1.id = 1; student1.name = "Abigail"; student1.age = 21; student1.course = "C#"; student1.grade = 'A'; Console.WriteLine($"{student1.name} is {student1.age} years old."); Student student2; student2.id = 2; student2.name = "Alex"; student2.age = 22; student2.course = "C#"; student2.grade = 'B'; Console.WriteLine($"{student2.name} has a {student2.grade} in {student2.course}."); Student student3; student3.id = 3; student3.name = "Jennifer"; student3.age = 19; student3.course = "C#"; student3.grade = 'A'; Console.WriteLine($"{student3.name} studies {student3.course}."); } }
The expression variableName.fieldName
as a whole can be treated as a variable and hence it is valid to use it in string formatting like how it is used in the example above.
1. Which keyword is used for defining a structure?
2. What is the output of the following code?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.04
Defining & Using a Structure
Swipe to show menu
You define a structure using the following syntax:
index.cs
1234567struct structureName { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }
For-example you can define a structure for storing some student data:
index.cs
12345678struct Student { public int id; public float age; public string name; public string course; public char grade; }
Now for storing data you need to create an instance (also called an object) using this structure. A structure is basically a custom datatype, and hence it can be treated as such. So for creating a new object of the structure, you simply use the following syntax:
index.cs
1structName variableName;
You can create multiple objects of the Student
class, all of which can store data:
index.cs
123Student student1; Student student2; Student student3;
Each object you created has the set of fields defined in the Student
structure and you can store and access data from those fields using the variableName.fieldName
syntax. For example you can access and output the name
field of student2
:
index.cs
1Console.WriteLine(student2.name);
When an empty object is created, the fields take up zero values depending upon their datatypes:
string
- empty string""
;char
- empty character''
;int
-0
;float
-0.0
;bool
-false
.
Therefore, the student2.name
will give an empty output.
You can store data in it using the assignment operator (=
):
index.cs
1student2.name = "Alex";
This way you can store data in all of the three objects:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839using System; struct Student { public int id; public float age; public string name; public string course; public char grade; } class ConsoleApp { static void Main(string[] args) { Student student1; student1.id = 1; student1.name = "Abigail"; student1.age = 21; student1.course = "C#"; student1.grade = 'A'; Console.WriteLine($"{student1.name} is {student1.age} years old."); Student student2; student2.id = 2; student2.name = "Alex"; student2.age = 22; student2.course = "C#"; student2.grade = 'B'; Console.WriteLine($"{student2.name} has a {student2.grade} in {student2.course}."); Student student3; student3.id = 3; student3.name = "Jennifer"; student3.age = 19; student3.course = "C#"; student3.grade = 'A'; Console.WriteLine($"{student3.name} studies {student3.course}."); } }
The expression variableName.fieldName
as a whole can be treated as a variable and hence it is valid to use it in string formatting like how it is used in the example above.
1. Which keyword is used for defining a structure?
2. What is the output of the following code?
Thanks for your feedback!