Skip to content

Instantly share code, notes, and snippets.

@nastajus
Created July 15, 2014 00:08
Show Gist options
  • Save nastajus/07b44016f5fc16d49396 to your computer and use it in GitHub Desktop.
Save nastajus/07b44016f5fc16d49396 to your computer and use it in GitHub Desktop.
Orbit-like over simplification of groups to teach me recursion at my request.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Mister Main tells grandpa GO TO MOFO EUROPE.
Group grandpa = new Group("grandpa");
//grandpa.PrintName();
grandpa.AddChild("rebecca");
//grandpa.groups["rebecca"].PrintName();
grandpa.groups["rebecca"].AddChild("erica");
grandpa.GoToEurope();
Console.ReadLine();
}
}
public class Group {
string name;
public Dictionary<string, Group> groups;
public Group(string name)
{
this.name = name;
groups = new Dictionary<string, Group>();
var variable = groups.Values.ToList();
}
public void AddChild( string child )
{
groups.Add(child, new Group(child)); //key and group name are same, to be synced. MAGIC!!
}
public void PrintName()
{
Console.WriteLine(name);
}
public void GoToEurope()
{
//either or
//foreach (string name in groups.Keys)
//{
// Group value = groups[name];
//}
// if has child //
foreach (Group group in groups.Values) //BASE CASE IMPLICIT
{
group.GoToEurope();
}
Console.WriteLine(name + " went to Europe.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment