Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I just saw a question asked by someone on a different to which no one has porivded a staisfacotry answer, and just out of curiousity i also wanted to know why is it compile time legal, but ofcourse it would fail at run-time, here is the code:

C#
public class DummyClassFactory
    {
        public Object GetDummyClassInstance()
        { 
            var dummyClass = new DummyClass();
			
            switch (DateTime.Now.DayOfWeek)
            {
                // Starting on monday.
                case DayOfWeek.Monday:
                    return (IDisposable)dummyClass;
                
               
                case DayOfWeek.Thursday:
                    return (IServiceProvider)dummyClass;
                case DayOfWeek.Friday:
                    return (IEquatable<List<Single>>)dummyClass;
                case DayOfWeek.Saturday:
                    return (IFormattable)dummyClass;
                
            }
            // return (DummyClassFactory)dummyClass; 
            return dummyClass;
        }
    }

    public class DummyClass { }
    public interface IDummyClass { }


Can someone please shed some light, why is it so?

Here is the demo fiddle : .NET DEMO Fiddle[^]

Thanks,
Ehsan Sajjad

What I have tried:

C#
public class DummyClassFactory
    {
        public Object GetDummyClassInstance()
        { 
            var dummyClass = new DummyClass();
			
            switch (DateTime.Now.DayOfWeek)
            {
                // Starting on monday.
                case DayOfWeek.Monday:
                    return (IDisposable)dummyClass;
                
               
                case DayOfWeek.Thursday:
                    return (IServiceProvider)dummyClass;
                case DayOfWeek.Friday:
                    return (IEquatable<List<Single>>)dummyClass;
                case DayOfWeek.Saturday:
                    return (IFormattable)dummyClass;
                
            }
            // return (DummyClassFactory)dummyClass; 
            return dummyClass;
        }
    }

    public class DummyClass { }
    public interface IDummyClass { }
Posted
Updated 25-Sep-17 6:02am
Comments
Graeme_Grant 25-Sep-17 22:49pm    
This will better explain what you are seeing: Difference between Early Binding and Late binding[^]

It's a bit complicated, but I'll try to explain...

Remember that a class can derive from a single base class (concrete or abstract), but can also inherit multiple interfaces. So descendant classes can implement interfaces that the original base class did not.
C#
class MyBase{}
class MyDerived : MyBase {}
class MyOtherDerived : MyDerived, IInterface1, IInterface2 {}

Since a variable of type MyBase could contain any type of object based on MyBase, it could conceivably contain a MyOtherDerived class instance - which supports two interfaces, so it can be cast to either (with the appropriate runtime checks to make sure it works).
That's all simple enough, and you could say "No derived class implements IDontKnowWhatToCallIt, so you shouldn't be able to cast to it and you should get a compiler error".

But ... the system doesn't know that. It's entirely possible that a different assembly altogether does create a class derived from MyBase and that does implement the IDontKnowWhatToCallIt Interface - so it can't create a compile time error without seriously mucking up your code!

If your base class is sealed it can check - and it does, and will raise a compiler error. But for derivable classes, it has to err on the "safe" side and let the cast through and rely on the run time checks to throw an exception.
 
Share this answer
 
Comments
Wendelius 25-Sep-17 12:03pm    
Fast fingers you have :)

A lot more text in the same time as I wrote only few lines.
[no name] 25-Sep-17 12:10pm    
True.
OriginalGriff 25-Sep-17 12:15pm    
A legacy of a computer typing course, many, many years ago! ("Mavis Beacon Teaches Typing", back in the DOS days!)
[no name] 25-Sep-17 12:19pm    
Sir answer my question if you have time to answer.
OriginalGriff 25-Sep-17 12:25pm    
Which one? You have 20 of them...
As far as I understand the situation, your class can be extended to support the mentioned interfaces in the code for example by inheritance. So the compiler is not sure if the cast would always fail.

Things change dramatically if you seal the class:
C#
public sealed class DummyClass { }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900