Why were magic methods implemented in C#?

In C#, I started seeing all these magic methods popping up, without being backed up by an interface. Why was this chosen?

Let me explain.

Previously in C#, if an object implemented the IEnumerable interface, it would automatically be iterable by a foreach loop. That makes sense to me, since it’s backed up by an interface, and if I were to have my own Iterator function inside the class being iterated through, I could do that without worrying that it would magically mean something else.

Now, apparently, (not sure when), these interfaces are no longer required. It just needs to have the right naming conversions.

Another example is making any object awaitable by having a method named exactly GetAwaiter which has a few specific properties.

Why not make an interface like they did with IEnumerable or INotifyPropertyChanged to back this “magic” up statically?

More details on what I mean here:

http://blog.nem.ec/2014/01/01/magic-methods-c-sharp/

What are the pros and cons of magic methods, and is there anywhere online where I can find anything on why these decisions were made?

10

In general “magic methods” are used when it’s not possible to create an interface that would work the same way.

When foreach was first introduced in C# 1.0 (that behavior is certainly nothing recent), it had to use magic methods, because there were no generics. The options basically were:

  1. Use the non-generic IEnumerable and IEnumerator that work with objects, which means boxing value types. Since iterating something like a list of ints should be very fast and certainly shouldn’t create lots of garbage boxed values, this is not a good choice.

  2. Wait for generics. This would probably mean delaying .Net 1.0 (or at least foreach) by more than 3 years.

  3. Use magic methods.

So, they chose option #3 and it stayed with us for backwards compatibility reasons, even though since .Net 2.0, requiring IEnumerable<T> would have worked too.


Collection initializers can look different on each collection type. Compare List<T>:

public void Add(T item)

and Dictionary<TKey, TValue>:

public void Add(TKey key, TValue value)

You can’t have a single interface that would support only the first form for List<T> and only the second for Dictionary<TKey, TValue>.


LINQ methods are usually implemented as extension methods (so that there can be just a single implementation of e.g. LINQ to Object for all types that implement IEnumerable<T>), which means it’s not possible to use an interface.


For await, The GetResult() method can return either void or some type T. Again, you can’t have a single interface that can handle both. Though await is partially interface-based: the awaiter has to implement INotifyCompletion and can also implement ICriticalNotifyCompletion.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật