Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# Convertion Pin
parths26-Mar-13 21:45
parths26-Mar-13 21:45 
AnswerRe: C# Convertion Pin
Midnight Ahri26-Mar-13 22:08
Midnight Ahri26-Mar-13 22:08 
GeneralRe: C# Convertion Pin
harold aptroot27-Mar-13 0:28
harold aptroot27-Mar-13 0:28 
AnswerRe: C# Convertion Pin
OriginalGriff26-Mar-13 21:33
mveOriginalGriff26-Mar-13 21:33 
GeneralRe: C# Convertion Pin
Richard MacCutchan26-Mar-13 22:54
mveRichard MacCutchan26-Mar-13 22:54 
GeneralRe: C# Convertion Pin
OriginalGriff26-Mar-13 22:57
mveOriginalGriff26-Mar-13 22:57 
GeneralRe: C# Convertion Pin
Keith Barrow26-Mar-13 23:51
professionalKeith Barrow26-Mar-13 23:51 
AnswerRe: C# Convertion Pin
V.26-Mar-13 22:29
professionalV.26-Mar-13 22:29 
just to complete the information.

casting is often used in boxing/unboxing objects and is often an expensive operation.
I didn't test, but I think that

C#
txtValue == (string)a;


is a heavier operation than

C#
txtValue = a.ToString();


with the additional problem that you get an exception if it cannot be cast. try/catch is also heavier then if/else. (so if(a != null) { txtValue = a.ToString() } is better than using try/catch. (this is what I call defensive programming)

If you write your own class and override the ToString method, you can do some fancy stuff. eg. if you do NOT override, ToString will probably return something like "thenamespace.theclassname". If you do override it you can make a representation of your class like eg a JSON object or an XML string (or whatever format takes your fancy) with all property values in there. (serializing objects)

C#
txtValue = Convert.ToString(a);


will convert the object to string, but in a safer way. In fact I rarely use the casting (option 1), only if I need to cast from a boxed object. I only use option 2 when I want to do fancy stuff with my class (because of the null reference exception problem.) I use option 3 in most normal cases.

hope this helps.

QuestionLinq To SQL Left Join Pin
Kevin Marois26-Mar-13 17:01
professionalKevin Marois26-Mar-13 17:01 
AnswerRe: Linq To SQL Left Join Pin
Simon_Whale26-Mar-13 23:19
Simon_Whale26-Mar-13 23:19 
Questionserialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:20
JudyL_MD26-Mar-13 14:20 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:22
JudyL_MD26-Mar-13 14:22 
AnswerRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 14:39
mveDave Kreskowiak26-Mar-13 14:39 
QuestionRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 15:13
JudyL_MD26-Mar-13 15:13 
AnswerRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 15:34
professionalMycroft Holmes26-Mar-13 15:34 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 16:21
JudyL_MD26-Mar-13 16:21 
GeneralRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 16:49
professionalMycroft Holmes26-Mar-13 16:49 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 17:02
mveDave Kreskowiak26-Mar-13 17:02 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:42
JudyL_MD27-Mar-13 1:42 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak27-Mar-13 1:48
mveDave Kreskowiak27-Mar-13 1:48 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:52
JudyL_MD27-Mar-13 1:52 
Questiondevexpress winforms controls Pin
naseer86126-Mar-13 11:16
naseer86126-Mar-13 11:16 
AnswerRe: devexpress winforms controls Pin
Dave Kreskowiak26-Mar-13 11:43
mveDave Kreskowiak26-Mar-13 11:43 
AnswerRe: devexpress winforms controls Pin
Pete O'Hanlon26-Mar-13 12:09
mvePete O'Hanlon26-Mar-13 12:09 
AnswerRe: devexpress winforms controls Pin
NotPolitcallyCorrect26-Mar-13 14:45
NotPolitcallyCorrect26-Mar-13 14:45 

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.