Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Defining & Using a Structure | Structs & Enumerators
C# Beyond Basics

Defining & Using a StructureDefining & Using a Structure

We define a structure using the following syntax:

cs

index.cs

For-example we can define a structure for storing some student data:

cs

index.cs

Now for storing data we 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, we simply use the following syntax:

cs

index.cs

We can create multiple objects of the Student class, all of which can store data:

cs

index.cs

Each object we created has the set of fields defined in the Student structure and we can store and access data from those fields using the variableName.fieldName syntax. For example we can access and output the name field of student2:

cs

index.cs

When an empty object is created, the fields take up zero values depending upon their datatypes:

  1. string - empty string ""
  2. char - empty character ''
  3. int - 0
  4. float - 0.0
  5. bool - false

Therefore the student2.name will give an empty output.

We can store data in it using the assignment operator (=):

cs

index.cs

This way we can store data in all of the three objects:

cs

index.cs

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?

Which keyword is used for defining a structure?

Виберіть правильну відповідь

What is the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 2
course content

Зміст курсу

C# Beyond Basics

Defining & Using a StructureDefining & Using a Structure

We define a structure using the following syntax:

cs

index.cs

For-example we can define a structure for storing some student data:

cs

index.cs

Now for storing data we 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, we simply use the following syntax:

cs

index.cs

We can create multiple objects of the Student class, all of which can store data:

cs

index.cs

Each object we created has the set of fields defined in the Student structure and we can store and access data from those fields using the variableName.fieldName syntax. For example we can access and output the name field of student2:

cs

index.cs

When an empty object is created, the fields take up zero values depending upon their datatypes:

  1. string - empty string ""
  2. char - empty character ''
  3. int - 0
  4. float - 0.0
  5. bool - false

Therefore the student2.name will give an empty output.

We can store data in it using the assignment operator (=):

cs

index.cs

This way we can store data in all of the three objects:

cs

index.cs

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?

Which keyword is used for defining a structure?

Виберіть правильну відповідь

What is the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 2
some-alt