Skip to content

Instantly share code, notes, and snippets.

@westonal
Created December 14, 2016 14:57
Show Gist options
  • Select an option

  • Save westonal/67dc612b0accece61673859fd2a426e8 to your computer and use it in GitHub Desktop.

Select an option

Save westonal/67dc612b0accece61673859fd2a426e8 to your computer and use it in GitHub Desktop.
Use the feature of calling .Add during initialization
using System;
using System.Collections;
public class Test
{
public static void Main()
{
new MyClass{1, "a", {2, "b"}}; //calls Add(1), Add("a"), Add(2, "a")
}
}
public sealed class MyClass : IEnumerable
{
public void Add(int i)
{
Console.WriteLine("Added an int " + i);
}
public void Add(String s)
{
Console.WriteLine("Added a string " + s);
}
public void Add(int i, String s)
{
Console.WriteLine("Added a pair (" + i + ", " + s + ")");
}
public IEnumerator GetEnumerator()
{
throw new NotSupportedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment