Click here to Skip to main content
15,892,269 members
Home / Discussions / C#
   

C#

 
QuestionGeneric method: Cast an object to a return type T Pin
Spunky Coder8-Apr-09 23:26
Spunky Coder8-Apr-09 23:26 
AnswerRe: Generic method: Cast an object to a return type T Pin
0x3c08-Apr-09 23:37
0x3c08-Apr-09 23:37 
GeneralRe: Generic method: Cast an object to a return type T Pin
_groo_8-Apr-09 23:49
_groo_8-Apr-09 23:49 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder8-Apr-09 23:51
Spunky Coder8-Apr-09 23:51 
GeneralRe: Generic method: Cast an object to a return type T Pin
_groo_9-Apr-09 0:01
_groo_9-Apr-09 0:01 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 0:14
Spunky Coder9-Apr-09 0:14 
AnswerRe: Generic method: Cast an object to a return type T Pin
Rob Philpott8-Apr-09 23:42
Rob Philpott8-Apr-09 23:42 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 0:01
Spunky Coder9-Apr-09 0:01 
Rob Philpott wrote:
T could be anything and so casting from SampleColl to T may very well not work


I agree with you...but at the calling site itself I have confined T for only two data types (as per the requirement). So in my code there is never a chance that T is a value type Smile | :)


Rob Philpott wrote:
private static T InitData<t>(T dummyColl, int value) where T: class


I've used this but frankly I don't understand its usage...even with that 'where' clause I'm able to do something like this without any compiler error.

int i=2;
(T)Convert.ChangeType(i, dummyColl.GetType())


So whether I use this constraint or not it throws the error at runtime only.Can you please let me know why is it happening?

"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)

GeneralRe: Generic method: Cast an object to a return type T Pin
Rob Philpott9-Apr-09 0:12
Rob Philpott9-Apr-09 0:12 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 0:43
Spunky Coder9-Apr-09 0:43 
GeneralRe: Generic method: Cast an object to a return type T Pin
Rob Philpott9-Apr-09 0:46
Rob Philpott9-Apr-09 0:46 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 1:08
Spunky Coder9-Apr-09 1:08 
GeneralRe: Generic method: Cast an object to a return type T Pin
_groo_9-Apr-09 1:47
_groo_9-Apr-09 1:47 
QuestionAssignability of output parameters in C# Pin
dojohansen8-Apr-09 22:55
dojohansen8-Apr-09 22:55 
AnswerRe: Assignability of output parameters in C# Pin
Rob Philpott8-Apr-09 23:36
Rob Philpott8-Apr-09 23:36 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen9-Apr-09 0:21
dojohansen9-Apr-09 0:21 
AnswerRe: Assignability of output parameters in C# Pin
S. Senthil Kumar9-Apr-09 4:13
S. Senthil Kumar9-Apr-09 4:13 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 0:52
dojohansen1-Jul-09 0:52 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 3:52
dojohansen1-Jul-09 3:52 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 5:55
S. Senthil Kumar1-Jul-09 5:55 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 6:05
dojohansen1-Jul-09 6:05 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 18:04
S. Senthil Kumar1-Jul-09 18:04 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 22:25
dojohansen1-Jul-09 22:25 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 23:02
S. Senthil Kumar1-Jul-09 23:02 
GeneralDiscussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 22:19
Cracked-Down8-Apr-09 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.