This project is read-only.
Define extension methods

Category:
Extension Methods
Description: Define extension methods
Code:
type System.String with 
member this.ToSecureString() =
let r = new System.Security.SecureString()
this |> Seq.iter (fun c -> r.AppendChar(c))
r
let ExtensionSample() =
let a = "password";
let secureString = a.ToSecureString()
Assert.AreEqual(secureString.Length, a.Length)

Execution Result:
both elements are equal

Define extension methods

Category:
Extension Methods
Description: Define extension methods to get version list from string
Code:
type System.String with 
member this.Versions =
if System.String.IsNullOrEmpty(this) then
failwith "empty or null string"
else
this.Split([|'.'|], System.StringSplitOptions.RemoveEmptyEntries)

let ExtensionSample2() =
let a = "1.2.4";
let versions = a.Versions
versions |> Seq.iter (fun n -> printfn "%s" n)
Assert.AreEqual(versions.Length, 3)
Assert.AreEqual(versions.[0], "1")

Execution Result:
1
2
4
both elements are equal
both elements are equal

Define C# extension method

Category:
Extension Methods
Description: Define C# extension methods
Code:
[]
let GetVersion(version:string) = version.Versions
# extension methods")>]
let ExtensionSample3() =
let version = "1.2.10";
let versions = version.Versions
printfn "version list = %A" versions
Assert.AreEqual(versions.Length, 3)
Assert.AreEqual(versions.[0], "1")

Execution Result:
version list = [|"1"; "2"; "10"|]
both elements are equal
both elements are equal

Last edited Sep 13, 2011 at 12:25 AM by ttliu2000, version 1

Comments

No comments yet.