Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Structs with Other Data Structures | Structs & Enumerators
C# Beyond Basics
course content

Contenuti del Corso

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

book
Structs with Other Data Structures

Since structs are essentially data types, we can use them for creating Arrays and Lists as well:

index.cs

index.cs

copy
1234567891011121314151617181920
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { // An array of 50 students Student[] studentsArr = new Student[50]; // A list of students List<Student> studentsList; } }

In a List or an Array, we would access a Student object's field using the following syntax:

index.cs

index.cs

copy
1
variableName[index].fieldName

For-example:

index.cs

index.cs

copy
1234567
// Array studentsArr[17].name = "Alex"; // List studentsList[27].age = 21; // Note: Both have the same syntax.

We can also loop through these arrays or lists to assign or access data. For-example the following code loops through a List of Students objects and calculates the average age:

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { // Creating an array Student[] students = new Student[7]; // Setting some data students[0].age = 18; students[1].age = 13; students[2].age = 16; students[3].age = 21; students[4].age = 30; students[5].age = 36; students[6].age = 20; int totalAge = 0; for (int i = 0; i < students.Length; i++) { totalAge += students[i].age; } // Formula for average is "sum of elements / number of elements" float averageAge = totalAge / students.Length; Console.WriteLine($"Average Age: {averageAge}"); } }

It is clear that here the Student struct acts like a data type. We can also use Student as a dictionary value as well. Following is an example of using Struct as a value in a dictionary:

index.cs

index.cs

copy
123456789101112131415161718192021222324
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { var studentsByID = new Dictionary<int, Student>(); Student student; student.name = "Thomas"; student.age = 36; studentsByID.Add(0, student); Console.WriteLine(studentsByID[0].name); } }
question mark

What is the correct syntax for accessing the score attribute of the 1st player?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

book
Structs with Other Data Structures

Since structs are essentially data types, we can use them for creating Arrays and Lists as well:

index.cs

index.cs

copy
1234567891011121314151617181920
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { // An array of 50 students Student[] studentsArr = new Student[50]; // A list of students List<Student> studentsList; } }

In a List or an Array, we would access a Student object's field using the following syntax:

index.cs

index.cs

copy
1
variableName[index].fieldName

For-example:

index.cs

index.cs

copy
1234567
// Array studentsArr[17].name = "Alex"; // List studentsList[27].age = 21; // Note: Both have the same syntax.

We can also loop through these arrays or lists to assign or access data. For-example the following code loops through a List of Students objects and calculates the average age:

index.cs

index.cs

copy
12345678910111213141516171819202122232425262728293031323334353637
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { // Creating an array Student[] students = new Student[7]; // Setting some data students[0].age = 18; students[1].age = 13; students[2].age = 16; students[3].age = 21; students[4].age = 30; students[5].age = 36; students[6].age = 20; int totalAge = 0; for (int i = 0; i < students.Length; i++) { totalAge += students[i].age; } // Formula for average is "sum of elements / number of elements" float averageAge = totalAge / students.Length; Console.WriteLine($"Average Age: {averageAge}"); } }

It is clear that here the Student struct acts like a data type. We can also use Student as a dictionary value as well. Following is an example of using Struct as a value in a dictionary:

index.cs

index.cs

copy
123456789101112131415161718192021222324
using System; using System.Collections.Generic; struct Student { public string name; public int age; } class Program { static void Main() { var studentsByID = new Dictionary<int, Student>(); Student student; student.name = "Thomas"; student.age = 36; studentsByID.Add(0, student); Console.WriteLine(studentsByID[0].name); } }
question mark

What is the correct syntax for accessing the score attribute of the 1st player?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt