Complete list of .NET conversion operators
Does anyone have a complete list of the conversion operators for VB/C# and how they differ?
So far I know about...
- value as type [C#]
- TryCast(value, type) [VB]
- Convert.ToXxx(value) [any]
- (type)value [C#]
- CType(value, type) [VB]
- DirectCast(value, type) [VB]
- CXxx(value) [VB]
- CTypeDynamic [any]
- implicit conversions when using Option Strict Off [VB]
- implicit conversions when using dynamic [C#]
- type value [F#]
- :> [F#]
- :?> [F#]
But of course just having the list isn't the same as knowing 开发者_如何学Gothe subtle differences between each one.
For F# versus C# on casts/conversions, see
What does this C# code look like in F#? (part one: expressions and statements)
for a short discussion of numeric conversions, boxing conversions, upcasts, and downcasts.
(Note that you list
type value
for F#, I presume you're talking about e.g.
int 'a'
but note that int
here is the name of a function in the F# library, rather than the name of a type. See the docs here; in general there is a function named T
for each primitive numeric type T
, and that function converts its argument to the destination type of the same name.)
Regarding implicit conversions in F#:
- There's
string
->PrintfFormat
(as part of the magic for typesafeprintf
) - There's upcasts at method calls arguments and property/array assignments that enables you to do e.g.
f(dog)
orperson.Pet <- dog
when anAnimal
is expected. This also works for known nominal types for collection literals, e.g.let controls : Control list = [button; form; window]
- There's a function-to-delegate conversion at method call arguments, which enable e.g.
new Thread(fun() -> ())
where the F# function is converted to theThreadStart
delegate. - There's
ref
tobyref
conversion at method call arguments, which enables you to pass aref
to e.g. anout
parameter.
I think those are it - there are very few implicit conversions in F#.
精彩评论