Casting in C#

Results 1 to 2 of 2

Thread: Casting in C#

  1. #1
    Join Date
    Dec 1969

    Default Casting in C#

    I been coding in VB.NET for 2 years and recently switched to C#. VB.NET does a lot of tasks automatically. Since, C# is a strongly type language I don&#039;t understanding a few concepts. I am posting this thread for an answer to a question that I can&#039;t figure out. I don&#039;t know the rules to casting objects. There are times I cast an object like (int)string and other times I use Convert.ToInt32(sting). Is there a rule when to use one or the other? I get an error if I cast incorrectly. When this happens I try the other. I just want to know when to use one or the other.<BR><BR>Thanks,<BR><BR>James

  2. #2
    Join Date
    Dec 1969

    Default RE: Casting in C#

    Typically you use the cast when the types are similar...<BR><BR>double x = 2.3;<BR>double y;<BR><BR>y = (double) x;<BR><BR><BR>Sometimes it&#039;ll coerce it into a smaller data type. You can also do these with the generic objects.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts