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

book
Defining & Using a Structure

We define a structure using the following syntax:

index.cs

index.cs

copy
struct structureName
{
public datatype fieldName1;
public datatype fieldName2;
public datatype fieldName3;
...
}
1234567
struct structureName { public datatype fieldName1; public datatype fieldName2; public datatype fieldName3; ... }

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

index.cs

index.cs

copy
struct Student
{
public int id;
public float age;
public string name;
public string course;
public char grade;
}
12345678
struct Student { public int id; public float age; public string name; public string course; public char grade; }

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:

index.cs

index.cs

copy
structName variableName;
1
structName variableName;

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

index.cs

index.cs

copy
Student student1;
Student student2;
Student student3;
123
Student student1; Student student2; Student student3;

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:

index.cs

index.cs

copy
Console.WriteLine(student2.name);
1
Console.WriteLine(student2.name);

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 (=):

index.cs

index.cs

copy
student2.name = "Alex";
1
student2.name = "Alex";

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

index.cs

index.cs

copy
using 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';
123456789101112131415161718192021222324252627282930313233343536373839
using 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}."); } }

1. Which keyword is used for defining a structure?

2. What is the output of the following code?

question mark

Which keyword is used for defining a structure?

Select the correct answer

question mark

What is the output of the following code?

struct Student
{
public float age;
public string name;
}

static void Main(string[] args)
{
Student student1;
Student student2;

student1.name = "Tom";
student1.age = 20;

student2.name = "Abi";
student2.age = 21;

if (student1.age > student2.age)
{
Console.WriteLine($"{student1.name} is older than {student2.name}.");
}
else if (student1.age < student2.age)
{
Console.WriteLine($"{student1.name} is younger than {student2.name}.");
}
else
{
Console.WriteLine($"{student1.name} is the same age as {student2.name}.");
}
}

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt