Typecasting in C#

后端 未结 5 1126
Happy的楠姐
Happy的楠姐 2020-11-29 06:17

What is type casting, what\'s the use of it? How does it work?

5条回答
  •  长情又很酷
    2020-11-29 06:49

    See this or this:

    Because C# is statically-typed at compile time, after a variable is declared, it cannot be declared again or used to store values of another type unless that type is convertible to the variable's type

    ...

    However, you might sometimes need to copy a value into a variable or method parameter of another type. For example, you might have an integer variable that you need to pass to a method whose parameter is typed as double. Or you might need to assign a class variable to a variable of an interface type. These kinds of operations are called type conversions. In C#, you can perform the following kinds of conversions

提交回复
热议问题