Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created August 22, 2012 02:12
Show Gist options
  • Save masaru-b-cl/3421483 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/3421483 to your computer and use it in GitHub Desktop.
コレクション初期化子のoverload解決順
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class MyCollection : IEnumerable
{
public void Add(object value)
{
Console.WriteLine("Call object overload");
}
public void Add(int value)
{
Console.WriteLine("Call int overload");
}
public void Add(string value)
{
Console.WriteLine("Call string overload");
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
var col = new MyCollection { "1", 1, null, (object)null };
// 実行結果
// Call string overload
// Call int overload
// Call string overload
// Call object overload
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment