Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# implicit conversion and conversion from Object

There is a class:

public class Date
{
    private DateTime _dateTime;

    public Date(DateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public static implicit operator DateTime(Date d)
    {
        if (d == null)
            return default(DateTime);

        return d._dateTime;
    }

    public static implicit operator Date(DateTime dt)
    {
        return new Date(dt);
    }
}

So, this code works fine:

Date d = DateTime.Now;
DateTime dt=new Date(DateTime.Now);

But this code doesn't work and throws InvalidCastException "Specified cast is not valid.":

Date d = DateTime.Now;
var obj = (Object)d;
DateTime dt = (DateTime)obj;

For me, this is expected behavior, but is there any hack to make this particular code work? (Without cast obj to Date)

like image 891
DeeRain Avatar asked Mar 07 '14 15:03

DeeRain


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

User defined implicit/explicit conversion operators are an entirely compile time construct. The compiler is going to say something alone the lines of, "Hey, this person is trying to stick a Foo somewhere that expects a Bar. Is a Foo a Bar? No, it's not. Hmm...Oh, I see that Foo has defined an implicit conversion to Bar, I'll go stick in a call to that static conversion method, so that at runtime all of the types match up."

The runtime has no knowledge about implicit/explicit conversions. By the time the program has finished being compiled, those are just regular static methods, like any other static method. Since the compiler only sees conversions from Date to object and from object to Date, it never sees a place where it needs to add the implicit conversion calls. By the time the runtime gets to it, it only sees that the Date isn't a DateTime.

like image 97
Servy Avatar answered Nov 03 '22 00:11

Servy