Created
December 14, 2016 14:57
-
-
Save westonal/67dc612b0accece61673859fd2a426e8 to your computer and use it in GitHub Desktop.
Use the feature of calling .Add during initialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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