Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.IConvertible F# snippet #7763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IConvertible/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="iconvertible.fs" />
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions snippets/fsharp/System/IConvertible/Overview/iconvertible.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// <snippet1>
open System

/// Class that implements IConvertible
type Complex(x, y) =
member _.GetDoubleValue() =
sqrt (x * x + y * y)

interface IConvertible with
member _.GetTypeCode() =
TypeCode.Object

member _.ToBoolean(provider: IFormatProvider) =
(x <> 0.) || (y <> 0.)

member this.ToByte(provider: IFormatProvider) =
Convert.ToByte(this.GetDoubleValue())

member this.ToChar(provider: IFormatProvider) =
Convert.ToChar(this.GetDoubleValue())

member this.ToDateTime(provider: IFormatProvider) =
Convert.ToDateTime(this.GetDoubleValue())

member this.ToDecimal(provider: IFormatProvider) =
Convert.ToDecimal(this.GetDoubleValue())

member this.ToDouble(provider: IFormatProvider) =
this.GetDoubleValue()

member this.ToInt16(provider: IFormatProvider) =
Convert.ToInt16(this.GetDoubleValue())

member this.ToInt32(provider: IFormatProvider) =
Convert.ToInt32(this.GetDoubleValue())

member this.ToInt64(provider: IFormatProvider) =
Convert.ToInt64(this.GetDoubleValue())

member this.ToSByte(provider: IFormatProvider) =
Convert.ToSByte(this.GetDoubleValue())

member this.ToSingle(provider: IFormatProvider) =
Convert.ToSingle(this.GetDoubleValue())

member _.ToString(provider: IFormatProvider) =
$"({x}, {y})"

member this.ToType(conversionType: Type, provider: IFormatProvider) =
Convert.ChangeType(this.GetDoubleValue(), conversionType)

member this.ToUInt16(provider: IFormatProvider) =
Convert.ToUInt16(this.GetDoubleValue())

member this.ToUInt32(provider: IFormatProvider) =
Convert.ToUInt32(this.GetDoubleValue())

member this.ToUInt64(provider: IFormatProvider) =
Convert.ToUInt64(this.GetDoubleValue())

let writeObjectInfo testObject =
let typeCode = Type.GetTypeCode(testObject.GetType())
match typeCode with
| TypeCode.Boolean ->
printfn $"Boolean: {testObject}"
| TypeCode.Double ->
printfn $"Boolean: {testObject}"
| _ ->
printfn $"{typeCode}: {testObject}"

let testComplex = Complex(4, 7)

writeObjectInfo testComplex
writeObjectInfo (Convert.ToBoolean testComplex)
writeObjectInfo (Convert.ToDecimal testComplex)
writeObjectInfo (Convert.ToString testComplex)
// </snippet1>
1 change: 1 addition & 0 deletions xml/System/IConvertible.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.IConvertible/CPP/iconvertible.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/IConvertible/Overview/iconvertible.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/IConvertible/Overview/iconvertible.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.IConvertible/VB/iconvertible.vb" id="Snippet1":::

]]></format>
Expand Down