Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the C# compiler not fault code where a static method calls an instance method?

The following code has a static method, Foo(), calling an instance method, Bar():

public sealed class Example {     int count;      public static void Foo( dynamic x )     {         Bar(x);     }      void Bar( dynamic x )     {         count++;     } } 

It compiles without error* but generates a runtime binder exception at runtime. Removing the dynamic parameter to these methods causes a compiler error, as expected.

So why does having a dynamic parameter allow the code to be compiled? ReSharper doesn't show it as an error either.

Edit 1: *in Visual Studio 2008

Edit 2: added sealed since it's possible that a subclass could contain a static Bar(...) method. Even the sealed version compiles when it's not possible that any method other than the instance method could be called at runtime.

like image 665
Mike Scott Avatar asked Oct 11 '12 15:10

Mike Scott


People also ask

Why does the c exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Why does the letter c make two sounds?

In the Latin-based orthographies of many European languages, including English, a distinction between hard and soft ⟨c⟩ occurs in which ⟨c⟩ represents two distinct phonemes. The sound of a hard ⟨c⟩ often precedes the non-front vowels ⟨a⟩, ⟨o⟩ and ⟨u⟩, and is that of the voiceless velar stop, /k/ (as in car).

Does the letter c need to exist?

So the C is indeed a very important letter and has no reason to feel ashamed because it makes no sound on it own. Like a man and a women come together to make a unique "sound" in their marriage, so "C" marries "H" to produce a special combined sound. CH itself has three different sounds.

What c makes the CH sound?

The last syllable “CI” is spelt CI. So we get the soft “ch” sound at the end.


1 Answers

UPDATE: Below answer was written in 2012, before the introduction of C# 7.3 (May 2018). In What's new in C# 7.3, the section Improved overload candidates, item 1, it is explained how the overload resolution rules have changed so that non-static overloads are discarded early. So the below answer (and this entire question) has mostly only historical interest by now!


(Pre C# 7.3:)

For some reason, overload resolution always finds the best match before checking for static versus non-static. Please try this code with all static types:

class SillyStuff {   static void SameName(object o) { }   void SameName(string s) { }    public static void Test()   {     SameName("Hi mom");   } } 

This will not compile because the best overload is the one taking a string. But hey, that's an instance method, so compiler complains (instead of taking the second-best overload).

Addition: So I think the explanation of the dynamic example of the Original Question is that, in order to be consistent, when types are dynamic we also first find the best overload (checking only parameter number and parameter types etc., not static vs. non-static), and only then check for static. But that means that the static check has to wait until runtime. Hence the observed behavior.

Late addition: Some background on why they chose to do things this funny order can be inferred from this blog post by Eric Lippert.

like image 130
Jeppe Stig Nielsen Avatar answered Sep 20 '22 21:09

Jeppe Stig Nielsen