|
|
От: |
Sinclair
|
https://github.com/evilguest/ |
| Дата: | 25.12.25 15:18 | ||
| Оценка: | |||
Ок. Я, наверное, плохо объясняю очевидные лично мне вещи.S>Так как Next для последнего и Previous для первого будет null
class Node
{
public required Node Next { get; set; }
public required Node Previous { get; set; }
public string Name {get; init;}
public Node(string name) => Name = name;
}
public static IEnumerable<string> Iterate(Node start) {
var current = start;
do {
yield return current.Name;
current = current.Next;
} while(current != start);
}var node1 = new Node("Hello");
var node2 = new Node("World") { Next = node1, Previous = node1 };
foreach(var n in Iterate(node1))
Console.WriteLine(n);Compile time error: Required member 'Program.Node.Next' must be set in the object initializer or attribute constructor.
Compile time error: Required member 'Program.Node.Previous' must be set in the object initializer or attribute constructor.
var node1 = new Node("Hello") { Next = null!, Previous = null! };
var node2 = new Node("World") { Next = node1, Previous = node1) };
node1.Previous = node2;
foreach(var n in Iterate(node1)
Console.WriteLine(n);S>#продвинутый nullable enable
S> var obj1 = new SomeType1();
S> var obj2 = new SomeType2();
S>obj1.Prop = obj2;
S>obj2.Prop = obj1;
S>// Local functions are allowed
S>void Connect() => obj1.Child = obj2;
S>Connect();
S>#продвинутый nullable disable
S>