user605334
user605334

Reputation:

how i can run infinite loop in c#

i want to run a infinite loop in c#. the structure i have is hirachical means every index have a list of same strucutre.

the thing look like

a person have their two child maybe the two child have two the loop is infinite how i can run them on aspx page.

any suggestion to do that

public struct mystruct{
public int ID;
public List<mystruct> childs
}

Upvotes: 1

Views: 519

Answers (2)

Karel
Karel

Reputation: 2212

CheckMyStruct(myStruct aStruct)
{
   doSomethingWithStruct(aStruct);
   if(aStruct.childs != null)
   { 
        foreach(myStruct aChild in aStruct.Childs)
            {
               CheckMyStruct(aChild);
            }
   }

}

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062492

A proper recursive graph is not really possible with structs; you would need to change to a class:

public class MyType{
    public int ID {get;set;}
    private readonly List<MyType> children = new List<MyType>();
    public List<MyType> Children {get{return children;}}
}

The problem is that otherwise virtually every time you try to mutate them to create the cycle, you get a copy, not the same instance.

Since this is a mutable entity type, it should be a class anyway.

Then even something as simple as:

var obj = new MyType();
obj.Children.Add(obj);

is a recursive graph.

Upvotes: 3

Related Questions