Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Casting errors should be caught during compiling, right?
Message
 
À
20/11/2004 19:20:37
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 1.1
OS:
Windows XP SP2
Divers
Thread ID:
00963257
Message ID:
00963330
Vues:
7
>Type safety and casting as I thought are closely coupled and if a type can't be casted to another type it should be caught during compilation.

Depends. Upcasting from a derived type to a parent type is always safe because you are going from the more special case to the the more general case.

Downcasting from a superclass to a subclass is inherantly unsafe because you are telling the compiler that the object is really a certain type. The compiler has no way of knowing. If its really not that type then a runtime exception occurs.

>Or to put it another way could a method return some other type than what it's instructed to do?

Yes and no. A method signature that says the method will return a DataSet will not compile if you try to return a DataReader - it must return an object of the declared type or any subtype. But, you can always take it to the logical extreme. Declare the method to return an object. Then you can return anything from the method. Object.System is like a variant.

>To reproduce what I mean try MSDN code sample under:
>System.Windows.Forms\Binding class

To what are you referring? If you are referring to
bmCustomers = this.BindingContext [ds, "Customers"];

this is simple upcasting from a base type to an abstract parent type.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform