none
What is Type Casting in C#? RRS feed

  • Question

  • So, I was looking up a tutorial online and it defined a thing called Type Casting. So i looked it up and something like this showed up,

    int x = (int) 20.9;
    float num1 = (float) 20.9

    It also said that If we want to assign a number like 20.9 to a float or decimal we need to add the 'f' and 'm' suffixes respectively, What does this Mean? And When would I use Type Casting?

    Thank you

    Thursday, February 28, 2019 1:43 PM

Answers

All replies

  • Hello,

    Please read the documentation on type casting which is about a four minute time to read

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Frozen_Nova Thursday, March 7, 2019 1:19 PM
    Thursday, February 28, 2019 2:20 PM
    Moderator
  • You can see : Type-Cast Conversions
    Thursday, February 28, 2019 2:22 PM
  • Hi Frozen_Nova,

    Thank you for posting here.

    According to your description, you want to know more information about type casting.

    The following link provides some simple code and it will be helpful for you.

    https://www.geeksforgeeks.org/c-sharp-type-casting/

    Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.

    Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Hope my advice could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 1, 2019 5:45 AM
    Moderator