Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Strukturen mit Anderen Datenstrukturen | Strukturen & Enumeratoren
C# Über die Grundlagen Hinaus

book
Strukturen mit Anderen Datenstrukturen

Da Strukturen im Wesentlichen Datentypen sind, können wir sie auch zur Erstellung von Arrays und Listen verwenden:

cs

index

copy
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;
}
}
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 einer Liste oder einem Array würden wir auf ein Feld eines Student-Objekts mit folgendem Syntax zugreifen:

cs

index

copy
variableName[index].fieldName
1
variableName[index].fieldName

Zum Beispiel:

cs

index

copy
// Array
studentsArr[17].name = "Alex";

// List
studentsList[27].age = 21;

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

Wir können auch durch diese Arrays oder Listen schleifen, um Daten zuzuweisen oder darauf zuzugreifen. Zum Beispiel durchläuft der folgende Code eine Liste von Students-Objekten und berechnet das Durchschnittsalter:

cs

index

copy
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}");
}
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}"); } }

Es ist klar, dass hier die Student-Struktur wie ein Datentyp fungiert. Wir können Student auch als Wert in einem Wörterbuch verwenden. Im Folgenden ist ein Beispiel für die Verwendung von Struct als Wert in einem Wörterbuch:

cs

index

copy
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);
}
}
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

Was ist die korrekte Syntax, um auf das score-Attribut des ersten Spielers zuzugreifen?

Player[] topThree = new Player[3];

topThree[0].score = 100;
topThree[1].score = 99;
topThree[2].score = 97;

var players = new Dictionary<string, Player[]>();
players.Add("Top Three", topThree);

Console.WriteLine(players["Top Three"][0].score);

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4
We use cookies to make your experience better!
some-alt