Struct Con Altre Strutture Dati
Poiché le struct sono fondamentalmente tipi di dati, è possibile utilizzarle anche per creare Array e Liste:
index.cs
1234567891011121314151617181920using 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 una List o in un Array, si accede al campo di un oggetto Student utilizzando la seguente sintassi:
index.cs
1variableName[index].fieldName
Ad esempio:
index.cs
1234567// Array studentsArr[17].name = "Alex"; // List studentsList[27].age = 21; // Note: Both have the same syntax.
È inoltre possibile scorrere questi array o liste per assegnare o accedere ai dati. Ad esempio, il seguente codice scorre una List di oggetti Students e calcola l'età media:
index.cs
12345678910111213141516171819202122232425262728293031323334353637using 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}"); } }
È evidente che qui la struct Student funge da tipo di dato. Possiamo anche utilizzare Student come valore in un dizionario. Di seguito è riportato un esempio di utilizzo di una Struct come valore in un dizionario:
index.cs
123456789101112131415161718192021222324using 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); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you show me an example of how to use a struct as a value in a dictionary?
How do I access or modify a struct stored in a dictionary?
What are some best practices for using structs in collections like arrays, lists, or dictionaries?
Fantastico!
Completion tasso migliorato a 2.04
Struct Con Altre Strutture Dati
Scorri per mostrare il menu
Poiché le struct sono fondamentalmente tipi di dati, è possibile utilizzarle anche per creare Array e Liste:
index.cs
1234567891011121314151617181920using 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 una List o in un Array, si accede al campo di un oggetto Student utilizzando la seguente sintassi:
index.cs
1variableName[index].fieldName
Ad esempio:
index.cs
1234567// Array studentsArr[17].name = "Alex"; // List studentsList[27].age = 21; // Note: Both have the same syntax.
È inoltre possibile scorrere questi array o liste per assegnare o accedere ai dati. Ad esempio, il seguente codice scorre una List di oggetti Students e calcola l'età media:
index.cs
12345678910111213141516171819202122232425262728293031323334353637using 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}"); } }
È evidente che qui la struct Student funge da tipo di dato. Possiamo anche utilizzare Student come valore in un dizionario. Di seguito è riportato un esempio di utilizzo di una Struct come valore in un dizionario:
index.cs
123456789101112131415161718192021222324using 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); } }
Grazie per i tuoi commenti!