-
-
Save 0x53A/5c8dedc0be448a299386 to your computer and use it in GitHub Desktop.
F# using patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void sample1() | |
{ | |
FileStream fs = File.Create("x.txt"); | |
try | |
{ | |
fs.WriteByte(1); | |
} | |
finally | |
{ | |
IDisposable disposable = fs as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
} | |
public static void sample2() | |
{ | |
Console.WriteLine("some text"); | |
FileStream fs = File.Create("x.txt"); | |
try | |
{ | |
fs.WriteByte(2); | |
Console.WriteLine("some text"); | |
} | |
finally | |
{ | |
IDisposable disposable = fs as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
} | |
public static void sample3() | |
{ | |
Console.WriteLine("some text"); | |
FileStream fs = File.Create("x.txt"); | |
try | |
{ | |
fs.WriteByte(3); | |
} | |
finally | |
{ | |
IDisposable disposable = fs as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
Console.WriteLine("some text"); | |
} | |
public static void sample4() | |
{ | |
Console.WriteLine("some text"); | |
FileStream fs = File.OpenRead("x.txt"); | |
int num; | |
try | |
{ | |
num = fs.ReadByte(); | |
} | |
finally | |
{ | |
IDisposable disposable = fs as IDisposable; | |
if (disposable != null) | |
{ | |
disposable.Dispose(); | |
} | |
} | |
int firstByte = num; | |
Console.WriteLine("read:" + firstByte.ToString()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void sample1() | |
{ | |
using (FileStream fs = File.Create("x.txt")) | |
{ | |
fs.WriteByte(1); | |
} | |
} | |
public static void sample2() | |
{ | |
Console.WriteLine("some text"); | |
using (FileStream fs = File.Create("x.txt")) | |
{ | |
fs.WriteByte(2); | |
Console.WriteLine("some text"); | |
} | |
} | |
public static void sample3() | |
{ | |
Console.WriteLine("some text"); | |
using (FileStream fs = File.Create("x.txt")) | |
{ | |
fs.WriteByte(3); | |
} | |
Console.WriteLine("some text"); | |
} | |
public static void sample4() | |
{ | |
Console.WriteLine("some text"); | |
int num; | |
using (FileStream fs = File.OpenRead("x.txt")) | |
{ | |
num = fs.ReadByte(); | |
} | |
int firstByte = num; | |
Console.WriteLine("read:" + firstByte.ToString()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.IO | |
let sample1() = | |
use fs = File.Create("x.txt") | |
fs.WriteByte(byte 1) | |
let sample2() = | |
Console.WriteLine("some text") | |
use fs = File.Create("x.txt") | |
fs.WriteByte(byte 2) | |
Console.WriteLine("some text") | |
let sample3() = | |
Console.WriteLine("some text") | |
do | |
use fs = File.Create("x.txt") | |
fs.WriteByte(byte 3) | |
Console.WriteLine("some text") | |
let sample4() = | |
Console.WriteLine("some text") | |
let firstByte = | |
use fs = File.OpenRead("x.txt") | |
fs.ReadByte() | |
Console.WriteLine("read:" + firstByte.ToString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Methods | |
.method public static | |
void sample1 () cil managed | |
{ | |
// Method begins at RVA 0x2050 | |
// Code size 48 (0x30) | |
.maxstack 4 | |
.locals init ( | |
[0] class [mscorlib]System.IO.FileStream fs, | |
[1] class [FSharp.Core]Microsoft.FSharp.Core.Unit, | |
[2] class [mscorlib]System.IDisposable | |
) | |
IL_0000: nop | |
IL_0001: ldstr "x.txt" | |
IL_0006: call class [mscorlib]System.IO.FileStream [mscorlib]System.IO.File::Create(string) | |
IL_000b: stloc.0 | |
.try | |
{ | |
IL_000c: ldloc.0 | |
IL_000d: ldc.i4.1 | |
IL_000e: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8) | |
IL_0013: ldnull | |
IL_0014: stloc.1 | |
IL_0015: leave.s IL_002d | |
} // end .try | |
finally | |
{ | |
IL_0017: ldloc.0 | |
IL_0018: isinst [mscorlib]System.IDisposable | |
IL_001d: stloc.2 | |
IL_001e: ldloc.2 | |
IL_001f: brfalse.s IL_002a | |
IL_0021: ldloc.2 | |
IL_0022: callvirt instance void [mscorlib]System.IDisposable::Dispose() | |
IL_0027: ldnull | |
IL_0028: pop | |
IL_0029: endfinally | |
IL_002a: ldnull | |
IL_002b: pop | |
IL_002c: endfinally | |
} // end handler | |
IL_002d: ldloc.1 | |
IL_002e: pop | |
IL_002f: ret | |
} // end of method Program::sample1 | |
.method public static | |
void sample2 () cil managed | |
{ | |
// Method begins at RVA 0x209c | |
// Code size 68 (0x44) | |
.maxstack 4 | |
.locals init ( | |
[0] class [mscorlib]System.IO.FileStream fs, | |
[1] class [FSharp.Core]Microsoft.FSharp.Core.Unit, | |
[2] class [mscorlib]System.IDisposable | |
) | |
IL_0000: nop | |
IL_0001: ldstr "some text" | |
IL_0006: call void [mscorlib]System.Console::WriteLine(string) | |
IL_000b: ldstr "x.txt" | |
IL_0010: call class [mscorlib]System.IO.FileStream [mscorlib]System.IO.File::Create(string) | |
IL_0015: stloc.0 | |
.try | |
{ | |
IL_0016: ldloc.0 | |
IL_0017: ldc.i4.2 | |
IL_0018: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8) | |
IL_001d: ldstr "some text" | |
IL_0022: call void [mscorlib]System.Console::WriteLine(string) | |
IL_0027: ldnull | |
IL_0028: stloc.1 | |
IL_0029: leave.s IL_0041 | |
} // end .try | |
finally | |
{ | |
IL_002b: ldloc.0 | |
IL_002c: isinst [mscorlib]System.IDisposable | |
IL_0031: stloc.2 | |
IL_0032: ldloc.2 | |
IL_0033: brfalse.s IL_003e | |
IL_0035: ldloc.2 | |
IL_0036: callvirt instance void [mscorlib]System.IDisposable::Dispose() | |
IL_003b: ldnull | |
IL_003c: pop | |
IL_003d: endfinally | |
IL_003e: ldnull | |
IL_003f: pop | |
IL_0040: endfinally | |
} // end handler | |
IL_0041: ldloc.1 | |
IL_0042: pop | |
IL_0043: ret | |
} // end of method Program::sample2 | |
.method public static | |
void sample3 () cil managed | |
{ | |
// Method begins at RVA 0x20fc | |
// Code size 68 (0x44) | |
.maxstack 4 | |
.locals init ( | |
[0] class [mscorlib]System.IO.FileStream fs, | |
[1] class [FSharp.Core]Microsoft.FSharp.Core.Unit, | |
[2] class [mscorlib]System.IDisposable | |
) | |
IL_0000: nop | |
IL_0001: ldstr "some text" | |
IL_0006: call void [mscorlib]System.Console::WriteLine(string) | |
IL_000b: ldstr "x.txt" | |
IL_0010: call class [mscorlib]System.IO.FileStream [mscorlib]System.IO.File::Create(string) | |
IL_0015: stloc.0 | |
.try | |
{ | |
IL_0016: ldloc.0 | |
IL_0017: ldc.i4.3 | |
IL_0018: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8) | |
IL_001d: ldnull | |
IL_001e: stloc.1 | |
IL_001f: leave.s IL_0037 | |
} // end .try | |
finally | |
{ | |
IL_0021: ldloc.0 | |
IL_0022: isinst [mscorlib]System.IDisposable | |
IL_0027: stloc.2 | |
IL_0028: ldloc.2 | |
IL_0029: brfalse.s IL_0034 | |
IL_002b: ldloc.2 | |
IL_002c: callvirt instance void [mscorlib]System.IDisposable::Dispose() | |
IL_0031: ldnull | |
IL_0032: pop | |
IL_0033: endfinally | |
IL_0034: ldnull | |
IL_0035: pop | |
IL_0036: endfinally | |
} // end handler | |
IL_0037: ldloc.1 | |
IL_0038: pop | |
IL_0039: ldstr "some text" | |
IL_003e: call void [mscorlib]System.Console::WriteLine(string) | |
IL_0043: ret | |
} // end of method Program::sample3 | |
.method public static | |
void sample4 () cil managed | |
{ | |
// Method begins at RVA 0x215c | |
// Code size 85 (0x55) | |
.maxstack 4 | |
.locals init ( | |
[0] int32 firstByte, | |
[1] class [mscorlib]System.IO.FileStream fs, | |
[2] int32, | |
[3] class [mscorlib]System.IDisposable | |
) | |
IL_0000: nop | |
IL_0001: ldstr "some text" | |
IL_0006: call void [mscorlib]System.Console::WriteLine(string) | |
IL_000b: nop | |
IL_000c: ldstr "x.txt" | |
IL_0011: call class [mscorlib]System.IO.FileStream [mscorlib]System.IO.File::OpenRead(string) | |
IL_0016: stloc.1 | |
.try | |
{ | |
IL_0017: ldloc.1 | |
IL_0018: callvirt instance int32 [mscorlib]System.IO.Stream::ReadByte() | |
IL_001d: stloc.2 | |
IL_001e: leave.s IL_0036 | |
} // end .try | |
finally | |
{ | |
IL_0020: ldloc.1 | |
IL_0021: isinst [mscorlib]System.IDisposable | |
IL_0026: stloc.3 | |
IL_0027: ldloc.3 | |
IL_0028: brfalse.s IL_0033 | |
IL_002a: ldloc.3 | |
IL_002b: callvirt instance void [mscorlib]System.IDisposable::Dispose() | |
IL_0030: ldnull | |
IL_0031: pop | |
IL_0032: endfinally | |
IL_0033: ldnull | |
IL_0034: pop | |
IL_0035: endfinally | |
} // end handler | |
IL_0036: ldloc.2 | |
IL_0037: stloc.0 | |
IL_0038: ldstr "read:" | |
IL_003d: ldloca.s firstByte | |
IL_003f: constrained. [mscorlib]System.Int32 | |
IL_0045: callvirt instance string [mscorlib]System.Object::ToString() | |
IL_004a: call string [mscorlib]System.String::Concat(string, string) | |
IL_004f: call void [mscorlib]System.Console::WriteLine(string) | |
IL_0054: ret | |
} // end of method Program::sample4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment