DCSIMG
April 2011 - Posts - Shimmy on .NET

April 2011 - Posts

9
Comments

Tip: Casting in C# shortcut extension method by Shimmy

The C# cast operator (Type)value is pretty verbose. When you work with COM, extenal APIs, DataSet s or other not well-OOPed infrastractures, you often get an expected type as object (i.e. you know the runtime-type of the variable is string , but it's object at compile-time). Here is an example of many castings in one-line: IEntity entity = ( IEntity ) GetObjectById ( "Customer" , 34); string name = ( string ) entity . GetField ( "Name" ); int length = name . Length ; Or as...
Powered by Community Server (Commercial Edition), by Telligent Systems