diff --git a/snippets/cpp/System/String/.ctor/sbyte_ctor1.cpp b/snippets/cpp/System/String/.ctor/sbyte_ctor1.cpp
new file mode 100644
index 00000000000..f178c345ffc
--- /dev/null
+++ b/snippets/cpp/System/String/.ctor/sbyte_ctor1.cpp
@@ -0,0 +1,20 @@
+// SByte_Ctor1.cpp : Defines the entry point for the console application.
+//
+
+//#include "stdafx.h"
+
+//
+using namespace System;
+
+void main()
+{
+ char chars[] = { 'a', 'b', 'c', 'd', '\x00' };
+
+ char* charPtr = chars;
+ String^ value = gcnew String(charPtr);
+
+ Console::WriteLine(value);
+}
+// The example displays the following output:
+// abcd
+//
diff --git a/snippets/csharp/System/Double/Overview/Program.cs b/snippets/csharp/System/Double/Overview/Program.cs
new file mode 100644
index 00000000000..170e0a43274
--- /dev/null
+++ b/snippets/csharp/System/Double/Overview/Program.cs
@@ -0,0 +1 @@
+Example9.Run();
diff --git a/snippets/csharp/System/Double/Overview/Project.csproj b/snippets/csharp/System/Double/Overview/Project.csproj
new file mode 100644
index 00000000000..274b5ecb6d6
--- /dev/null
+++ b/snippets/csharp/System/Double/Overview/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Double/Overview/precisionlist1.cs b/snippets/csharp/System/Double/Overview/precisionlist1.cs
new file mode 100644
index 00000000000..ce19a1e9735
--- /dev/null
+++ b/snippets/csharp/System/Double/Overview/precisionlist1.cs
@@ -0,0 +1,17 @@
+//
+using System;
+
+public class Example9
+{
+ public static void Run()
+ {
+ double value1 = 1 / 3.0;
+ float sValue2 = 1 / 3.0f;
+ double value2 = (double)sValue2;
+ Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
+ }
+}
+
+// The example displays the following output:
+// 0.33333333333333331 = 0.3333333432674408: False
+//
diff --git a/snippets/csharp/System/Double/Overview/source.cs b/snippets/csharp/System/Double/Overview/source.cs
index 709b8021499..fdaf6981acd 100644
--- a/snippets/csharp/System/Double/Overview/source.cs
+++ b/snippets/csharp/System/Double/Overview/source.cs
@@ -213,6 +213,7 @@ public double Celsius {
}
//
}
+
namespace Snippets5 {
//
public class Temperature {
@@ -257,6 +258,7 @@ public double Celsius {
}
//
}
+
namespace Snippets6 {
//
public class Temperature {
@@ -301,6 +303,7 @@ public double Celsius {
}
//
}
+
namespace Snippets7 {
//
public class Temperature {
@@ -345,6 +348,7 @@ public double Celsius {
}
//
}
+
namespace Snippets8 {
//
public class Temperature {
diff --git a/snippets/csharp/System/FormatException/Overview/FormatExample2.cs b/snippets/csharp/System/FormatException/Overview/FormatExample2.cs
new file mode 100644
index 00000000000..925589ccda8
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/FormatExample2.cs
@@ -0,0 +1,77 @@
+//
+using System;
+
+public class TestFormatter
+{
+ public static void Main()
+ {
+ int acctNumber = 79203159;
+ Console.WriteLine(String.Format(new CustomerFormatter(), "{0}", acctNumber));
+ Console.WriteLine(String.Format(new CustomerFormatter(), "{0:G}", acctNumber));
+ Console.WriteLine(String.Format(new CustomerFormatter(), "{0:S}", acctNumber));
+ Console.WriteLine(String.Format(new CustomerFormatter(), "{0:P}", acctNumber));
+ try {
+ Console.WriteLine(String.Format(new CustomerFormatter(), "{0:X}", acctNumber));
+ }
+ catch (FormatException e) {
+ Console.WriteLine(e.Message);
+ }
+ }
+}
+
+public class CustomerFormatter : IFormatProvider, ICustomFormatter
+{
+ public object GetFormat(Type formatType)
+ {
+ if (formatType == typeof(ICustomFormatter))
+ return this;
+ else
+ return null;
+ }
+
+ public string Format(string format,
+ object arg,
+ IFormatProvider formatProvider)
+ {
+ if (! this.Equals(formatProvider))
+ {
+ return null;
+ }
+ else
+ {
+ if (String.IsNullOrEmpty(format))
+ format = "G";
+
+ string customerString = arg.ToString();
+ if (customerString.Length < 8)
+ customerString = customerString.PadLeft(8, '0');
+
+ format = format.ToUpper();
+ switch (format)
+ {
+ case "G":
+ return customerString.Substring(0, 1) + "-" +
+ customerString.Substring(1, 5) + "-" +
+ customerString.Substring(6);
+ case "S":
+ return customerString.Substring(0, 1) + "/" +
+ customerString.Substring(1, 5) + "/" +
+ customerString.Substring(6);
+ case "P":
+ return customerString.Substring(0, 1) + "." +
+ customerString.Substring(1, 5) + "." +
+ customerString.Substring(6);
+ default:
+ throw new FormatException(
+ String.Format("The '{0}' format specifier is not supported.", format));
+ }
+ }
+ }
+}
+// The example displays the following output:
+// 7-92031-59
+// 7-92031-59
+// 7/92031/59
+// 7.92031.59
+// The 'X' format specifier is not supported.
+//
diff --git a/snippets/csharp/System/FormatException/Overview/Project.csproj b/snippets/csharp/System/FormatException/Overview/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/FormatException/Overview/example2.cs b/snippets/csharp/System/FormatException/Overview/example2.cs
index 7b0564b04e5..0902cb10939 100644
--- a/snippets/csharp/System/FormatException/Overview/example2.cs
+++ b/snippets/csharp/System/FormatException/Overview/example2.cs
@@ -1,7 +1,7 @@
//
using System;
-public class Example
+public class FormatExample2
{
public enum TemperatureScale
{ Celsius, Fahrenheit, Kelvin }
diff --git a/snippets/csharp/System/FormatException/Overview/example3.cs b/snippets/csharp/System/FormatException/Overview/example3.cs
index a1dff5db6ac..70238dcd314 100644
--- a/snippets/csharp/System/FormatException/Overview/example3.cs
+++ b/snippets/csharp/System/FormatException/Overview/example3.cs
@@ -1,6 +1,6 @@
using System;
-public class Example
+public class FormatExample3
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/format4.cs b/snippets/csharp/System/FormatException/Overview/format4.cs
index 38c367ec183..772c9b37849 100644
--- a/snippets/csharp/System/FormatException/Overview/format4.cs
+++ b/snippets/csharp/System/FormatException/Overview/format4.cs
@@ -1,8 +1,8 @@
using System;
-public class Example
+public class FormatExample4
{
- public static void Main()
+ public static void Run()
{
//
string formatString = " {0,10} ({0,8:X8})\n" +
@@ -10,9 +10,10 @@ public static void Main()
" = {2,10} ({2,8:X8})";
int value1 = 16932;
int value2 = 15421;
- string result = String.Format(formatString,
+ string result = string.Format(formatString,
value1, value2, value1 & value2);
Console.WriteLine(result);
+
// The example displays the following output:
// 16932 (00004224)
// And 15421 (00003C3D)
diff --git a/snippets/csharp/System/FormatException/Overview/format5.cs b/snippets/csharp/System/FormatException/Overview/format5.cs
index 62ec752edfb..4d4adce3647 100644
--- a/snippets/csharp/System/FormatException/Overview/format5.cs
+++ b/snippets/csharp/System/FormatException/Overview/format5.cs
@@ -1,6 +1,6 @@
using System;
-public class Example
+public class FormatExample5
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/format7.cs b/snippets/csharp/System/FormatException/Overview/format7.cs
index 42deffaa1d6..43b1c2139dd 100644
--- a/snippets/csharp/System/FormatException/Overview/format7.cs
+++ b/snippets/csharp/System/FormatException/Overview/format7.cs
@@ -1,6 +1,6 @@
using System;
-public class Example
+public class FormatExample7
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/format_paramarray1.cs b/snippets/csharp/System/FormatException/Overview/format_paramarray1.cs
index a1cd7e518ad..5dcf4ae3a45 100644
--- a/snippets/csharp/System/FormatException/Overview/format_paramarray1.cs
+++ b/snippets/csharp/System/FormatException/Overview/format_paramarray1.cs
@@ -17,7 +17,7 @@ public CityInfo(String name, int population, Decimal area, int year)
public readonly int Year;
}
-public class Example
+public class FormatExample10
{
public static void Main()
{
@@ -38,4 +38,4 @@ private static void ShowPopulationData(CityInfo city)
// The example displays the following output:
// New York in 2010: Population 8,175,133, Area 302.6 sq. feet
// Seattle in 2010: Population 608,660, Area 83.9 sq. feet
-//
\ No newline at end of file
+//
diff --git a/snippets/csharp/System/FormatException/Overview/formatexample4.cs b/snippets/csharp/System/FormatException/Overview/formatexample4.cs
index 8bec656f71d..6da03ced2be 100644
--- a/snippets/csharp/System/FormatException/Overview/formatexample4.cs
+++ b/snippets/csharp/System/FormatException/Overview/formatexample4.cs
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
-public class Example
+public class FormatExample6
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/formatoverload1.cs b/snippets/csharp/System/FormatException/Overview/formatoverload1.cs
new file mode 100644
index 00000000000..067922e00d7
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/formatoverload1.cs
@@ -0,0 +1,18 @@
+using System;
+
+public class Example2
+{
+ public static void Main()
+ {
+ //
+ DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0);
+ string city = "Chicago";
+ int temp = -16;
+ string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
+ dat, city, temp);
+ Console.WriteLine(output);
+ // The example displays output like the following:
+ // At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/formatoverload2.cs b/snippets/csharp/System/FormatException/Overview/formatoverload2.cs
new file mode 100644
index 00000000000..9e9e5396bd6
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/formatoverload2.cs
@@ -0,0 +1,38 @@
+using System;
+
+public class Example3
+{
+ public static void Main()
+ {
+ //
+ // Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
+ Tuple[] cities =
+ { Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277,
+ new DateTime(1950, 1, 1), 1970358),
+ Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995,
+ new DateTime(1950, 1, 1), 7891957),
+ Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808,
+ new DateTime(1950, 1, 1), 3620962),
+ Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452,
+ new DateTime(1950, 1, 1), 1849568) };
+
+ // Display header
+ var header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
+ "City", "Year", "Population", "Change (%)");
+ Console.WriteLine(header);
+ foreach (var city in cities) {
+ var output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
+ city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
+ (city.Item5 - city.Item3)/ (double)city.Item3);
+ Console.WriteLine(output);
+ }
+ // The example displays the following output:
+ // City Year Population Year Population Change (%)
+ //
+ // Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
+ // New York 1940 7,454,995 1950 7,891,957 5.9 %
+ // Chicago 1940 3,396,808 1950 3,620,962 6.6 %
+ // Detroit 1940 1,623,452 1950 1,849,568 13.9 %
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/formatsyntax1.cs b/snippets/csharp/System/FormatException/Overview/formatsyntax1.cs
new file mode 100644
index 00000000000..c11ce9190e7
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/formatsyntax1.cs
@@ -0,0 +1,12 @@
+using System;
+
+public class Example4
+{
+ public static void Main()
+ {
+ //
+ var value = String.Format("{0,-10:C}", 126347.89m);
+ Console.WriteLine(value);
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/iformattable1.cs b/snippets/csharp/System/FormatException/Overview/iformattable1.cs
index 38e4261b622..7161e9fd8dd 100644
--- a/snippets/csharp/System/FormatException/Overview/iformattable1.cs
+++ b/snippets/csharp/System/FormatException/Overview/iformattable1.cs
@@ -1,7 +1,7 @@
//
using System;
-public class Example
+public class FormatExample8
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/iformattable2.cs b/snippets/csharp/System/FormatException/Overview/iformattable2.cs
index d6af7025d27..00c29b7425a 100644
--- a/snippets/csharp/System/FormatException/Overview/iformattable2.cs
+++ b/snippets/csharp/System/FormatException/Overview/iformattable2.cs
@@ -1,7 +1,7 @@
//
using System;
-public class Example
+public class FormatExample9
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/iformattable3.cs b/snippets/csharp/System/FormatException/Overview/iformattable3.cs
index 9849d5ca31b..9c1eedabbba 100644
--- a/snippets/csharp/System/FormatException/Overview/iformattable3.cs
+++ b/snippets/csharp/System/FormatException/Overview/iformattable3.cs
@@ -1,7 +1,7 @@
//
using System;
-public class Example
+public class IFormattableExample3
{
public static void Main()
{
@@ -9,6 +9,7 @@ public static void Main()
Console.WriteLine(Guid.ParseExact(guidString, "G"));
}
}
+
// The example displays the following output:
// Unhandled Exception: System.FormatException:
// Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
diff --git a/snippets/csharp/System/FormatException/Overview/iformattable4.cs b/snippets/csharp/System/FormatException/Overview/iformattable4.cs
index eb980650b48..9169bb84335 100644
--- a/snippets/csharp/System/FormatException/Overview/iformattable4.cs
+++ b/snippets/csharp/System/FormatException/Overview/iformattable4.cs
@@ -1,7 +1,7 @@
//
using System;
-public class Example
+public class FormatExample11
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/interceptor2.cs b/snippets/csharp/System/FormatException/Overview/interceptor2.cs
new file mode 100644
index 00000000000..010a3529f85
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/interceptor2.cs
@@ -0,0 +1,119 @@
+//
+using System;
+using System.Globalization;
+
+public class InterceptProvider : IFormatProvider, ICustomFormatter
+{
+ public object GetFormat(Type formatType)
+ {
+ if (formatType == typeof(ICustomFormatter))
+ return this;
+ else
+ return null;
+ }
+
+ public string Format(String format, Object obj, IFormatProvider provider)
+ {
+ // Display information about method call.
+ string formatString = format ?? "";
+ Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
+ provider.GetType().Name, obj ?? "", formatString);
+
+ if (obj == null) return String.Empty;
+
+ // If this is a byte and the "R" format string, format it with Roman numerals.
+ if (obj is Byte && formatString.ToUpper().Equals("R")) {
+ Byte value = (Byte) obj;
+ int remainder;
+ int result;
+ String returnString = String.Empty;
+
+ // Get the hundreds digit(s)
+ result = Math.DivRem(value, 100, out remainder);
+ if (result > 0)
+ returnString = new String('C', result);
+ value = (Byte) remainder;
+ // Get the 50s digit
+ result = Math.DivRem(value, 50, out remainder);
+ if (result == 1)
+ returnString += "L";
+ value = (Byte) remainder;
+ // Get the tens digit.
+ result = Math.DivRem(value, 10, out remainder);
+ if (result > 0)
+ returnString += new String('X', result);
+ value = (Byte) remainder;
+ // Get the fives digit.
+ result = Math.DivRem(value, 5, out remainder);
+ if (result > 0)
+ returnString += "V";
+ value = (Byte) remainder;
+ // Add the ones digit.
+ if (remainder > 0)
+ returnString += new String('I', remainder);
+
+ // Check whether we have too many X characters.
+ int pos = returnString.IndexOf("XXXX");
+ if (pos >= 0) {
+ int xPos = returnString.IndexOf("L");
+ if (xPos >= 0 & xPos == pos - 1)
+ returnString = returnString.Replace("LXXXX", "XC");
+ else
+ returnString = returnString.Replace("XXXX", "XL");
+ }
+ // Check whether we have too many I characters
+ pos = returnString.IndexOf("IIII");
+ if (pos >= 0)
+ if (returnString.IndexOf("V") >= 0)
+ returnString = returnString.Replace("VIIII", "IX");
+ else
+ returnString = returnString.Replace("IIII", "IV");
+
+ return returnString;
+ }
+
+ // Use default for all other formatting.
+ if (obj is IFormattable)
+ return ((IFormattable) obj).ToString(format, CultureInfo.CurrentCulture);
+ else
+ return obj.ToString();
+ }
+}
+
+public class FormatExample12
+{
+ public static void Main()
+ {
+ int n = 10;
+ double value = 16.935;
+ DateTime day = DateTime.Now;
+ InterceptProvider provider = new InterceptProvider();
+ Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day));
+ Console.WriteLine(String.Format(provider, "{0}: {1:F}\n", "Today: ",
+ (DayOfWeek) DateTime.Now.DayOfWeek));
+ Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
+ (Byte) 2, (Byte) 12, (Byte) 199));
+ Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}\n",
+ (Byte) 2, (Byte) 12, (Byte) 199));
+ }
+}
+// The example displays the following output:
+// Provider: InterceptProvider, Object: 10, Format String: N0
+// Provider: InterceptProvider, Object: 16.935, Format String: C2
+// Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
+// 10: $16.94 on 1/31/2013
+//
+// Provider: InterceptProvider, Object: Today: , Format String:
+// Provider: InterceptProvider, Object: Thursday, Format String: F
+// Today: : Thursday
+//
+// Provider: InterceptProvider, Object: 2, Format String: X
+// Provider: InterceptProvider, Object: 12, Format String:
+// Provider: InterceptProvider, Object: 199, Format String:
+// 2, 12, 199
+//
+// Provider: InterceptProvider, Object: 2, Format String: R
+// Provider: InterceptProvider, Object: 12, Format String: R
+// Provider: InterceptProvider, Object: 199, Format String: R
+// II, XII, CXCIX
+//
diff --git a/snippets/csharp/System/FormatException/Overview/qa-interpolated1.cs b/snippets/csharp/System/FormatException/Overview/qa-interpolated1.cs
new file mode 100644
index 00000000000..faf264947e3
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa-interpolated1.cs
@@ -0,0 +1,23 @@
+using System;
+
+public class Example12
+{
+ public static void Main()
+ {
+ //
+ string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
+ string output = names[0] + ", " + names[1] + ", " + names[2] + ", " +
+ names[3] + ", " + names[4] + ", " + names[5] + ", " +
+ names[6];
+
+ output += "\n";
+ var date = DateTime.Now;
+ output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
+ date, date.DayOfWeek);
+ Console.WriteLine(output);
+ // The example displays the following output:
+ // Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+ // It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa-interpolated2.cs b/snippets/csharp/System/FormatException/Overview/qa-interpolated2.cs
new file mode 100644
index 00000000000..d69dc65af5a
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa-interpolated2.cs
@@ -0,0 +1,20 @@
+using System;
+
+public class Example13
+{
+ public static void Main()
+ {
+ //
+ string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
+ string output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, " +
+ $"{names[5]}, {names[6]}";
+
+ var date = DateTime.Now;
+ output += $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}.";
+ Console.WriteLine(output);
+ // The example displays the following output:
+ // Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+ // It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa1.cs b/snippets/csharp/System/FormatException/Overview/qa1.cs
index a4c2827110d..ff8496d8bef 100644
--- a/snippets/csharp/System/FormatException/Overview/qa1.cs
+++ b/snippets/csharp/System/FormatException/Overview/qa1.cs
@@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;
-public class Example
+public class QAExample1
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/qa11.cs b/snippets/csharp/System/FormatException/Overview/qa11.cs
new file mode 100644
index 00000000000..00e98be5982
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa11.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+
+public class Example5
+{
+ public static void Main()
+ {
+ //
+ Random rnd = new Random();
+ int[] numbers = new int[4];
+ int total = 0;
+ for (int ctr = 0; ctr <= 2; ctr++)
+ {
+ int number = rnd.Next(1001);
+ numbers[ctr] = number;
+ total += number;
+ }
+ numbers[3] = total;
+ Console.WriteLine($"{numbers} + {1} + {2} = {3}");
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa2.cs b/snippets/csharp/System/FormatException/Overview/qa2.cs
index 2c7da859059..9794045a467 100644
--- a/snippets/csharp/System/FormatException/Overview/qa2.cs
+++ b/snippets/csharp/System/FormatException/Overview/qa2.cs
@@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;
-public class Example
+public class QAExample2
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/qa21.cs b/snippets/csharp/System/FormatException/Overview/qa21.cs
new file mode 100644
index 00000000000..067bd5fdd03
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa21.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+
+public class Example6
+{
+ public static void Main()
+ {
+ //
+ Random rnd = new Random();
+ int[] numbers = new int[4];
+ int total = 0;
+ for (int ctr = 0; ctr <= 2; ctr++)
+ {
+ int number = rnd.Next(1001);
+ numbers[ctr] = number;
+ total += number;
+ }
+ numbers[3] = total;
+ object[] values = new object[numbers.Length];
+ numbers.CopyTo(values, 0);
+ Console.WriteLine($"{values} + {1} + {2} = {3}");
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa26.cs b/snippets/csharp/System/FormatException/Overview/qa26.cs
new file mode 100644
index 00000000000..372f1370470
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa26.cs
@@ -0,0 +1,24 @@
+using System;
+
+public class Example7
+{
+ public static void Main()
+ {
+ //
+ object[] values = { 1603, 1794.68235, 15436.14 };
+ string result;
+ foreach (var value in values)
+ {
+ result = String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}\n",
+ Convert.ToDouble(value), Convert.ToDouble(value) / 10000);
+ Console.WriteLine(result);
+ }
+ // The example displays output like the following:
+ // $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
+ //
+ // $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
+ //
+ // $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa27.cs b/snippets/csharp/System/FormatException/Overview/qa27.cs
new file mode 100644
index 00000000000..403a9da46af
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa27.cs
@@ -0,0 +1,16 @@
+using System;
+
+public class Example8
+{
+ public static void Main()
+ {
+ //
+ decimal value = 16309.5436m;
+ string result = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
+ value);
+ Console.WriteLine(result);
+ // The example displays the following output:
+ // 16309.54360 16,309.54 16309.544
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa28.cs b/snippets/csharp/System/FormatException/Overview/qa28.cs
new file mode 100644
index 00000000000..acd3e998104
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa28.cs
@@ -0,0 +1,16 @@
+using System;
+
+public class Example9
+{
+ public static void Main()
+ {
+ //
+ int value = 16342;
+ string result = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
+ value);
+ Console.WriteLine(result);
+ // The example displays the following output:
+ // 00016342 00016342.000 0,000,016,342.0
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa29.cs b/snippets/csharp/System/FormatException/Overview/qa29.cs
new file mode 100644
index 00000000000..33faef50db8
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/qa29.cs
@@ -0,0 +1,15 @@
+using System;
+
+public class Example10
+{
+ public static void Main()
+ {
+ //
+ int value = 1326;
+ string result = String.Format("{0,10:D6} {0,10:X8}", value);
+ Console.WriteLine(result);
+ // The example displays the following output:
+ // 001326 0000052E
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/qa3.cs b/snippets/csharp/System/FormatException/Overview/qa3.cs
index 4a82da781dc..a599a1e270f 100644
--- a/snippets/csharp/System/FormatException/Overview/qa3.cs
+++ b/snippets/csharp/System/FormatException/Overview/qa3.cs
@@ -1,6 +1,6 @@
using System;
-public class Example
+public class FormatExample13
{
public static void Main()
{
diff --git a/snippets/csharp/System/FormatException/Overview/starting1.cs b/snippets/csharp/System/FormatException/Overview/starting1.cs
new file mode 100644
index 00000000000..3e444869dc2
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/starting1.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Text;
+
+public class Example14
+{
+ public static void Main()
+ {
+ //
+ decimal temp = 20.4m;
+ string s = String.Format("The temperature is {0}°C.", temp);
+ Console.WriteLine(s);
+ // Displays 'The temperature is 20.4°C.'
+ //
+
+ Snippet31();
+ Snippet32();
+ Snippet34();
+ }
+
+ private static void Snippet31()
+ {
+ //
+ string s = String.Format("At {0}, the temperature is {1}°C.",
+ DateTime.Now, 20.4);
+ Console.WriteLine(s);
+ // Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
+ //
+ }
+
+ private static void Snippet32()
+ {
+ //
+ string s = String.Format("It is now {0:d} at {0:t}", DateTime.Now);
+ Console.WriteLine(s);
+ // Output similar to: 'It is now 4/10/2015 at 10:04 AM'
+ //
+ }
+
+ private static void Snippet34()
+ {
+ //
+ int[] years = { 2013, 2014, 2015 };
+ int[] population = { 1025632, 1105967, 1148203 };
+ String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population");
+ for (int index = 0; index < years.Length; index++)
+ s += String.Format("{0,-10} {1,-10:N0}\n",
+ years[index], population[index]);
+ Console.WriteLine($"\n{s}");
+ // Result:
+ // Year Population
+ //
+ // 2013 1,025,632
+ // 2014 1,105,967
+ // 2015 1,148,203
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/starting2.cs b/snippets/csharp/System/FormatException/Overview/starting2.cs
new file mode 100644
index 00000000000..1bfa720b14d
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/starting2.cs
@@ -0,0 +1,28 @@
+using System;
+
+public class Example15
+{
+ public static void Main()
+ {
+ //
+ Decimal pricePerOunce = 17.36m;
+ String s = String.Format("The current price is {0} per ounce.",
+ pricePerOunce);
+ Console.WriteLine(s);
+ // Result: The current price is 17.36 per ounce.
+ //
+ ShowFormatted();
+ }
+
+ private static void ShowFormatted()
+ {
+ //
+ Decimal pricePerOunce = 17.36m;
+ String s = String.Format("The current price is {0:C2} per ounce.",
+ pricePerOunce);
+ Console.WriteLine(s);
+ // Result if current culture is en-US:
+ // The current price is $17.36 per ounce.
+ //
+ }
+}
diff --git a/snippets/csharp/System/FormatException/Overview/starting3.cs b/snippets/csharp/System/FormatException/Overview/starting3.cs
new file mode 100644
index 00000000000..48ef9b7f0b0
--- /dev/null
+++ b/snippets/csharp/System/FormatException/Overview/starting3.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Text;
+
+class Example16
+{
+ public static void Main()
+ {
+ //
+ int[] years = { 2013, 2014, 2015 };
+ int[] population = { 1025632, 1105967, 1148203 };
+ var sb = new System.Text.StringBuilder();
+ sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population"));
+ for (int index = 0; index < years.Length; index++)
+ sb.Append(String.Format("{0,6} {1,15:N0}\n", years[index], population[index]));
+
+ Console.WriteLine(sb);
+
+ // Result:
+ // Year Population
+ //
+ // 2013 1,025,632
+ // 2014 1,105,967
+ // 2015 1,148,203
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/CompareTo/Project.csproj b/snippets/csharp/System/Single/CompareTo/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/Single/CompareTo/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Single/CompareTo/compareto2.cs b/snippets/csharp/System/Single/CompareTo/compareto2.cs
new file mode 100644
index 00000000000..cdd6191ed77
--- /dev/null
+++ b/snippets/csharp/System/Single/CompareTo/compareto2.cs
@@ -0,0 +1,20 @@
+//
+using System;
+
+public class Example2
+{
+ public static void Main()
+ {
+ float value1 = 16.5457f;
+ float operand = 3.8899982f;
+ float value2 = value1 * operand / operand;
+ Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}");
+ Console.WriteLine();
+ Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}");
+ }
+}
+// The example displays the following output:
+// Comparing 16.5457 and 16.5457: -1
+//
+// Comparing 16.5457 and 16.545702: -1
+//
diff --git a/snippets/csharp/System/Single/CompareTo/compareto3.cs b/snippets/csharp/System/Single/CompareTo/compareto3.cs
new file mode 100644
index 00000000000..4d5b9b389fc
--- /dev/null
+++ b/snippets/csharp/System/Single/CompareTo/compareto3.cs
@@ -0,0 +1,20 @@
+//
+using System;
+
+public class Example
+{
+ public static void Main()
+ {
+ float value1 = 16.5457f;
+ float operand = 3.8899982f;
+ object value2 = value1 * operand / operand;
+ Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}");
+ Console.WriteLine();
+ Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}");
+ }
+}
+// The example displays the following output:
+// Comparing 16.5457 and 16.5457: -1
+//
+// Comparing 16.5457 and 16.545702: -1
+//
diff --git a/snippets/csharp/System/Single/Epsilon/Project.csproj b/snippets/csharp/System/Single/Epsilon/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/Single/Epsilon/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Single/Epsilon/epsilon.cs b/snippets/csharp/System/Single/Epsilon/epsilon.cs
new file mode 100644
index 00000000000..2bf16f5c802
--- /dev/null
+++ b/snippets/csharp/System/Single/Epsilon/epsilon.cs
@@ -0,0 +1,25 @@
+//
+using System;
+
+public class Example1
+{
+ public static void Main()
+ {
+ float[] values = { 0f, Single.Epsilon, Single.Epsilon * .5f };
+
+ for (int ctr = 0; ctr <= values.Length - 2; ctr++)
+ {
+ for (int ctr2 = ctr + 1; ctr2 <= values.Length - 1; ctr2++)
+ {
+ Console.WriteLine($"{values[ctr]:r} = {values[ctr2]:r}: {values[ctr].Equals(values[ctr2])}");
+ }
+ Console.WriteLine();
+ }
+ }
+}
+// The example displays the following output:
+// 0 = 1.401298E-45: False
+// 0 = 0: True
+//
+// 1.401298E-45 = 0: False
+//
diff --git a/snippets/csharp/System/Single/Epsilon/epsilon1.cs b/snippets/csharp/System/Single/Epsilon/epsilon1.cs
new file mode 100644
index 00000000000..87664989eac
--- /dev/null
+++ b/snippets/csharp/System/Single/Epsilon/epsilon1.cs
@@ -0,0 +1,50 @@
+//
+using System;
+
+public class Example2
+{
+ public static void Main()
+ {
+ float[] values = { 0.0f, Single.Epsilon };
+ foreach (var value in values) {
+ Console.WriteLine(GetComponentParts(value));
+ Console.WriteLine();
+ }
+ }
+
+ private static string GetComponentParts(float value)
+ {
+ string result = String.Format("{0:R}: ", value);
+ int indent = result.Length;
+
+ // Convert the single to a 4-byte array.
+ byte[] bytes = BitConverter.GetBytes(value);
+ int formattedSingle = BitConverter.ToInt32(bytes, 0);
+
+ // Get the sign bit (byte 3, bit 7).
+ result += String.Format("Sign: {0}\n",
+ (formattedSingle >> 31) != 0 ? "1 (-)" : "0 (+)");
+
+ // Get the exponent (byte 2 bit 7 to byte 3, bits 6)
+ int exponent = (formattedSingle >> 23) & 0x000000FF;
+ int adjustment = (exponent != 0) ? 127 : 126;
+ result += String.Format("{0}Exponent: 0x{1:X4} ({1})\n", new String(' ', indent), exponent - adjustment);
+
+ // Get the significand (bits 0-22)
+ long significand = exponent != 0 ?
+ ((formattedSingle & 0x007FFFFF) | 0x800000) :
+ (formattedSingle & 0x007FFFFF);
+ result += String.Format("{0}Mantissa: 0x{1:X13}\n", new String(' ', indent), significand);
+ return result;
+ }
+}
+// // The example displays the following output:
+// 0: Sign: 0 (+)
+// Exponent: 0xFFFFFF82 (-126)
+// Mantissa: 0x0000000000000
+//
+//
+// 1.401298E-45: Sign: 0 (+)
+// Exponent: 0xFFFFFF82 (-126)
+// Mantissa: 0x0000000000001
+//
diff --git a/snippets/csharp/System/Single/Equals/Project.csproj b/snippets/csharp/System/Single/Equals/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/Single/Equals/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Single/Equals/equalsabs1.cs b/snippets/csharp/System/Single/Equals/equalsabs1.cs
new file mode 100644
index 00000000000..6aa1d68c28e
--- /dev/null
+++ b/snippets/csharp/System/Single/Equals/equalsabs1.cs
@@ -0,0 +1,43 @@
+//
+using System;
+
+public class Example
+{
+ public static void Main()
+ {
+ float value1 = .1f * 10f;
+ float value2 = 0f;
+ for (int ctr = 0; ctr < 10; ctr++)
+ value2 += .1f;
+
+ Console.WriteLine($"{value1:R} = {value2:R}: {HasMinimalDifference(value1, value2, 1)}");
+ }
+
+ public static bool HasMinimalDifference(float value1, float value2, int units)
+ {
+ byte[] bytes = BitConverter.GetBytes(value1);
+ int iValue1 = BitConverter.ToInt32(bytes, 0);
+
+ bytes = BitConverter.GetBytes(value2);
+ int iValue2 = BitConverter.ToInt32(bytes, 0);
+
+ // If the signs are different, return false except for +0 and -0.
+ if ((iValue1 >> 31) != (iValue2 >> 31))
+ {
+ if (value1 == value2)
+ return true;
+
+ return false;
+ }
+
+ int diff = Math.Abs(iValue1 - iValue2);
+
+ if (diff <= units)
+ return true;
+
+ return false;
+ }
+}
+// The example displays the following output:
+// 1 = 1.00000012: True
+//
\ No newline at end of file
diff --git a/snippets/csharp/System/Single/Equals/equalsoverl.cs b/snippets/csharp/System/Single/Equals/equalsoverl.cs
index 6b466ab062e..33dd94b5851 100644
--- a/snippets/csharp/System/Single/Equals/equalsoverl.cs
+++ b/snippets/csharp/System/Single/Equals/equalsoverl.cs
@@ -1,10 +1,10 @@
//
using System;
-public class Example
+public class Example2
{
static float value = 112;
-
+
public static void Main()
{
byte byte1= 112;
@@ -22,7 +22,7 @@ public static void Main()
long long1 = 112;
Console.WriteLine("value = long1: {0,17}", value.Equals(long1));
TestObjectForEquality(long1);
-
+
sbyte sbyte1 = 112;
Console.WriteLine("value = sbyte1: {0,16}", value.Equals(sbyte1));
TestObjectForEquality(sbyte1);
@@ -38,7 +38,7 @@ public static void Main()
ulong ulong1 = 112;
Console.WriteLine("value = ulong1: {0,17}", value.Equals(ulong1));
TestObjectForEquality(ulong1);
-
+
decimal dec1 = 112m;
Console.WriteLine("value = dec1: {0,21}", value.Equals(dec1));
TestObjectForEquality(dec1);
diff --git a/snippets/csharp/System/Single/Overview/Project.csproj b/snippets/csharp/System/Single/Overview/Project.csproj
new file mode 100644
index 00000000000..258b56c95b9
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/Project.csproj
@@ -0,0 +1,9 @@
+
+
+
+ Exe
+ net10.0
+ Example1
+
+
+
diff --git a/snippets/csharp/System/Single/Overview/comparison1.cs b/snippets/csharp/System/Single/Overview/comparison1.cs
new file mode 100644
index 00000000000..965c922e4f9
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/comparison1.cs
@@ -0,0 +1,15 @@
+//
+using System;
+
+public class Example
+{
+ public static void Main()
+ {
+ float value1 = .3333333f;
+ float value2 = 1.0f/3;
+ Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
+ }
+}
+// The example displays the following output:
+// 0.3333333 = 0.333333343: False
+//
diff --git a/snippets/csharp/System/Single/Overview/comparison2.cs b/snippets/csharp/System/Single/Overview/comparison2.cs
new file mode 100644
index 00000000000..3282246945f
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/comparison2.cs
@@ -0,0 +1,20 @@
+using System;
+
+public class Example1
+{
+ public static void Main()
+ {
+ //
+ float value1 = 10.201438f;
+ value1 = (float)Math.Sqrt((float)Math.Pow(value1, 2));
+ float value2 = (float)Math.Pow((float)value1 * 3.51f, 2);
+ value2 = ((float)Math.Sqrt(value2)) / 3.51f;
+ Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
+
+ // The example displays the following output on .NET:
+ // 10.201438 = 10.201439: False
+ // The example displays the following output on .NET Framework:
+ // 10.20144 = 10.20144: False
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/comparison3.cs b/snippets/csharp/System/Single/Overview/comparison3.cs
new file mode 100644
index 00000000000..4f82ff0c2ea
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/comparison3.cs
@@ -0,0 +1,19 @@
+using System;
+
+public class Example2
+{
+ public static void Main()
+ {
+ //
+ float value1 = .3333333f;
+ float value2 = 1.0f / 3;
+ int precision = 7;
+ value1 = (float)Math.Round(value1, precision);
+ value2 = (float)Math.Round(value2, precision);
+ Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
+
+ // The example displays the following output:
+ // 0.3333333 = 0.3333333: True
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/comparison4.cs b/snippets/csharp/System/Single/Overview/comparison4.cs
new file mode 100644
index 00000000000..ded666dc514
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/comparison4.cs
@@ -0,0 +1,51 @@
+using System;
+
+public class Example3
+{
+ //
+ public static void Main()
+ {
+ float one1 = .1f * 10;
+ float one2 = 0f;
+ for (int ctr = 1; ctr <= 10; ctr++)
+ one2 += .1f;
+
+ Console.WriteLine($"{one1:R} = {one2:R}: {one1.Equals(one2)}");
+ Console.WriteLine($"{one1:R} is approximately equal to {one2:R}: " +
+ $"{IsApproximatelyEqual(one1, one2, .000001f)}");
+
+ float negativeOne1 = -1 * one1;
+ float negativeOne2 = -1 * one2;
+
+ Console.WriteLine($"{negativeOne1:R} = {negativeOne2:R}: {negativeOne1.Equals(negativeOne2)}");
+ Console.WriteLine($"{negativeOne1:R} is approximately equal to {negativeOne2:R}: " +
+ $"{IsApproximatelyEqual(negativeOne1, negativeOne2, .000001f)}");
+ }
+
+ static bool IsApproximatelyEqual(float value1, float value2, float epsilon)
+ {
+ // If they are equal anyway, just return True.
+ if (value1.Equals(value2))
+ return true;
+
+ // Handle NaN, Infinity.
+ if (Double.IsInfinity(value1) | Double.IsNaN(value1))
+ return value1.Equals(value2);
+ else if (Double.IsInfinity(value2) | Double.IsNaN(value2))
+ return value1.Equals(value2);
+
+ // Handle zero to avoid division by zero.
+ double divisor = Math.Max(value1, value2);
+ if (divisor.Equals(0))
+ divisor = Math.Min(value1, value2);
+
+ return Math.Abs((value1 - value2) / divisor) <= epsilon;
+ }
+
+ // The example displays the following output on .NET:
+ // 1 = 1.0000001: False
+ // 1 is approximately equal to 1.0000001: True
+ // -1 = -1.0000001: False
+ // -1 is approximately equal to -1.0000001: True
+ //
+}
diff --git a/snippets/csharp/System/Single/Overview/convert1.cs b/snippets/csharp/System/Single/Overview/convert1.cs
new file mode 100644
index 00000000000..8a5e0e75a97
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/convert1.cs
@@ -0,0 +1,48 @@
+//
+using System;
+
+public class Example4
+{
+ public static void Main()
+ {
+ dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
+ Decimal.MaxValue, Double.MinValue, Double.MaxValue,
+ Int16.MinValue, Int16.MaxValue, Int32.MinValue,
+ Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
+ SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
+ UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
+ UInt64.MinValue, UInt64.MaxValue };
+ float sngValue;
+ foreach (var value in values)
+ {
+ if (value.GetType() == typeof(Decimal) ||
+ value.GetType() == typeof(Double))
+ sngValue = (float)value;
+ else
+ sngValue = value;
+ Console.WriteLine($"{value} ({value.GetType().Name}) --> {sngValue:R} ({sngValue.GetType().Name})");
+ }
+ }
+}
+// The example displays the following output:
+// 0 (Byte) --> 0 (Single)
+// 255 (Byte) --> 255 (Single)
+// -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single)
+// 79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single)
+// -1.79769313486232E+308 (Double) --> -Infinity (Single)
+// 1.79769313486232E+308 (Double) --> Infinity (Single)
+// -32768 (Int16) --> -32768 (Single)
+// 32767 (Int16) --> 32767 (Single)
+// -2147483648 (Int32) --> -2.14748365E+09 (Single)
+// 2147483647 (Int32) --> 2.14748365E+09 (Single)
+// -9223372036854775808 (Int64) --> -9.223372E+18 (Single)
+// 9223372036854775807 (Int64) --> 9.223372E+18 (Single)
+// -128 (SByte) --> -128 (Single)
+// 127 (SByte) --> 127 (Single)
+// 0 (UInt16) --> 0 (Single)
+// 65535 (UInt16) --> 65535 (Single)
+// 0 (UInt32) --> 0 (Single)
+// 4294967295 (UInt32) --> 4.2949673E+09 (Single)
+// 0 (UInt64) --> 0 (Single)
+// 18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)
+//
diff --git a/snippets/csharp/System/Single/Overview/convert2.cs b/snippets/csharp/System/Single/Overview/convert2.cs
new file mode 100644
index 00000000000..d9a17fb336c
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/convert2.cs
@@ -0,0 +1,98 @@
+using System;
+
+public class Example5
+{
+ public static void Main()
+ {
+ //
+ float[] values = { Single.MinValue, -67890.1234f, -12345.6789f,
+ 12345.6789f, 67890.1234f, Single.MaxValue,
+ Single.NaN, Single.PositiveInfinity,
+ Single.NegativeInfinity };
+ checked
+ {
+ foreach (var value in values)
+ {
+ try
+ {
+ Int64 lValue = (long)value;
+ Console.WriteLine($"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})");
+ }
+ catch (OverflowException)
+ {
+ Console.WriteLine($"Unable to convert {value} to Int64.");
+ }
+ try
+ {
+ UInt64 ulValue = (ulong)value;
+ Console.WriteLine($"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})");
+ }
+ catch (OverflowException)
+ {
+ Console.WriteLine($"Unable to convert {value} to UInt64.");
+ }
+ try
+ {
+ Decimal dValue = (decimal)value;
+ Console.WriteLine($"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})");
+ }
+ catch (OverflowException)
+ {
+ Console.WriteLine($"Unable to convert {value} to Decimal.");
+ }
+
+ Double dblValue = value;
+ Console.WriteLine($"{value} ({value.GetType().Name}) --> {dblValue} ({dblValue.GetType().Name})");
+ Console.WriteLine();
+ }
+ }
+
+ // The example displays the following output for conversions performed
+ // in a checked context:
+ // Unable to convert -3.402823E+38 to Int64.
+ // Unable to convert -3.402823E+38 to UInt64.
+ // Unable to convert -3.402823E+38 to Decimal.
+ // -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
+ //
+ // -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
+ // Unable to convert -67890.13 to UInt64.
+ // -67890.13 (Single) --> -67890.12 (Decimal)
+ // -67890.13 (Single) --> -67890.125 (Double)
+ //
+ // -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
+ // Unable to convert -12345.68 to UInt64.
+ // -12345.68 (Single) --> -12345.68 (Decimal)
+ // -12345.68 (Single) --> -12345.6787109375 (Double)
+ //
+ // 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
+ // 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
+ // 12345.68 (Single) --> 12345.68 (Decimal)
+ // 12345.68 (Single) --> 12345.6787109375 (Double)
+ //
+ // 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
+ // 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
+ // 67890.13 (Single) --> 67890.12 (Decimal)
+ // 67890.13 (Single) --> 67890.125 (Double)
+ //
+ // Unable to convert 3.402823E+38 to Int64.
+ // Unable to convert 3.402823E+38 to UInt64.
+ // Unable to convert 3.402823E+38 to Decimal.
+ // 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
+ //
+ // Unable to convert NaN to Int64.
+ // Unable to convert NaN to UInt64.
+ // Unable to convert NaN to Decimal.
+ // NaN (Single) --> NaN (Double)
+ //
+ // Unable to convert ∞ to Int64.
+ // Unable to convert ∞ to UInt64.
+ // Unable to convert ∞ to Decimal.
+ // ∞ (Single) --> ∞ (Double)
+ //
+ // Unable to convert -∞ to Int64.
+ // Unable to convert -∞ to UInt64.
+ // Unable to convert -∞ to Decimal.
+ // -∞ (Single) --> -∞ (Double)
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/exceptional1.cs b/snippets/csharp/System/Single/Overview/exceptional1.cs
new file mode 100644
index 00000000000..5f914834675
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/exceptional1.cs
@@ -0,0 +1,19 @@
+using System;
+
+public class Example6
+{
+ public static void Main()
+ {
+ //
+ float value1 = 1.163287e-36f;
+ float value2 = 9.164234e-25f;
+ float result = value1 * value2;
+ Console.WriteLine($"{value1} * {value2} = {result}");
+ Console.WriteLine($"{result} = 0: {result.Equals(0.0f)}");
+
+ // The example displays the following output:
+ // 1.163287E-36 * 9.164234E-25 = 0
+ // 0 = 0: True
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/exceptional2.cs b/snippets/csharp/System/Single/Overview/exceptional2.cs
new file mode 100644
index 00000000000..6c0cf45b151
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/exceptional2.cs
@@ -0,0 +1,28 @@
+using System;
+
+public class Example7
+{
+ public static void Main()
+ {
+ //
+ float value1 = 3.065e35f;
+ float value2 = 6.9375e32f;
+ float result = value1 * value2;
+ Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}");
+ Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}");
+ Console.WriteLine();
+
+ value1 = -value1;
+ result = value1 * value2;
+ Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}");
+ Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}");
+
+ // The example displays the following output:
+ // PositiveInfinity: True
+ // NegativeInfinity: False
+ //
+ // PositiveInfinity: False
+ // NegativeInfinity: True
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/precisionlist1.cs b/snippets/csharp/System/Single/Overview/precisionlist1.cs
new file mode 100644
index 00000000000..c42b4fc898f
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/precisionlist1.cs
@@ -0,0 +1,17 @@
+using System;
+
+public class Example9
+{
+ public static void Main()
+ {
+ //
+ Double value1 = 1 / 3.0;
+ Single sValue2 = 1 / 3.0f;
+ Double value2 = (Double)sValue2;
+ Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}");
+
+ // The example displays the following output on .NET:
+ // 0.3333333333333333 = 0.3333333432674408: False
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/precisionlist3.cs b/snippets/csharp/System/Single/Overview/precisionlist3.cs
new file mode 100644
index 00000000000..d05493e8b93
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/precisionlist3.cs
@@ -0,0 +1,25 @@
+//
+using System;
+
+public class PrecisionList3Example
+{
+ public static void Main()
+ {
+ Single[] values = { 10.01f, 2.88f, 2.88f, 2.88f, 9.0f };
+ Single result = 27.65f;
+ Single total = 0f;
+ foreach (var value in values)
+ total += value;
+
+ if (total.Equals(result))
+ Console.WriteLine("The sum of the values equals the total.");
+ else
+ Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result}).");
+ }
+}
+
+// The example displays the following output on .NET:
+// The sum of the values (27.650002) does not equal the total (27.65).
+// The example displays the following output on .NET Framework:
+// The sum of the values (27.65) does not equal the total (27.65).
+//
diff --git a/snippets/csharp/System/Single/Overview/precisionlist4a.cs b/snippets/csharp/System/Single/Overview/precisionlist4a.cs
new file mode 100644
index 00000000000..299b38273f9
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/precisionlist4a.cs
@@ -0,0 +1,35 @@
+using System;
+using System.IO;
+
+public class Example10
+{
+ public static void Main()
+ {
+ //
+ StreamWriter sw = new(@"./Singles.dat");
+ float[] values = { 3.2f / 1.11f, 1.0f / 3f, (float)Math.PI };
+ for (int ctr = 0; ctr < values.Length; ctr++)
+ {
+ sw.Write(values[ctr].ToString());
+ if (ctr != values.Length - 1)
+ sw.Write("|");
+ }
+ sw.Close();
+
+ float[] restoredValues = new float[values.Length];
+ StreamReader sr = new(@"./Singles.dat");
+ string temp = sr.ReadToEnd();
+ string[] tempStrings = temp.Split('|');
+ for (int ctr = 0; ctr < tempStrings.Length; ctr++)
+ restoredValues[ctr] = float.Parse(tempStrings[ctr]);
+
+ for (int ctr = 0; ctr < values.Length; ctr++)
+ Console.WriteLine($"{values[ctr]} {(values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>")} {restoredValues[ctr]}");
+
+ // The example displays the following output on .NET Framework:
+ // 2.882883 <> 2.882883
+ // 0.3333333 <> 0.3333333
+ // 3.141593 <> 3.141593
+ //
+ }
+}
diff --git a/snippets/csharp/System/Single/Overview/representation1.cs b/snippets/csharp/System/Single/Overview/representation1.cs
new file mode 100644
index 00000000000..6ea38e68a79
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/representation1.cs
@@ -0,0 +1,21 @@
+//
+using System;
+
+public class Example12
+{
+ public static void Main()
+ {
+ Single value = .2f;
+ Single result1 = value * 10f;
+ Single result2 = 0f;
+ for (int ctr = 1; ctr <= 10; ctr++)
+ result2 += value;
+
+ Console.WriteLine($".2 * 10: {result1:R}");
+ Console.WriteLine($".2 Added 10 times: {result2:R}");
+ }
+}
+// The example displays the following output:
+// .2 * 10: 2
+// .2 Added 10 times: 2.0000002
+//
diff --git a/snippets/csharp/System/Single/Overview/representation2.cs b/snippets/csharp/System/Single/Overview/representation2.cs
new file mode 100644
index 00000000000..a3a072bdb3b
--- /dev/null
+++ b/snippets/csharp/System/Single/Overview/representation2.cs
@@ -0,0 +1,16 @@
+//
+using System;
+
+public class Example13
+{
+ public static void Main()
+ {
+ Single value = 123.456f;
+ Single additional = Single.Epsilon * 1e15f;
+ Console.WriteLine($"{value} + {additional} = {value + additional}");
+ }
+}
+
+// The example displays the following output:
+// 123.456 + 1.401298E-30 = 123.456
+//
diff --git a/snippets/csharp/System/String/.ctor/Project.csproj b/snippets/csharp/System/String/.ctor/Project.csproj
new file mode 100644
index 00000000000..22dd9034898
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/Project.csproj
@@ -0,0 +1,9 @@
+
+
+
+ Library
+ net10.0
+ true
+
+
+
diff --git a/snippets/csharp/System/String/.ctor/char2_ctor.cs b/snippets/csharp/System/String/.ctor/char2_ctor.cs
new file mode 100644
index 00000000000..ea152e7404d
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/char2_ctor.cs
@@ -0,0 +1,30 @@
+//
+using System;
+
+public class Example1
+{
+ public static unsafe void Main()
+ {
+ char[] characters = { 'H', 'e', 'l', 'l', 'o', ' ',
+ 'w', 'o', 'r', 'l', 'd', '!', '\u0000' };
+ String value;
+
+ fixed (char* charPtr = characters) {
+ int length = 0;
+ Char* iterator = charPtr;
+
+ while (*iterator != '\x0000')
+ {
+ if (*iterator == '!' || *iterator == '.')
+ break;
+ iterator++;
+ length++;
+ }
+ value = new String(charPtr, 0, length);
+ }
+ Console.WriteLine(value);
+ }
+}
+// The example displays the following output:
+// Hello World
+//
diff --git a/snippets/csharp/System/String/.ctor/chptrctor_null.cs b/snippets/csharp/System/String/.ctor/chptrctor_null.cs
new file mode 100644
index 00000000000..41eb2e7fa08
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/chptrctor_null.cs
@@ -0,0 +1,31 @@
+//
+using System;
+
+public class Example2
+{
+ public unsafe static void Main()
+ {
+ char[] chars = { 'a', 'b', 'c', 'd', '\0', 'A', 'B', 'C', 'D', '\0' };
+ string s = null;
+
+ fixed(char* chPtr = chars) {
+ s = new string(chPtr, 0, chars.Length);
+ }
+
+ foreach (var ch in s)
+ Console.Write($"{(ushort)ch:X4} ");
+ Console.WriteLine();
+
+ fixed(char* chPtr = chars) {
+ s = new string(chPtr);
+ }
+
+ foreach (var ch in s)
+ Console.Write($"{(ushort)ch:X4} ");
+ Console.WriteLine();
+ }
+}
+// The example displays the following output:
+// 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000
+// 0061 0062 0063 0064
+//
diff --git a/snippets/csharp/System/String/.ctor/chptrctor_null.csproj b/snippets/csharp/System/String/.ctor/chptrctor_null.csproj
deleted file mode 100644
index d417f512b40..00000000000
--- a/snippets/csharp/System/String/.ctor/chptrctor_null.csproj
+++ /dev/null
@@ -1,59 +0,0 @@
-
-
-
-
- Debug
- AnyCPU
- {A494B950-CDBA-4812-A4D9-0A5682B6F169}
- Exe
- Properties
- ChPtrCtor_Null
- ChPtrCtor_Null
- v4.8
- 512
-
-
-
- AnyCPU
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
- true
-
-
- AnyCPU
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/snippets/csharp/System/String/.ctor/ctor1.cs b/snippets/csharp/System/String/.ctor/ctor1.cs
new file mode 100644
index 00000000000..c3ff8a27bd1
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/ctor1.cs
@@ -0,0 +1,17 @@
+//
+using System;
+
+public class Example3
+{
+ public static void Main()
+ {
+ String value1 = "This is a string.";
+ String value2 = value1;
+ Console.WriteLine(value1);
+ Console.WriteLine(value2);
+ }
+}
+// The example displays the following output:
+// This is a string.
+// This is a string.
+//
diff --git a/snippets/csharp/System/String/.ctor/ctor2.cs b/snippets/csharp/System/String/.ctor/ctor2.cs
new file mode 100644
index 00000000000..b6e0d7f7885
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/ctor2.cs
@@ -0,0 +1,20 @@
+//
+using System;
+
+public class Example4
+{
+ public static unsafe void Main()
+ {
+ char[] characters = { 'H', 'e', 'l', 'l', 'o', ' ',
+ 'w', 'o', 'r', 'l', 'd', '!', '\u0000' };
+ string value;
+
+ fixed (char* charPtr = characters) {
+ value = new String(charPtr);
+ }
+ Console.WriteLine(value);
+ }
+}
+// The example displays the following output:
+// Hello world!
+//
diff --git a/snippets/csharp/System/String/.ctor/ptrctor_null.cs b/snippets/csharp/System/String/.ctor/ptrctor_null.cs
new file mode 100644
index 00000000000..7209113786f
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/ptrctor_null.cs
@@ -0,0 +1,32 @@
+//
+using System;
+
+public class Example5
+{
+ public unsafe static void Main()
+ {
+ sbyte[] bytes = { 0x61, 0x62, 0x063, 0x064, 0x00, 0x41, 0x42, 0x43, 0x44, 0x00 };
+
+ string s = null;
+ fixed (sbyte* bytePtr = bytes) {
+ s = new string(bytePtr, 0, bytes.Length);
+ }
+
+ foreach (var ch in s)
+ Console.Write($"{(ushort)ch:X4} ");
+
+ Console.WriteLine();
+
+ fixed(sbyte* bytePtr = bytes) {
+ s = new string(bytePtr);
+ }
+
+ foreach (var ch in s)
+ Console.Write($"{(ushort)ch:X4} ");
+ Console.WriteLine();
+ }
+}
+// The example displays the following output:
+// 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000
+// 0061 0062 0063 0064
+//
diff --git a/snippets/csharp/System/String/.ctor/source.cs b/snippets/csharp/System/String/.ctor/source.cs
new file mode 100644
index 00000000000..7eebd4e46d9
--- /dev/null
+++ b/snippets/csharp/System/String/.ctor/source.cs
@@ -0,0 +1,91 @@
+using System;
+using System.Text;
+
+namespace Microsoft.Demo
+{
+ class ConsoleApp
+ {
+ [STAThread]
+ static void Main(string[] args)
+ {
+//
+ // Unicode Mathematical operators
+ char [] charArr1 = {'\u2200','\u2202','\u200F','\u2205'};
+ String szMathSymbols = new String(charArr1);
+
+ // Unicode Letterlike Symbols
+ char [] charArr2 = {'\u2111','\u2118','\u2122','\u2126'};
+ String szLetterLike = new String (charArr2);
+
+ // Compare Strings - the result is false
+ Console.WriteLine("The Strings are equal? " +
+ (String.Compare(szMathSymbols, szLetterLike)==0?"true":"false") );
+//
+//
+ unsafe
+ {
+ // Null terminated ASCII characters in an sbyte array
+ String szAsciiUpper = null;
+ sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
+ // Instruct the Garbage Collector not to move the memory
+ fixed(sbyte* pAsciiUpper = sbArr1)
+ {
+ szAsciiUpper = new String(pAsciiUpper);
+ }
+ String szAsciiLower = null;
+ sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
+ // Instruct the Garbage Collector not to move the memory
+ fixed(sbyte* pAsciiLower = sbArr2)
+ {
+ szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
+ }
+ // Prints "ABC abc"
+ Console.WriteLine(szAsciiUpper + " " + szAsciiLower);
+
+ // Compare Strings - the result is true
+ Console.WriteLine("The Strings are equal when capitalized ? " +
+ (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") );
+
+ // This is the effective equivalent of another Compare method, which ignores case
+ Console.WriteLine("The Strings are equal when capitalized ? " +
+ (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );
+ }
+//
+//
+ // Create a Unicode String with 5 Greek Alpha characters
+ String szGreekAlpha = new String('\u0391',5);
+ // Create a Unicode String with a Greek Omega character
+ String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1);
+
+ String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());
+
+ // Examine the result
+ Console.WriteLine(szGreekLetters);
+
+ // The first index of Alpha
+ int ialpha = szGreekLetters.IndexOf('\u0391');
+ // The last index of Omega
+ int iomega = szGreekLetters.LastIndexOf('\u03A9');
+
+ Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
+ " and Omega last appears at index " + iomega + " in this String.");
+//
+
+//
+ unsafe
+ {
+ String utfeightstring = null;
+ sbyte [] asciiChars = new sbyte[] { 0x51,0x52,0x53,0x54,0x54,0x56 };
+ UTF8Encoding encoding = new UTF8Encoding(true, true);
+
+ // Instruct the Garbage Collector not to move the memory
+ fixed(sbyte* pAsciiChars = asciiChars)
+ {
+ utfeightstring = new String(pAsciiChars,0,asciiChars.Length,encoding);
+ }
+ Console.WriteLine("The UTF8 String is " + utfeightstring ); // prints "QRSTTV"
+ }
+//
+ }
+ }
+}
\ No newline at end of file
diff --git a/snippets/csharp/System/String/Format/Example1.cs b/snippets/csharp/System/String/Format/Example1.cs
new file mode 100644
index 00000000000..1040735cbf8
--- /dev/null
+++ b/snippets/csharp/System/String/Format/Example1.cs
@@ -0,0 +1,25 @@
+using System;
+
+public class Example1
+{
+ public static void Main()
+ {
+ //
+ short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
+ Console.WriteLine("{0,10} {1,10}\n", "Decimal", "Hex");
+ foreach (short value in values)
+ {
+ string formatString = String.Format("{0,10:G}: {0,10:X}", value);
+ Console.WriteLine(formatString);
+ }
+ // The example displays the following output:
+ // Decimal Hex
+ //
+ // -32768: 8000
+ // -27: FFE5
+ // 0: 0
+ // 1042: 412
+ // 32767: 7FFF
+ //
+ }
+}
diff --git a/snippets/csharp/System/String/Format/Example2.cs b/snippets/csharp/System/String/Format/Example2.cs
index af0fbbb8d57..b33a5f30d9e 100644
--- a/snippets/csharp/System/String/Format/Example2.cs
+++ b/snippets/csharp/System/String/Format/Example2.cs
@@ -1,13 +1,13 @@
using System;
using System.Globalization;
-public class Example
+public class Example2
{
public static void Main()
{
//
string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" };
-
+
DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0);
double value = 9164.32;
@@ -15,13 +15,13 @@ public static void Main()
foreach (string cultureName in cultureNames)
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
- string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}",
+ string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}",
culture.Name, dateToDisplay, value);
Console.WriteLine(output);
- }
+ }
// The example displays the following output:
// Culture Date Value
- //
+ //
// en-US Tuesday, September 01, 2009 9,164.32
// fr-FR mardi 1 septembre 2009 9 164,32
// de-DE Dienstag, 1. September 2009 9.164,32
diff --git a/snippets/csharp/System/String/Format/Project.csproj b/snippets/csharp/System/String/Format/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/String/Format/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/String/Intern/Intern1.cs b/snippets/csharp/System/String/Intern/Intern1.cs
new file mode 100644
index 00000000000..fa3a57ae33a
--- /dev/null
+++ b/snippets/csharp/System/String/Intern/Intern1.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Text;
+
+[assembly: CLSCompliant(true)]
+public class Class1
+{
+ public static void Main()
+ {
+ //
+ string s1 = "MyTest";
+ string s2 = new StringBuilder().Append("My").Append("Test").ToString();
+ string s3 = String.Intern(s2);
+ Console.WriteLine((Object)s2==(Object)s1); // Different references.
+ Console.WriteLine((Object)s3==(Object)s1); // The same reference.
+ //
+ }
+}
diff --git a/snippets/csharp/System/String/Intern/Project.csproj b/snippets/csharp/System/String/Intern/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/String/Intern/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/String/IsNullOrEmpty/NullString1.cs b/snippets/csharp/System/String/IsNullOrEmpty/NullString1.cs
new file mode 100644
index 00000000000..c9da93895ba
--- /dev/null
+++ b/snippets/csharp/System/String/IsNullOrEmpty/NullString1.cs
@@ -0,0 +1,40 @@
+using System;
+
+public class Example
+{
+ public static void Main()
+ {
+ //
+ String s = null;
+
+ Console.WriteLine($"The value of the string is '{s}'");
+
+ try
+ {
+ Console.WriteLine($"String length is {s.Length}");
+ }
+ catch (NullReferenceException e)
+ {
+ Console.WriteLine(e.Message);
+ }
+
+ // The example displays the following output:
+ // The value of the string is ''
+ // Object reference not set to an instance of an object.
+ //
+ }
+}
+
+public class Empty
+{
+ public void Test()
+ {
+ //
+ String s = "";
+ Console.WriteLine($"The length of '{s}' is {s.Length}.");
+
+ // The example displays the following output:
+ // The length of '' is 0.
+ //
+ }
+}
diff --git a/snippets/csharp/System/String/IsNullOrEmpty/Project.csproj b/snippets/csharp/System/String/IsNullOrEmpty/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/String/IsNullOrEmpty/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/String/IsNullOrEmpty/isnullorempty1.cs b/snippets/csharp/System/String/IsNullOrEmpty/isnullorempty1.cs
new file mode 100644
index 00000000000..d023d3a5236
--- /dev/null
+++ b/snippets/csharp/System/String/IsNullOrEmpty/isnullorempty1.cs
@@ -0,0 +1,25 @@
+using System;
+
+public class Example1
+{
+ public static void Main()
+ {
+ //
+ bool TestForNullOrEmpty(string s)
+ {
+ bool result;
+ result = s == null || s == string.Empty;
+ return result;
+ }
+
+ string s1 = null;
+ string s2 = "";
+ Console.WriteLine(TestForNullOrEmpty(s1));
+ Console.WriteLine(TestForNullOrEmpty(s2));
+
+ // The example displays the following output:
+ // True
+ // True
+ //
+ }
+}
diff --git a/snippets/csharp/System/TimeSpan/Overview/Program.cs b/snippets/csharp/System/TimeSpan/Overview/Program.cs
new file mode 100644
index 00000000000..1047b684bc1
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Overview/Program.cs
@@ -0,0 +1 @@
+Example4.Run();
diff --git a/snippets/csharp/System/TimeSpan/Overview/Project.csproj b/snippets/csharp/System/TimeSpan/Overview/Project.csproj
new file mode 100644
index 00000000000..274b5ecb6d6
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Overview/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/TimeSpan/Overview/instantiate1.cs b/snippets/csharp/System/TimeSpan/Overview/instantiate1.cs
new file mode 100644
index 00000000000..c93d7f01285
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Overview/instantiate1.cs
@@ -0,0 +1,74 @@
+using System;
+
+public class Example
+{
+ public static void Main()
+ {
+ Implicit();
+ Console.WriteLine();
+ Explicit();
+ Console.WriteLine();
+ TimeSpanOperation();
+ Console.WriteLine();
+ Parse();
+ Console.WriteLine();
+ }
+
+ private static void Implicit()
+ {
+ //
+ TimeSpan interval = new TimeSpan();
+ Console.WriteLine(interval.Equals(TimeSpan.Zero)); // Displays "True".
+ //
+ }
+
+ private static void Explicit()
+ {
+ //
+ TimeSpan interval = new TimeSpan(2, 14, 18);
+ Console.WriteLine(interval.ToString());
+
+ // Displays "02:14:18".
+ //
+ }
+
+ private static void TimeSpanOperation()
+ {
+ //
+ DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
+ DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
+ TimeSpan travelTime = arrival - departure;
+ Console.WriteLine($"{arrival} - {departure} = {travelTime}");
+
+ // The example displays the following output:
+ // 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
+ //
+ }
+
+ private static void Parse()
+ {
+ //
+ string[] values = { "12", "31.", "5.8:32:16", "12:12:15.95", ".12"};
+ foreach (string value in values)
+ {
+ try {
+ TimeSpan ts = TimeSpan.Parse(value);
+ Console.WriteLine($"'{value}' --> {ts}");
+ }
+ catch (FormatException) {
+ Console.WriteLine($"Unable to parse '{value}'");
+ }
+ catch (OverflowException) {
+ Console.WriteLine($"'{value}' is outside the range of a TimeSpan.");
+ }
+ }
+
+ // The example displays the following output:
+ // '12' --> 12.00:00:00
+ // Unable to parse '31.'
+ // '5.8:32:16' --> 5.08:32:16
+ // '12:12:15.95' --> 12:12:15.9500000
+ // Unable to parse '.12'
+ //
+ }
+}
diff --git a/snippets/csharp/System/TimeSpan/Overview/structure1.cs b/snippets/csharp/System/TimeSpan/Overview/structure1.cs
index c6ad2557bd6..f034ea01cd4 100644
--- a/snippets/csharp/System/TimeSpan/Overview/structure1.cs
+++ b/snippets/csharp/System/TimeSpan/Overview/structure1.cs
@@ -1,6 +1,6 @@
using System;
-public class Example
+public class StructureExample1
{
public static void Main()
{
diff --git a/snippets/csharp/System/TimeSpan/Overview/zero1.cs b/snippets/csharp/System/TimeSpan/Overview/zero1.cs
new file mode 100644
index 00000000000..3ac36200ed2
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Overview/zero1.cs
@@ -0,0 +1,31 @@
+using System;
+
+public class Example4
+{
+ public static void Run()
+ {
+ //
+ Random rnd = new Random();
+
+ TimeSpan timeSpent = TimeSpan.Zero;
+
+ timeSpent += GetTimeBeforeLunch();
+ timeSpent += GetTimeAfterLunch();
+
+ Console.WriteLine($"Total time: {timeSpent}");
+
+ TimeSpan GetTimeBeforeLunch()
+ {
+ return new TimeSpan(rnd.Next(3, 6), 0, 0);
+ }
+
+ TimeSpan GetTimeAfterLunch()
+ {
+ return new TimeSpan(rnd.Next(3, 6), 0, 0);
+ }
+
+ // The example displays output like the following:
+ // Total time: 08:00:00
+ //
+ }
+}
diff --git a/snippets/csharp/System/TimeSpan/Parse/Project.csproj b/snippets/csharp/System/TimeSpan/Parse/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Parse/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/TimeSpan/Parse/parse1.cs b/snippets/csharp/System/TimeSpan/Parse/parse1.cs
index 164e268a774..0604062b069 100644
--- a/snippets/csharp/System/TimeSpan/Parse/parse1.cs
+++ b/snippets/csharp/System/TimeSpan/Parse/parse1.cs
@@ -3,20 +3,20 @@
using System.Globalization;
using System.Threading;
-public class Example
+public class Example1
{
public static void Main()
{
- string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
- "6.12:14:45", "6:12:14:45.3448",
+ string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
+ "6.12:14:45", "6:12:14:45.3448",
"6:12:14:45,3448", "6:34:14:45" };
string[] cultureNames = { "hr-HR", "en-US"};
-
+
// Change the current culture.
foreach (string cultureName in cultureNames)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
- Console.WriteLine("Current Culture: {0}",
+ Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.Name);
foreach (string value in values)
{
@@ -26,12 +26,12 @@ public static void Main()
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
- }
+ }
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
- }
- Console.WriteLine();
+ }
+ Console.WriteLine();
}
}
}
@@ -45,7 +45,7 @@ public static void Main()
// 6:12:14:45.3448: Bad Format
// 6:12:14:45,3448 --> 6.12:14:45.3448000
// 6:34:14:45: Overflow
-//
+//
// Current Culture: en-US
// 6 --> 6.00:00:00
// 6:12 --> 06:12:00
@@ -55,4 +55,4 @@ public static void Main()
// 6:12:14:45.3448 --> 6.12:14:45.3448000
// 6:12:14:45,3448: Bad Format
// 6:34:14:45: Overflow
-//
\ No newline at end of file
+//
diff --git a/snippets/csharp/System/TimeSpan/Parse/parse2.cs b/snippets/csharp/System/TimeSpan/Parse/parse2.cs
index 4a29c755743..fe7eb0fef9a 100644
--- a/snippets/csharp/System/TimeSpan/Parse/parse2.cs
+++ b/snippets/csharp/System/TimeSpan/Parse/parse2.cs
@@ -3,25 +3,25 @@
using System.Globalization;
using System.Text.RegularExpressions;
-public class Example
+public class Example2
{
public static void Main()
{
- string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
- "6.12:14:45", "6:12:14:45.3448",
+ string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
+ "6.12:14:45", "6:12:14:45.3448",
"6:12:14:45,3448", "6:34:14:45" };
- CultureInfo[] cultures = { new CultureInfo("en-US"),
+ CultureInfo[] cultures = { new CultureInfo("en-US"),
new CultureInfo("ru-RU"),
CultureInfo.InvariantCulture };
-
+
string header = String.Format("{0,-17}", "String");
foreach (CultureInfo culture in cultures)
- header += culture.Equals(CultureInfo.InvariantCulture) ?
+ header += culture.Equals(CultureInfo.InvariantCulture) ?
String.Format("{0,20}", "Invariant") :
String.Format("{0,20}", culture.Name);
Console.WriteLine(header);
Console.WriteLine();
-
+
foreach (string value in values)
{
Console.Write("{0,-17}", value);
@@ -33,18 +33,18 @@ public static void Main()
}
catch (FormatException) {
Console.Write("{0,20}", "Bad Format");
- }
+ }
catch (OverflowException) {
Console.Write("{0,20}", "Overflow");
- }
+ }
}
- Console.WriteLine();
+ Console.WriteLine();
}
}
}
// The example displays the following output:
// String en-US ru-RU Invariant
-//
+//
// 6 6.00:00:00 6.00:00:00 6.00:00:00
// 6:12 06:12:00 06:12:00 06:12:00
// 6:12:14 06:12:14 06:12:14 06:12:14
@@ -53,4 +53,4 @@ public static void Main()
// 6:12:14:45.3448 6.12:14:45.3448000 Bad Format 6.12:14:45.3448000
// 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
// 6:34:14:45 Overflow Overflow Overflow
-//
\ No newline at end of file
+//
diff --git a/snippets/csharp/System/TimeSpan/Parse/parsefailure1.cs b/snippets/csharp/System/TimeSpan/Parse/parsefailure1.cs
new file mode 100644
index 00000000000..8ddbd82d5f0
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/Parse/parsefailure1.cs
@@ -0,0 +1,31 @@
+using System;
+
+public class Example3
+{
+ public static void Main()
+ {
+ //
+ string[] values = { "000000006", "12.12:12:12.12345678" };
+ foreach (string value in values)
+ {
+ try {
+ TimeSpan interval = TimeSpan.Parse(value);
+ Console.WriteLine($"{value} --> {interval}");
+ }
+ catch (FormatException) {
+ Console.WriteLine($"{value}: Bad Format");
+ }
+ catch (OverflowException) {
+ Console.WriteLine($"{value}: Overflow");
+ }
+ }
+
+ // Output from .NET Framework 3.5 and earlier versions:
+ // 000000006 --> 6.00:00:00
+ // 12.12:12:12.12345678: Bad Format
+ // Output from .NET Framework 4 and later versions or .NET Core:
+ // 000000006: Overflow
+ // 12.12:12:12.12345678: Overflow
+ //
+ }
+}
diff --git a/snippets/csharp/System/TimeSpan/TryParse/Program.cs b/snippets/csharp/System/TimeSpan/TryParse/Program.cs
new file mode 100644
index 00000000000..7855a5dcc15
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/TryParse/Program.cs
@@ -0,0 +1 @@
+TryParse.Main();
diff --git a/snippets/csharp/System/TimeSpan/TryParse/Project.csproj b/snippets/csharp/System/TimeSpan/TryParse/Project.csproj
new file mode 100644
index 00000000000..f99395b4b2b
--- /dev/null
+++ b/snippets/csharp/System/TimeSpan/TryParse/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Type/GetType/Project.csproj b/snippets/csharp/System/Type/GetType/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/Type/GetType/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Type/GetType/source.cs b/snippets/csharp/System/Type/GetType/source.cs
new file mode 100644
index 00000000000..cdf553928e0
--- /dev/null
+++ b/snippets/csharp/System/Type/GetType/source.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Reflection;
+
+class Example
+{
+ static void Main()
+ {
+ string test = "System.Collections.Generic.Dictionary`2[System.String,[MyNamespace.MyType, MyAssembly]]";
+ //
+ Type t = Type.GetType(test,
+ (aName) => aName.Name == "MyAssembly" ?
+ Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") : null,
+ (assem, name, ignore) => assem == null ?
+ Type.GetType(name, false, ignore) :
+ assem.GetType(name, false, ignore)
+ );
+ //
+ Console.WriteLine(t);
+
+ test = "System.Collections.Generic.Dictionary`2[[YourNamespace.YourType, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], [MyNamespace.MyType, MyAssembly]]";
+ //
+ Type t2 = Type.GetType(test,
+ (aName) => aName.Name == "MyAssembly" ?
+ Assembly.LoadFrom(@".\MyPath\v5.0\MyAssembly.dll") :
+ Assembly.Load(aName),
+ (assem, name, ignore) => assem == null ?
+ Type.GetType(name, false, ignore) :
+ assem.GetType(name, false, ignore), true
+ );
+ //
+ Console.WriteLine(t2);
+ }
+}
diff --git a/snippets/csharp/System/Type/GetType/type_gettype.cs b/snippets/csharp/System/Type/GetType/type_gettype.cs
index f429942b207..c0345bf6a7a 100644
--- a/snippets/csharp/System/Type/GetType/type_gettype.cs
+++ b/snippets/csharp/System/Type/GetType/type_gettype.cs
@@ -1,7 +1,7 @@
//
using System;
-class Example
+class GetTypeExample
{
public static void Main()
{
diff --git a/snippets/csharp/System/Type/MakeGenericType/remarks.cs b/snippets/csharp/System/Type/MakeGenericType/remarks.cs
new file mode 100644
index 00000000000..a9f8c04adb2
--- /dev/null
+++ b/snippets/csharp/System/Type/MakeGenericType/remarks.cs
@@ -0,0 +1,22 @@
+using System;
+
+//
+public class Base { }
+public class Derived : Base { }
+//
+
+//
+public class Outermost
+{
+ public class Inner
+ {
+ public class Innermost1 {}
+ public class Innermost2 {}
+ }
+}
+//
+
+class ProgStubClass
+{
+ public static void Main() {}
+}
\ No newline at end of file
diff --git a/snippets/csharp/System/Type/Overview/Equals1.cs b/snippets/csharp/System/Type/Overview/Equals1.cs
new file mode 100644
index 00000000000..e3a44d20d3f
--- /dev/null
+++ b/snippets/csharp/System/Type/Overview/Equals1.cs
@@ -0,0 +1,27 @@
+using System;
+
+public class Example1
+{
+ public static void Main()
+ {
+ //
+ long number1 = 1635429;
+ int number2 = 16203;
+ double number3 = 1639.41;
+ long number4 = 193685412;
+
+ // Get the type of number1.
+ Type t = number1.GetType();
+
+ // Compare types of all objects with number1.
+ Console.WriteLine($"Type of number1 and number2 are equal: {Object.ReferenceEquals(t, number2.GetType())}");
+ Console.WriteLine($"Type of number1 and number3 are equal: {Object.ReferenceEquals(t, number3.GetType())}");
+ Console.WriteLine($"Type of number1 and number4 are equal: {Object.ReferenceEquals(t, number4.GetType())}");
+
+ // The example displays the following output:
+ // Type of number1 and number2 are equal: False
+ // Type of number1 and number3 are equal: False
+ // Type of number1 and number4 are equal: True
+ //
+ }
+}
diff --git a/snippets/csharp/System/Type/Overview/GetType1.cs b/snippets/csharp/System/Type/Overview/GetType1.cs
new file mode 100644
index 00000000000..ecf2365b6d1
--- /dev/null
+++ b/snippets/csharp/System/Type/Overview/GetType1.cs
@@ -0,0 +1,20 @@
+using System;
+
+public class Example2
+{
+ public static void Main()
+ {
+ //
+ object[] values = { "word", true, 120, 136.34, 'a' };
+ foreach (var value in values)
+ Console.WriteLine($"{value} - type {value.GetType().Name}");
+
+ // The example displays the following output:
+ // word - type String
+ // True - type Boolean
+ // 120 - type Int32
+ // 136.34 - type Double
+ // a - type Char
+ //
+ }
+}
diff --git a/snippets/csharp/System/Type/Overview/Project.csproj b/snippets/csharp/System/Type/Overview/Project.csproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/csharp/System/Type/Overview/Project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Type/Overview/source.cs b/snippets/csharp/System/Type/Overview/source.cs
index e74fdd1f265..2e479978b80 100644
--- a/snippets/csharp/System/Type/Overview/source.cs
+++ b/snippets/csharp/System/Type/Overview/source.cs
@@ -5,16 +5,16 @@
using System;
using System.Reflection;
-class Example
+class Example3
{
static void Main()
{
Type t = typeof(String);
- MethodInfo substr = t.GetMethod("Substring",
+ MethodInfo substr = t.GetMethod("Substring",
new Type[] { typeof(int), typeof(int) });
- Object result =
+ Object result =
substr.Invoke("Hello, World!", new Object[] { 7, 5 });
Console.WriteLine("{0} returned \"{1}\".", substr, result);
}
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/Missing1.cs b/snippets/csharp/System/TypeInitializationException/Overview/Missing1.cs
new file mode 100644
index 00000000000..b6d72e3e237
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/Missing1.cs
@@ -0,0 +1,47 @@
+//
+using System;
+
+public class MissingEx1
+{
+ public static void Main()
+ {
+ Person p = new Person("John", "Doe");
+ Console.WriteLine(p);
+ }
+}
+
+public class Person
+{
+ static readonly InfoModule s_infoModule;
+
+ readonly string _fName;
+ readonly string _lName;
+
+ static Person()
+ {
+ s_infoModule = new InfoModule(DateTime.UtcNow);
+ }
+
+ public Person(string fName, string lName)
+ {
+ _fName = fName;
+ _lName = lName;
+ s_infoModule.Increment();
+ }
+
+ public override string ToString()
+ {
+ return string.Format("{0} {1}", _fName, _lName);
+ }
+}
+// The example displays the following output if missing1a.dll is renamed or removed:
+// Unhandled Exception: System.TypeInitializationException:
+// The type initializer for 'Person' threw an exception. --->
+// System.IO.FileNotFoundException: Could not load file or assembly
+// 'Missing1a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
+// or one of its dependencies. The system cannot find the file specified.
+// at Person..cctor()
+// --- End of inner exception stack trace ---
+// at Person..ctor(String fName, String lName)
+// at Example.Main()
+//
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/Missing1a.cs b/snippets/csharp/System/TypeInitializationException/Overview/Missing1a.cs
new file mode 100644
index 00000000000..56903779fba
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/Missing1a.cs
@@ -0,0 +1,24 @@
+//
+using System;
+
+public class InfoModule
+{
+ private DateTime firstUse;
+ private int ctr = 0;
+
+ public InfoModule(DateTime dat)
+ {
+ firstUse = dat;
+ }
+
+ public int Increment()
+ {
+ return ++ctr;
+ }
+
+ public DateTime GetInitializationTime()
+ {
+ return firstUse;
+ }
+}
+//
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/Program.cs b/snippets/csharp/System/TypeInitializationException/Overview/Program.cs
new file mode 100644
index 00000000000..b397c42b9a8
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/Program.cs
@@ -0,0 +1 @@
+RegexEx1.Run();
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/Regex1.cs b/snippets/csharp/System/TypeInitializationException/Overview/Regex1.cs
new file mode 100644
index 00000000000..dfeda845b44
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/Regex1.cs
@@ -0,0 +1,33 @@
+//
+using System;
+using System.Text.RegularExpressions;
+using static System.Net.Mime.MediaTypeNames;
+
+public class RegexEx1
+{
+ public static void Run()
+ {
+ AppDomain domain = AppDomain.CurrentDomain;
+ // Set a timeout interval of -2 seconds.
+ domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(-2));
+
+ Regex rgx = new Regex("[aeiouy]");
+ Console.WriteLine($"Regular expression pattern: {rgx.ToString()}");
+ Console.WriteLine($"Timeout interval for this regex: {rgx.MatchTimeout.TotalSeconds} seconds");
+ }
+}
+
+// The example displays the following output:
+
+// Unhandled exception. System.TypeInitializationException:
+// The type initializer for 'System.Text.RegularExpressions.Regex' threw an exception. --->
+// System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
+// (Parameter 'AppDomain data 'REGEX_DEFAULT_MATCH_TIMEOUT' contains the invalid value or object
+// '-00:00:02' for specifying a default matching timeout for System.Text.RegularExpressions.Regex.')
+// at System.Text.RegularExpressions.Regex.InitDefaultMatchTimeout()
+// at System.Text.RegularExpressions.Regex..cctor()
+// --- End of inner exception stack trace ---
+// at System.Text.RegularExpressions.Regex..ctor(String pattern, CultureInfo culture)
+// at RegexEx1.Run()
+
+//
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/ctorException1.cs b/snippets/csharp/System/TypeInitializationException/Overview/ctorException1.cs
new file mode 100644
index 00000000000..ac866e10443
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/ctorException1.cs
@@ -0,0 +1,33 @@
+//
+using System;
+
+public class Example
+{
+ private static TestClass test = new TestClass(3);
+
+ public static void Main()
+ {
+ Example ex = new Example();
+ Console.WriteLine(test.Value);
+ }
+}
+
+public class TestClass
+{
+ public readonly int Value;
+
+ public TestClass(int value)
+ {
+ if (value < 0 || value > 1) throw new ArgumentOutOfRangeException(nameof(value));
+ Value = value;
+ }
+}
+// The example displays the following output:
+// Unhandled Exception: System.TypeInitializationException:
+// The type initializer for 'Example' threw an exception. --->
+// System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
+// at TestClass..ctor(Int32 value)
+// at Example..cctor()
+// --- End of inner exception stack trace ---
+// at Example.Main()
+//
diff --git a/snippets/csharp/System/TypeInitializationException/Overview/project.csproj b/snippets/csharp/System/TypeInitializationException/Overview/project.csproj
new file mode 100644
index 00000000000..a15a29bf12c
--- /dev/null
+++ b/snippets/csharp/System/TypeInitializationException/Overview/project.csproj
@@ -0,0 +1,8 @@
+
+
+
+ Exe
+ net10.0
+
+
+
diff --git a/snippets/csharp/System/Version/Overview/GettingVersions1.cs b/snippets/csharp/System/Version/Overview/GettingVersions1.cs
new file mode 100644
index 00000000000..7d914d90746
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/GettingVersions1.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Reflection;
+
+[assembly: CLSCompliant(true)]
+public class Class1
+{
+ public static void Run()
+ {
+ GetOsVersion();
+ Console.WriteLine();
+ GetClrVersion();
+ Console.WriteLine();
+ }
+
+ private static void GetOsVersion()
+ {
+ //
+ // Get the operating system version.
+ OperatingSystem os = Environment.OSVersion;
+ Version ver = os.Version;
+ Console.WriteLine($"Operating System: {os.VersionString} ({ver})");
+ //
+ }
+
+ private static void GetClrVersion()
+ {
+ //
+ // Get the common language runtime version.
+ Version ver = Environment.Version;
+ Console.WriteLine($"CLR Version {ver}");
+ //
+ }
+}
diff --git a/snippets/csharp/System/Version/Overview/Program.cs b/snippets/csharp/System/Version/Overview/Program.cs
new file mode 100644
index 00000000000..22abad5dddf
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/Program.cs
@@ -0,0 +1 @@
+Class1.Run();
diff --git a/snippets/csharp/System/Version/Overview/Project.csproj b/snippets/csharp/System/Version/Overview/Project.csproj
new file mode 100644
index 00000000000..8810b14fdbd
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/Project.csproj
@@ -0,0 +1,9 @@
+
+
+
+ Exe
+ net10.0
+ false
+
+
+
diff --git a/snippets/csharp/System/Version/Overview/comparisons1.cs b/snippets/csharp/System/Version/Overview/comparisons1.cs
new file mode 100644
index 00000000000..67c94f8978e
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/comparisons1.cs
@@ -0,0 +1,34 @@
+using System;
+
+public class Example7
+{
+ public static void Main()
+ {
+ CompareSimple();
+ }
+
+ private static void CompareSimple()
+ {
+ //
+ Version v1 = new(2, 0);
+ Version v2 = new("2.1");
+ Console.Write("Version {0} is ", v1);
+ switch(v1.CompareTo(v2))
+ {
+ case 0:
+ Console.Write("the same as");
+ break;
+ case 1:
+ Console.Write("later than");
+ break;
+ case -1:
+ Console.Write("earlier than");
+ break;
+ }
+ Console.WriteLine($" Version {v2}.");
+
+ // The example displays the following output:
+ // Version 2.0 is earlier than Version 2.1.
+ //
+ }
+}
diff --git a/snippets/csharp/System/Version/Overview/comparisons2.cs b/snippets/csharp/System/Version/Overview/comparisons2.cs
new file mode 100644
index 00000000000..e4f6dc58c92
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/comparisons2.cs
@@ -0,0 +1,27 @@
+//
+using System;
+
+enum VersionTime {Earlier = -1, Same = 0, Later = 1 };
+
+public class Example2
+{
+ public static void Main()
+ {
+ Version v1 = new(1, 1);
+ Version v1a = new("1.1.0");
+ ShowRelationship(v1, v1a);
+
+ Version v1b = new(1, 1, 0, 0);
+ ShowRelationship(v1b, v1a);
+ }
+
+ private static void ShowRelationship(Version v1, Version v2)
+ {
+ Console.WriteLine($"Relationship of {v1} to {v2}: {(VersionTime) v1.CompareTo(v2)}");
+ }
+}
+
+// The example displays the following output:
+// Relationship of 1.1 to 1.1.0: Earlier
+// Relationship of 1.1.0.0 to 1.1.0: Later
+//
diff --git a/snippets/csharp/System/Version/Overview/currentapp.cs b/snippets/csharp/System/Version/Overview/currentapp.cs
new file mode 100644
index 00000000000..0a932351d50
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/currentapp.cs
@@ -0,0 +1,16 @@
+//
+using System;
+using System.Reflection;
+
+public class Example4
+{
+ public static void Main()
+ {
+ // Get the version of the executing assembly (that is, this assembly).
+ Assembly assem = Assembly.GetEntryAssembly();
+ AssemblyName assemName = assem.GetName();
+ Version ver = assemName.Version;
+ Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString());
+ }
+}
+//
diff --git a/snippets/csharp/System/Version/Overview/currentassem.cs b/snippets/csharp/System/Version/Overview/currentassem.cs
new file mode 100644
index 00000000000..e83f32b25e7
--- /dev/null
+++ b/snippets/csharp/System/Version/Overview/currentassem.cs
@@ -0,0 +1,16 @@
+//
+using System;
+using System.Reflection;
+
+public class Example3
+{
+ public static void Main()
+ {
+ // Get the version of the current assembly.
+ Assembly assem = typeof(Example3).Assembly;
+ AssemblyName assemName = assem.GetName();
+ Version ver = assemName.Version;
+ Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString());
+ }
+}
+//
diff --git a/snippets/csharp/System/Version/Overview/example1.cs b/snippets/csharp/System/Version/Overview/example1.cs
index c210f946275..e595209c12f 100644
--- a/snippets/csharp/System/Version/Overview/example1.cs
+++ b/snippets/csharp/System/Version/Overview/example1.cs
@@ -16,6 +16,7 @@ public static void Main()
Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name);
}
}
+
// The example displays the following output:
// This is version 2.0.1.0 of Example1.
//
diff --git a/snippets/fsharp/System/FormatException/Overview/FormatExample2.fs b/snippets/fsharp/System/FormatException/Overview/FormatExample2.fs
new file mode 100644
index 00000000000..0cf9b0f18d8
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/FormatExample2.fs
@@ -0,0 +1,66 @@
+module FormatExample2
+
+//
+open System
+
+type CustomerFormatter() =
+ interface IFormatProvider with
+ member this.GetFormat(formatType) =
+ if formatType = typeof then
+ this
+ else
+ null
+
+ interface ICustomFormatter with
+ member this.Format(format, arg, formatProvider: IFormatProvider) =
+ if this.Equals formatProvider |> not then
+ null
+ else
+ let format =
+ if String.IsNullOrEmpty format then "G"
+ else format.ToUpper()
+
+ let customerString =
+ let s = string arg
+ if s.Length < 8 then
+ s.PadLeft(8, '0')
+ else s
+
+ match format with
+ | "G" ->
+ customerString.Substring(0, 1) + "-" +
+ customerString.Substring(1, 5) + "-" +
+ customerString.Substring 6
+ | "S" ->
+ customerString.Substring(0, 1) + "/" +
+ customerString.Substring(1, 5) + "/" +
+ customerString.Substring 6
+ | "P" ->
+ customerString.Substring(0, 1) + "." +
+ customerString.Substring(1, 5) + "." +
+ customerString.Substring 6
+ | _ ->
+ raise (FormatException $"The '{format}' format specifier is not supported.")
+
+let acctNumber = 79203159
+String.Format(CustomerFormatter(), "{0}", acctNumber)
+|> printfn "%s"
+String.Format(CustomerFormatter(), "{0:G}", acctNumber)
+|> printfn "%s"
+String.Format(CustomerFormatter(), "{0:S}", acctNumber)
+|> printfn "%s"
+String.Format(CustomerFormatter(), "{0:P}", acctNumber)
+|> printfn "%s"
+try
+ String.Format(CustomerFormatter(), "{0:X}", acctNumber)
+ |> printfn "%s"
+with :? FormatException as e ->
+ printfn $"{e.Message}"
+
+// The example displays the following output:
+// 7-92031-59
+// 7-92031-59
+// 7/92031/59
+// 7.92031.59
+// The 'X' format specifier is not supported.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/formatoverload1.fs b/snippets/fsharp/System/FormatException/Overview/formatoverload1.fs
new file mode 100644
index 00000000000..7365178fd89
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/formatoverload1.fs
@@ -0,0 +1,13 @@
+module formatoverload1
+
+//
+open System
+
+let dat = DateTime(2012, 1, 17, 9, 30, 0)
+let city = "Chicago"
+let temp = -16
+String.Format("At {0} in {1}, the temperature was {2} degrees.", dat, city, temp)
+|> printfn "%s"
+// The example displays output like the following:
+// At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/formatoverload2.fs b/snippets/fsharp/System/FormatException/Overview/formatoverload2.fs
new file mode 100644
index 00000000000..76ebd80b44e
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/formatoverload2.fs
@@ -0,0 +1,29 @@
+module formatoverload2
+
+open System
+
+//
+// Create a list of 5-tuples with population data for three U.S. cities, 1940-1950.
+let cities =
+ [ "Los Angeles", DateTime(1940, 1, 1), 1504277, DateTime(1950, 1, 1), 1970358
+ "New York", DateTime(1940, 1, 1), 7454995, DateTime(1950, 1, 1), 7891957
+ "Chicago", DateTime(1940, 1, 1), 3396808, DateTime(1950, 1, 1), 3620962
+ "Detroit", DateTime(1940, 1, 1), 1623452, DateTime(1950, 1, 1), 1849568 ]
+
+// Display header
+String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", "City", "Year", "Population", "Change (%)")
+|> printfn "%s"
+
+for name, year1, pop1, year2, pop2 in cities do
+ String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
+ name, year1, pop1, year2, pop2,
+ double (pop2 - pop1) / double pop1)
+ |> printfn "%s"
+// The example displays the following output:
+// City Year Population Year Population Change (%)
+//
+// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
+// New York 1940 7,454,995 1950 7,891,957 5.9 %
+// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
+// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/formatsyntax1.fs b/snippets/fsharp/System/FormatException/Overview/formatsyntax1.fs
new file mode 100644
index 00000000000..a743dbc0b0d
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/formatsyntax1.fs
@@ -0,0 +1,8 @@
+module formatsyntax1
+
+//
+open System
+
+String.Format("{0,-10:C}", 126347.89m)
+|> printfn "%s"
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/interceptor2.fs b/snippets/fsharp/System/FormatException/Overview/interceptor2.fs
new file mode 100644
index 00000000000..f45c4c0ea79
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/interceptor2.fs
@@ -0,0 +1,109 @@
+module interceptor2
+
+//
+open System
+open System.Globalization
+
+type InterceptProvider() =
+ interface IFormatProvider with
+ member this.GetFormat(formatType) =
+ if formatType = typeof then
+ this
+ else
+ null
+ interface ICustomFormatter with
+ member _.Format(format, obj, provider: IFormatProvider) =
+ // Display information about method call.
+ let formatString =
+ if format = null then "" else format
+ printfn $"Provider: {provider.GetType().Name}, Object: %A{obj}, Format String: %s{formatString}"
+
+ if obj = null then
+ String.Empty
+ else
+ // If this is a byte and the "R" format string, format it with Roman numerals.
+ match obj with
+ | :? byte as value when formatString.ToUpper().Equals "R" ->
+ let mutable returnString = String.Empty
+
+ // Get the hundreds digit(s)
+ let struct (result, remainder) = Math.DivRem(value, 100uy)
+ if result > 0uy then
+ returnString <- String('C', int result)
+ let value = byte remainder
+ // Get the 50s digit
+ let struct (result, remainder) = Math.DivRem(value, 50uy)
+ if result = 1uy then
+ returnString <- returnString + "L"
+ let value = byte remainder
+ // Get the tens digit.
+ let struct (result, remainder) = Math.DivRem(value, 10uy)
+ if result > 0uy then
+ returnString <- returnString + String('X', int result)
+ let value = byte remainder
+ // Get the fives digit.
+ let struct (result, remainder) = Math.DivRem(value, 5uy)
+ if result > 0uy then
+ returnString <- returnString + "V"
+ let value = byte remainder
+ // Add the ones digit.
+ if remainder > 0uy then
+ returnString <- returnString + String('I', int remainder)
+
+ // Check whether we have too many X characters.
+ let pos = returnString.IndexOf "XXXX"
+ if pos >= 0 then
+ let xPos = returnString.IndexOf "L"
+ returnString <-
+ if xPos >= 0 && xPos = pos - 1 then
+ returnString.Replace("LXXXX", "XC")
+ else
+ returnString.Replace("XXXX", "XL")
+ // Check whether we have too many I characters
+ let pos = returnString.IndexOf "IIII"
+ if pos >= 0 then
+ returnString <-
+ if returnString.IndexOf "V" >= 0 then
+ returnString.Replace("VIIII", "IX")
+ else
+ returnString.Replace("IIII", "IV")
+ returnString
+
+ // Use default for all other formatting.
+ | :? IFormattable as x ->
+ x.ToString(format, CultureInfo.CurrentCulture)
+ | _ ->
+ string obj
+
+let n = 10
+let value = 16.935
+let day = DateTime.Now
+let provider = InterceptProvider()
+String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day)
+|> printfn "%s"
+String.Format(provider, "{0}: {1:F}\n", "Today: ", DateTime.Now.DayOfWeek)
+|> printfn "%s"
+String.Format(provider, "{0:X}, {1}, {2}\n", 2uy, 12uy, 199uy)
+|> printfn "%s"
+String.Format(provider, "{0:R}, {1:R}, {2:R}\n", 2uy, 12uy, 199uy)
+|> printfn "%s"
+// The example displays the following output:
+// Provider: InterceptProvider, Object: 10, Format String: N0
+// Provider: InterceptProvider, Object: 16.935, Format String: C2
+// Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
+// 10: $16.94 on 1/31/2013
+//
+// Provider: InterceptProvider, Object: Today: , Format String:
+// Provider: InterceptProvider, Object: Thursday, Format String: F
+// Today: : Thursday
+//
+// Provider: InterceptProvider, Object: 2, Format String: X
+// Provider: InterceptProvider, Object: 12, Format String:
+// Provider: InterceptProvider, Object: 199, Format String:
+// 2, 12, 199
+//
+// Provider: InterceptProvider, Object: 2, Format String: R
+// Provider: InterceptProvider, Object: 12, Format String: R
+// Provider: InterceptProvider, Object: 199, Format String: R
+// II, XII, CXCIX
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa-interpolated1.fs b/snippets/fsharp/System/FormatException/Overview/qa-interpolated1.fs
new file mode 100644
index 00000000000..f29e8e4b141
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa-interpolated1.fs
@@ -0,0 +1,18 @@
+module qa_interpolated1
+
+//
+open System
+
+let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
+let output =
+ names[0] + ", " + names[1] + ", " + names[2] + ", " +
+ names[3] + ", " + names[4] + ", " + names[5] + ", " +
+ names[6] + "\n"
+
+let date = DateTime.Now
+output + String.Format("It is {0:t} on {0:d}. The day of the week is {1}.", date, date.DayOfWeek)
+|> printfn "%s"
+// The example displays the following output:
+// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa-interpolated2.fs b/snippets/fsharp/System/FormatException/Overview/qa-interpolated2.fs
new file mode 100644
index 00000000000..a70413c32d7
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa-interpolated2.fs
@@ -0,0 +1,15 @@
+module qa_interpolated2
+
+//
+open System
+
+let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
+let output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, {names[5]}, {names[6]}"
+
+let date = DateTime.Now
+output + $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}."
+|> printfn "%s"
+// The example displays the following output:
+// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa11.fs b/snippets/fsharp/System/FormatException/Overview/qa11.fs
new file mode 100644
index 00000000000..3d152075bed
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa11.fs
@@ -0,0 +1,15 @@
+module qa11
+
+//
+open System
+
+let rnd = Random()
+let mutable total = 0
+let numbers = Array.zeroCreate 4
+for i = 0 to 2 do
+ let number = rnd.Next 1001
+ numbers[i] <- number
+ total <- total + number
+numbers[3] <- total
+Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa21.fs b/snippets/fsharp/System/FormatException/Overview/qa21.fs
new file mode 100644
index 00000000000..56dc9afe390
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa21.fs
@@ -0,0 +1,17 @@
+module qa21
+
+//
+open System
+
+let rnd = Random()
+let numbers = Array.zeroCreate 4
+let mutable total = 0
+for i = 0 to 2 do
+ let number = rnd.Next 1001
+ numbers[i] <- number
+ total <- total + number
+numbers[3] <- total
+let values = Array.zeroCreate numbers.Length
+numbers.CopyTo(values, 0)
+Console.WriteLine("{0} + {1} + {2} = {3}", values)
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa26.fs b/snippets/fsharp/System/FormatException/Overview/qa26.fs
new file mode 100644
index 00000000000..308535944f9
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa26.fs
@@ -0,0 +1,16 @@
+module qa26
+
+//
+open System
+
+let values: obj list = [ 1603, 1794.68235, 15436.14 ]
+for value in values do
+ String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}\n", Convert.ToDouble(value), Convert.ToDouble(value) / 10000.)
+ |> printfn "%s"
+// The example displays output like the following:
+// $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
+//
+// $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
+//
+// $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa27.fs b/snippets/fsharp/System/FormatException/Overview/qa27.fs
new file mode 100644
index 00000000000..dde74671a0e
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa27.fs
@@ -0,0 +1,11 @@
+module qa27
+
+open System
+
+//
+let value = 16309.5436m
+String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}", value)
+|> printfn "%s"
+// The example displays the following output:
+// 16309.54360 16,309.54 16309.544
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa28.fs b/snippets/fsharp/System/FormatException/Overview/qa28.fs
new file mode 100644
index 00000000000..dbb92793763
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa28.fs
@@ -0,0 +1,11 @@
+module qa28
+
+//
+open System
+
+let value = 16342
+String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}", value)
+|> printfn "%s"
+// The example displays the following output:
+// 00016342 00016342.000 0,000,016,342.0
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/qa29.fs b/snippets/fsharp/System/FormatException/Overview/qa29.fs
new file mode 100644
index 00000000000..74ab5f7281b
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/qa29.fs
@@ -0,0 +1,11 @@
+module qa29
+
+//
+open System
+
+let value = 1326
+String.Format("{0,10:D6} {0,10:X8}", value)
+|> printfn "%s"
+// The example displays the following output:
+// 001326 0000052E
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/starting1.fs b/snippets/fsharp/System/FormatException/Overview/starting1.fs
new file mode 100644
index 00000000000..3ea79b6661f
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/starting1.fs
@@ -0,0 +1,44 @@
+module starting1
+
+open System
+
+let snippet31 () =
+ //
+ String.Format("At {0}, the temperature is {1}°C.", DateTime.Now, 20.4)
+ |> printfn "%s"
+ // Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
+ //
+
+let snippet32 () =
+ //
+ String.Format("It is now {0:d} at {0:t}", DateTime.Now)
+ |> printfn "%s"
+ // Output similar to: 'It is now 4/10/2015 at 10:04 AM'
+ //
+
+let snippet34 () =
+ //
+ let years = [| 2013; 2014; 2015 |]
+ let population = [| 1025632; 1105967; 1148203 |]
+ let mutable s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population")
+ for i = 0 to years.Length - 1 do
+ s <- s + String.Format("{0,-10} {1,-10:N0}\n", years[i], population[i])
+ printfn $"\n{s}"
+ // Result:
+ // Year Population
+ //
+ // 2013 1,025,632
+ // 2014 1,105,967
+ // 2015 1,148,203
+ //
+
+//
+let temp = 20.4m
+String.Format("The temperature is {0}°C.", temp)
+|> printfn "%s"
+// Displays 'The temperature is 20.4°C.'
+//
+
+snippet31 ()
+snippet32 ()
+snippet34 ()
\ No newline at end of file
diff --git a/snippets/fsharp/System/FormatException/Overview/starting2.fs b/snippets/fsharp/System/FormatException/Overview/starting2.fs
new file mode 100644
index 00000000000..9680d1bbabb
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/starting2.fs
@@ -0,0 +1,20 @@
+module starting2
+
+open System
+
+let showFormatted () =
+ //
+ let pricePerOunce = 17.36m
+ String.Format("The current price is {0:C2} per ounce.", pricePerOunce)
+ |> printfn "%s"
+ // Result if current culture is en-US:
+ // The current price is $17.36 per ounce.
+ //
+
+//
+let pricePerOunce = 17.36m
+String.Format("The current price is {0} per ounce.", pricePerOunce)
+|> printfn "%s"
+// Result: The current price is 17.36 per ounce.
+//
+showFormatted ()
diff --git a/snippets/fsharp/System/FormatException/Overview/starting3.fs b/snippets/fsharp/System/FormatException/Overview/starting3.fs
new file mode 100644
index 00000000000..0d881960c3a
--- /dev/null
+++ b/snippets/fsharp/System/FormatException/Overview/starting3.fs
@@ -0,0 +1,22 @@
+module starting3
+
+//
+open System
+open System.Text
+
+let years = [| 2013; 2014; 2015 |]
+let population = [| 1025632; 1105967; 1148203 |]
+let sb = StringBuilder()
+sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population")) |> ignore
+for i = 0 to years.Length - 1 do
+ sb.Append(String.Format("{0,6} {1,15:N0}\n", years[i], population[i])) |> ignore
+
+printfn $"{sb}"
+
+// Result:
+// Year Population
+//
+// 2013 1,025,632
+// 2014 1,105,967
+// 2015 1,148,203
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/Random/Overview/Random1.fs b/snippets/fsharp/System/Random/Overview/Random1.fs
new file mode 100644
index 00000000000..0017b215d5b
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/Random1.fs
@@ -0,0 +1,50 @@
+module Random1
+
+open System
+
+//
+let bytes1 = Array.zeroCreate 100
+let bytes2 = Array.zeroCreate 100
+let rnd1 = Random()
+let rnd2 = Random()
+
+rnd1.NextBytes bytes1
+rnd2.NextBytes bytes2
+
+printfn "First Series"
+for i = bytes1.GetLowerBound 0 to bytes1.GetUpperBound 0 do
+ printf "%5i" bytes1.[i]
+ if (i + 1) % 10 = 0 then printfn ""
+
+printfn ""
+
+printfn "Second Series"
+for i = bytes2.GetLowerBound 0 to bytes2.GetUpperBound 0 do
+ printf "%5i" bytes2.[i]
+ if (i + 1) % 10 = 0 then printfn ""
+
+// The example displays output like the following:
+// First Series:
+// 97 129 149 54 22 208 120 105 68 177
+// 113 214 30 172 74 218 116 230 89 18
+// 12 112 130 105 116 180 190 200 187 120
+// 7 198 233 158 58 51 50 170 98 23
+// 21 1 113 74 146 245 34 255 96 24
+// 232 255 23 9 167 240 255 44 194 98
+// 18 175 173 204 169 171 236 127 114 23
+// 167 202 132 65 253 11 254 56 214 127
+// 145 191 104 163 143 7 174 224 247 73
+// 52 6 231 255 5 101 83 165 160 231
+//
+// Second Series:
+// 97 129 149 54 22 208 120 105 68 177
+// 113 214 30 172 74 218 116 230 89 18
+// 12 112 130 105 116 180 190 200 187 120
+// 7 198 233 158 58 51 50 170 98 23
+// 21 1 113 74 146 245 34 255 96 24
+// 232 255 23 9 167 240 255 44 194 98
+// 18 175 173 204 169 171 236 127 114 23
+// 167 202 132 65 253 11 254 56 214 127
+// 145 191 104 163 143 7 174 224 247 73
+// 52 6 231 255 5 101 83 165 160 231
+//
diff --git a/snippets/fsharp/System/Random/Overview/array1.fs b/snippets/fsharp/System/Random/Overview/array1.fs
new file mode 100644
index 00000000000..ca56497d573
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/array1.fs
@@ -0,0 +1,21 @@
+module Array1
+
+open System
+
+//
+let cities =
+ [| "Atlanta"; "Boston"; "Chicago"; "Detroit";
+ "Fort Wayne"; "Greensboro"; "Honolulu"; "Indianapolis";
+ "Jersey City"; "Kansas City"; "Los Angeles";
+ "Milwaukee"; "New York"; "Omaha"; "Philadelphia";
+ "Raleigh"; "San Francisco"; "Tulsa"; "Washington" |]
+
+let rnd = Random()
+
+let index = rnd.Next(0,cities.Length)
+
+printfn "Today's city of the day: %s" cities.[index]
+
+// The example displays output like the following:
+// Today's city of the day: Honolulu
+//
diff --git a/snippets/fsharp/System/Random/Overview/booleans1.fs b/snippets/fsharp/System/Random/Overview/booleans1.fs
new file mode 100644
index 00000000000..44a9bba0f75
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/booleans1.fs
@@ -0,0 +1,28 @@
+module Booleans1
+
+//
+open System
+
+type BooleanGenerator() =
+ let rnd = Random()
+
+ member _.NextBoolean() =
+ rnd.Next(0, 2) = 1
+
+let boolGen = BooleanGenerator()
+let mutable totalTrue, totalFalse = 0, 0
+
+for _ = 1 to 1000000 do
+ let value = boolGen.NextBoolean()
+ if value then
+ totalTrue <- totalTrue + 1
+ else
+ totalFalse <- totalFalse + 1
+
+printfn $"Number of true values: {totalTrue,7:N0} ({(double totalTrue) / double (totalTrue + totalFalse):P3})"
+printfn $"Number of false values: {totalFalse,7:N0} ({(double totalFalse) / double (totalTrue + totalFalse):P3})"
+
+// The example displays output like the following:
+// Number of true values: 500,004 (50.000 %)
+// Number of false values: 499,996 (50.000 %)
+//
diff --git a/snippets/fsharp/System/Random/Overview/booleans2.fs b/snippets/fsharp/System/Random/Overview/booleans2.fs
new file mode 100644
index 00000000000..e4e146c3a8d
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/booleans2.fs
@@ -0,0 +1,26 @@
+module Booleans2
+
+open System
+
+//
+let rnd = Random()
+
+let nextBool () =
+ rnd.Next(0, 2) = 1
+
+let mutable totalTrue, totalFalse = 0, 0
+
+for _ = 1 to 1000000 do
+ let value = nextBool ()
+ if value then
+ totalTrue <- totalTrue + 1
+ else
+ totalFalse <- totalFalse + 1
+
+printfn $"Number of true values: {totalTrue,7:N0} ({(double totalTrue) / double (totalTrue + totalFalse):P3})"
+printfn $"Number of false values: {totalFalse,7:N0} ({(double totalFalse) / double (totalTrue + totalFalse):P3})"
+
+// The example displays output like the following:
+// Number of true values: 499,777 (49.978 %)
+// Number of false values: 500,223 (50.022 %)
+//
diff --git a/snippets/fsharp/System/Random/Overview/bytes1.fs b/snippets/fsharp/System/Random/Overview/bytes1.fs
new file mode 100644
index 00000000000..9b605bc10ea
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/bytes1.fs
@@ -0,0 +1,49 @@
+module Bytes1
+
+//
+open System
+
+type Random2() =
+ inherit Random()
+
+ member this.NextBytes(bytes: byte[], minValue: byte, maxValue: byte) =
+ for i=bytes.GetLowerBound(0) to bytes.GetUpperBound(0) do
+ bytes.[i] <- this.Next(int minValue, int maxValue) |> byte
+
+let rnd = Random2()
+let bytes = Array.zeroCreate 10000
+let total = Array.zeroCreate 101
+rnd.NextBytes(bytes, 0uy, 101uy)
+
+// Calculate how many of each value we have.
+for v in bytes do
+ total.[int v] <- total.[int v] + 1
+
+// Display the results.
+for i = 0 to total.Length - 1 do
+ printf "%3i: %-3i " i total.[i]
+ if (i + 1) % 5 = 0 then printfn ""
+
+// The example displays output like the following:
+// 0: 115 1: 119 2: 92 3: 98 4: 92
+// 5: 102 6: 103 7: 84 8: 93 9: 116
+// 10: 91 11: 98 12: 106 13: 91 14: 92
+// 15: 101 16: 100 17: 96 18: 97 19: 100
+// 20: 101 21: 106 22: 112 23: 82 24: 85
+// 25: 102 26: 107 27: 98 28: 106 29: 102
+// 30: 109 31: 108 32: 94 33: 101 34: 107
+// 35: 101 36: 86 37: 100 38: 101 39: 102
+// 40: 113 41: 95 42: 96 43: 89 44: 99
+// 45: 81 46: 89 47: 105 48: 100 49: 85
+// 50: 103 51: 103 52: 93 53: 89 54: 91
+// 55: 97 56: 105 57: 97 58: 110 59: 86
+// 60: 116 61: 94 62: 117 63: 98 64: 110
+// 65: 93 66: 102 67: 100 68: 105 69: 83
+// 70: 81 71: 97 72: 85 73: 70 74: 98
+// 75: 100 76: 110 77: 114 78: 83 79: 90
+// 80: 96 81: 112 82: 102 83: 102 84: 99
+// 85: 81 86: 100 87: 93 88: 99 89: 118
+// 90: 95 91: 124 92: 108 93: 96 94: 104
+// 95: 106 96: 99 97: 99 98: 92 99: 99
+// 100: 108
+//
diff --git a/snippets/fsharp/System/Random/Overview/doublerange1.fs b/snippets/fsharp/System/Random/Overview/doublerange1.fs
new file mode 100644
index 00000000000..d4c9fe115ef
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/doublerange1.fs
@@ -0,0 +1,37 @@
+module DoubleRange1
+
+open System
+
+//
+[]
+let ONE_TENTH = 922337203685477581L
+
+let rnd = Random()
+
+// Generate 20 million random integers.
+let count =
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (float Int64.MaxValue) )
+ |> Array.countBy (fun x -> x / (float ONE_TENTH) |> int ) // Categorize into 10 groups and count them.
+ |> Array.map snd
+
+// Display breakdown by range.
+printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
+for i = 0 to 9 do
+ let r1 = int64 i * ONE_TENTH
+ let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {float count.[i] / 20000000.0,7:P2}"
+
+// The example displays output like the following:
+// Range Count Pct.
+//
+// 0- 922,337,203,685,477,580 1,996,148 9.98 %
+// 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+// 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+// 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+// 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+// 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+// 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+// 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+// 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+// 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+//
diff --git a/snippets/fsharp/System/Random/Overview/doublerange2.fs b/snippets/fsharp/System/Random/Overview/doublerange2.fs
new file mode 100644
index 00000000000..04c48ba953f
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/doublerange2.fs
@@ -0,0 +1,22 @@
+module DoubleRange2
+
+open System
+
+//
+let rnd = Random()
+
+for _ = 1 to 10 do
+ printfn "%O" (rnd.NextDouble() - 1.0)
+
+// The example displays output like the following:
+// -0.930412760437658
+// -0.164699016215605
+// -0.9851692803135
+// -0.43468508843085
+// -0.177202483255976
+// -0.776813320245972
+// -0.0713201854710096
+// -0.0912875561468711
+// -0.540621722368813
+// -0.232211863730201
+//
diff --git a/snippets/fsharp/System/Random/Overview/doublerange3.fs b/snippets/fsharp/System/Random/Overview/doublerange3.fs
new file mode 100644
index 00000000000..829f6d79b1e
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/doublerange3.fs
@@ -0,0 +1,31 @@
+module DoubleRange3
+
+open System
+
+//
+let rnd = Random()
+
+let lowerBound = 10.0
+let upperBound = 11.0
+
+let range =
+ Array.init 1000000 (fun _ -> rnd.NextDouble() * (upperBound - lowerBound) + lowerBound)
+ |> Array.countBy (fun x -> Math.Truncate((x - lowerBound) * 10.0) |> int)
+ |> Array.map snd
+
+for i = 0 to 9 do
+ let lowerRange = 10.0 + float i * 0.1
+ printfn $"{lowerRange:N1} to {lowerRange + 0.1:N1}: {range.[i],8:N0} ({float range.[i] / 1000000.0,6:P2})"
+
+// The example displays output like the following:
+// 10.0 to 10.1: 99,929 ( 9.99 %)
+// 10.1 to 10.2: 100,189 (10.02 %)
+// 10.2 to 10.3: 99,384 ( 9.94 %)
+// 10.3 to 10.4: 100,240 (10.02 %)
+// 10.4 to 10.5: 99,397 ( 9.94 %)
+// 10.5 to 10.6: 100,580 (10.06 %)
+// 10.6 to 10.7: 100,293 (10.03 %)
+// 10.7 to 10.8: 100,135 (10.01 %)
+// 10.8 to 10.9: 99,905 ( 9.99 %)
+// 10.9 to 11.0: 99,948 ( 9.99 %)
+//
diff --git a/snippets/fsharp/System/Random/Overview/long1.fs b/snippets/fsharp/System/Random/Overview/long1.fs
new file mode 100644
index 00000000000..90b86226dce
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/long1.fs
@@ -0,0 +1,37 @@
+module Long1
+
+open System
+
+//
+[]
+let ONE_TENTH = 922337203685477581L
+
+let rnd = Random()
+
+let count =
+ // Generate 20 million random long integers.
+ Array.init 20000000 (fun _ -> rnd.NextDouble() * (float Int64.MaxValue) |> int64 )
+ |> Array.countBy (fun x -> x / ONE_TENTH) // Categorize and count random numbers.
+ |> Array.map snd
+
+// Display breakdown by range.
+printfn "%28s %32s %7s\n" "Range" "Count" "Pct."
+for i = 0 to 9 do
+ let r1 = int64 i * ONE_TENTH
+ let r2 = if i < 9 then r1 + ONE_TENTH - 1L else Int64.MaxValue
+ printfn $"{r1,25:N0}-{r2,25:N0} {count.[i],8:N0} {float count.[i] / 20000000.0,7:P2}"
+
+// The example displays output like the following:
+// Range Count Pct.
+//
+// 0- 922,337,203,685,477,580 1,996,148 9.98 %
+// 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+// 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+// 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+// 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+// 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+// 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+// 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+// 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+// 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+//
diff --git a/snippets/fsharp/System/Random/Overview/nextbytes1.fs b/snippets/fsharp/System/Random/Overview/nextbytes1.fs
new file mode 100644
index 00000000000..53520b2b3ce
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/nextbytes1.fs
@@ -0,0 +1,17 @@
+module NextBytes1
+
+open System
+
+//
+let rnd = Random()
+let bytes = Array.zeroCreate 20
+rnd.NextBytes bytes
+
+for i = 1 to bytes.Length do
+ printf "%3i " bytes.[i - 1]
+ if (i % 10 = 0) then printfn ""
+
+// The example displays output like the following:
+// 141 48 189 66 134 212 211 71 161 56
+// 181 166 220 133 9 252 222 57 62 62
+//
diff --git a/snippets/fsharp/System/Random/Overview/nextdoubleex1.fs b/snippets/fsharp/System/Random/Overview/nextdoubleex1.fs
new file mode 100644
index 00000000000..5e1fe90a370
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/nextdoubleex1.fs
@@ -0,0 +1,16 @@
+module NextDoubleEx1
+
+open System
+
+//
+let rnd = Random()
+for i = 0 to 9 do
+ printf $"{rnd.NextDouble(),-19:R} "
+ if (i + 1) % 3 = 0 then printfn ""
+
+// The example displays output like the following:
+// 0.7911680553998649 0.0903414949264105 0.79776258291572455
+// 0.615568345233597 0.652644504165577 0.84023809378977776
+// 0.099662564741290441 0.91341467383942321 0.96018602045261581
+// 0.74772306473354022
+//
diff --git a/snippets/fsharp/System/Random/Overview/nextex1.fs b/snippets/fsharp/System/Random/Overview/nextex1.fs
new file mode 100644
index 00000000000..9b650155718
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/nextex1.fs
@@ -0,0 +1,12 @@
+module NextEx1
+
+open System
+
+//
+let rnd = Random()
+for i = 0 to 9 do
+ printf "%3i " (rnd.Next(-10, 11))
+
+// The example displays output like the following:
+// 2 9 -3 2 4 -7 -3 -8 -8 5
+//
diff --git a/snippets/fsharp/System/Random/Overview/range1.fs b/snippets/fsharp/System/Random/Overview/range1.fs
new file mode 100644
index 00000000000..13943083adb
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/range1.fs
@@ -0,0 +1,14 @@
+module Range1
+
+open System
+
+//
+let rnd = Random()
+for i = 1 to 15 do
+ printf "%3i " (rnd.Next(-10, 11))
+ if i % 5 = 0 then printfn ""
+// The example displays output like the following:
+// -2 -5 -1 -2 10
+// -3 6 -4 -8 3
+// -7 10 5 -2 4
+//
diff --git a/snippets/fsharp/System/Random/Overview/range2.fs b/snippets/fsharp/System/Random/Overview/range2.fs
new file mode 100644
index 00000000000..3f8a084972c
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/range2.fs
@@ -0,0 +1,17 @@
+module Range2
+
+open System
+
+//
+let rnd = Random()
+for i = 1 to 50 do
+ printf "%3i " (rnd.Next(1000, 10000))
+ if i % 10 = 0 then printfn ""
+
+// The example displays output like the following:
+// 9570 8979 5770 1606 3818 4735 8495 7196 7070 2313
+// 5279 6577 5104 5734 4227 3373 7376 6007 8193 5540
+// 7558 3934 3819 7392 1113 7191 6947 4963 9179 7907
+// 3391 6667 7269 1838 7317 1981 5154 7377 3297 5320
+// 9869 8694 2684 4949 2999 3019 2357 5211 9604 2593
+//
diff --git a/snippets/fsharp/System/Random/Overview/same1.fs b/snippets/fsharp/System/Random/Overview/same1.fs
new file mode 100644
index 00000000000..0d8e9045a16
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/same1.fs
@@ -0,0 +1,76 @@
+module Same1
+
+//
+open System
+open System.IO
+
+let showRandomNumbers seed =
+ let rnd = Random seed
+ for _ = 0 to 20 do
+ printfn $"{rnd.NextDouble()}"
+
+let persistSeed (seed: int) =
+ use bin = new BinaryWriter(new FileStream(@".\seed.dat", FileMode.Create))
+ bin.Write seed
+
+let displayNewRandomNumbers () =
+ use bin = new BinaryReader(new FileStream(@".\seed.dat", FileMode.Open))
+ let seed = bin.ReadInt32()
+
+ let rnd = Random seed
+ for _ = 0 to 20 do
+ printfn $"{rnd.NextDouble()}"
+
+let seed = 100100
+showRandomNumbers seed
+printfn ""
+
+persistSeed seed
+
+displayNewRandomNumbers ()
+
+// The example displays output like the following:
+// 0.500193602172748
+// 0.0209461245783354
+// 0.465869495396442
+// 0.195512794514891
+// 0.928583675496552
+// 0.729333720509584
+// 0.381455668891527
+// 0.0508996467343064
+// 0.019261200921266
+// 0.258578445417145
+// 0.0177532266908107
+// 0.983277184415272
+// 0.483650274334313
+// 0.0219647376900375
+// 0.165910115077118
+// 0.572085966622497
+// 0.805291457942357
+// 0.927985211335116
+// 0.4228545699375
+// 0.523320379910674
+// 0.157783938645285
+//
+// 0.500193602172748
+// 0.0209461245783354
+// 0.465869495396442
+// 0.195512794514891
+// 0.928583675496552
+// 0.729333720509584
+// 0.381455668891527
+// 0.0508996467343064
+// 0.019261200921266
+// 0.258578445417145
+// 0.0177532266908107
+// 0.983277184415272
+// 0.483650274334313
+// 0.0219647376900375
+// 0.165910115077118
+// 0.572085966622497
+// 0.805291457942357
+// 0.927985211335116
+// 0.4228545699375
+// 0.523320379910674
+// 0.157783938645285
+//
diff --git a/snippets/fsharp/System/Random/Overview/threadsafeex1.fs b/snippets/fsharp/System/Random/Overview/threadsafeex1.fs
new file mode 100644
index 00000000000..df8058ffdd9
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/threadsafeex1.fs
@@ -0,0 +1,146 @@
+module ThreadSafeEx1
+
+//
+open System
+open System.Threading
+
+type Example() =
+ []
+ static val mutable private previous : float
+
+ []
+ static val mutable private perThreadCtr : int
+
+ []
+ static val mutable private perThreadTotal : float
+
+ static let source = new CancellationTokenSource()
+ static let countdown = new CountdownEvent(1)
+ static let randLock = obj ()
+ static let numericLock = obj ()
+ static let rand = Random()
+
+ let mutable totalValue = 0.0
+ let mutable totalCount = 0
+
+ member _.GetRandomNumbers(token: CancellationToken) =
+ let mutable result = 0.0
+ countdown.AddCount 1
+ try
+ try
+ for _ = 0 to 1999999 do
+ // Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ lock randLock (fun () ->
+ result <- rand.NextDouble() )
+
+ // Check for corruption of Random instance.
+ if result = Example.previous && result = 0.0 then
+ source.Cancel()
+ else
+ Example.previous <- result
+
+ Example.perThreadCtr <- Example.perThreadCtr + 1
+ Example.perThreadTotal <- Example.perThreadTotal + result
+
+ // Update overall totals.
+ lock numericLock (fun () ->
+ // Show result.
+ printfn "Thread %s finished execution." Thread.CurrentThread.Name
+ printfn $"Random numbers generated: {Example.perThreadCtr:N0}"
+ printfn $"Sum of random numbers: {Example.perThreadTotal:N2}"
+ printfn $"Random number mean: {(Example.perThreadTotal / float Example.perThreadCtr):N4}\n"
+
+ // Update overall totals.
+ totalCount <- totalCount + Example.perThreadCtr
+ totalValue <- totalValue + Example.perThreadTotal)
+
+ with :? OperationCanceledException as e ->
+ printfn "Corruption in Thread %s %s" (e.GetType().Name) Thread.CurrentThread.Name
+ finally
+ countdown.Signal() |> ignore
+
+ member this.Execute() =
+ let token = source.Token
+ for i = 1 to 10 do
+ let newThread = Thread(fun () -> this.GetRandomNumbers token)
+ newThread.Name <- string i
+ newThread.Start()
+ this.GetRandomNumbers token
+
+ countdown.Signal() |> ignore
+
+ countdown.Wait()
+
+ source.Dispose()
+
+ printfn $"\nTotal random numbers generated: {totalCount:N0}"
+ printfn $"Total sum of all random numbers: {totalValue:N2}"
+ printfn $"Random number mean: {(totalValue / float totalCount):N4}"
+
+let ex = Example()
+Thread.CurrentThread.Name <- "Main"
+ex.Execute()
+
+// The example displays output like the following:
+// Thread 6 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,491.05
+// Random number mean: 0.5002
+//
+// Thread 10 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,329.64
+// Random number mean: 0.4997
+//
+// Thread 4 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,166.89
+// Random number mean: 0.5001
+//
+// Thread 8 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,628.37
+// Random number mean: 0.4998
+//
+// Thread Main finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,920.89
+// Random number mean: 0.5000
+//
+// Thread 3 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,370.45
+// Random number mean: 0.4997
+//
+// Thread 7 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,330.92
+// Random number mean: 0.4997
+//
+// Thread 9 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,172.79
+// Random number mean: 0.5001
+//
+// Thread 5 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,079.43
+// Random number mean: 0.5000
+//
+// Thread 1 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,817.91
+// Random number mean: 0.4999
+//
+// Thread 2 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,930.63
+// Random number mean: 0.5000
+//
+//
+// Total random numbers generated: 22,000,000
+// Total sum of all random numbers: 10,998,238.98
+// Random number mean: 0.4999
+//
diff --git a/snippets/fsharp/System/Random/Overview/threadsafeex2.fs b/snippets/fsharp/System/Random/Overview/threadsafeex2.fs
new file mode 100644
index 00000000000..aa9cd5bdee2
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/threadsafeex2.fs
@@ -0,0 +1,143 @@
+module ThreadSafeEx2
+
+//
+open System
+open System.Threading
+open System.Threading.Tasks
+
+type Example() =
+ static let source = new CancellationTokenSource()
+ static let rand = Random()
+
+ static let randLock = obj ()
+ static let numericLock = obj ()
+
+ let mutable totalValue = 0.0
+ let mutable totalCount = 0
+
+ member _.Execute() =
+ use source = source // Dispose of the CancellationTokenSource when we're done with it.
+ let token = source.Token
+
+ let tasks =
+ [| for i = 0 to 10 do
+ Task.Run(
+ (fun () ->
+ let mutable previous = 0.0
+ let mutable taskCtr = 0
+ let mutable taskTotal = 0.0
+ let mutable result = 0.0
+
+ for _ = 1 to 2000000 do
+ // Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ lock randLock (fun () -> result <- rand.NextDouble())
+
+ // Check for corruption of Random instance.
+ if result = previous && result = 0.0 then
+ source.Cancel()
+ else
+ previous <- result
+
+ taskCtr <- taskCtr + 1
+ taskTotal <- taskTotal + result
+
+ lock numericLock (fun () ->
+ // Show result.
+ printfn "Task %i finished execution." i
+ printfn $"Random numbers generated: {taskCtr:N0}"
+ printfn $"Sum of random numbers: {taskTotal:N2}"
+ printfn $"Random number mean: {(taskTotal / float taskCtr):N4}\n"
+
+ // Update overall totals.
+ totalCount <- totalCount + taskCtr
+ totalValue <- totalValue + taskTotal)),
+ token
+ ) |]
+
+ try
+ // Run tasks with F# Async.
+ Task.WhenAll tasks
+ |> Async.AwaitTask
+ |> Async.RunSynchronously
+
+ printfn $"\nTotal random numbers generated: {totalCount:N0}"
+ printfn $"Total sum of all random numbers: {totalValue:N2}"
+ printfn $"Random number mean: {(totalValue / float totalCount):N4}"
+ with
+ | :? AggregateException as e ->
+ for inner in e.InnerExceptions do
+ match inner with
+ | :? TaskCanceledException as canc ->
+ if canc <> null then
+ printfn $"Task #{canc.Task.Id} cancelled"
+ else
+ printfn $"Exception: {inner.GetType().Name}"
+ | _ -> ()
+
+let ex = Example()
+Thread.CurrentThread.Name <- "Main"
+ex.Execute()
+
+// The example displays output like the following:
+// Task 1 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,502.47
+// Random number mean: 0.5003
+//
+// Task 0 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,445.63
+// Random number mean: 0.5002
+//
+// Task 2 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,556.04
+// Random number mean: 0.5003
+//
+// Task 3 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,178.87
+// Random number mean: 0.5001
+//
+// Task 4 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,819.17
+// Random number mean: 0.4999
+//
+// Task 5 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,190.58
+// Random number mean: 0.5001
+//
+// Task 6 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,720.21
+// Random number mean: 0.4999
+//
+// Task 7 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,000.96
+// Random number mean: 0.4995
+//
+// Task 8 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,499.33
+// Random number mean: 0.4997
+//
+// Task 9 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 1,000,193.25
+// Random number mean: 0.5001
+//
+// Task 10 finished execution.
+// Random numbers generated: 2,000,000
+// Sum of random numbers: 999,960.82
+// Random number mean: 0.5000
+//
+//
+// Total random numbers generated: 22,000,000
+// Total sum of all random numbers: 11,000,067.33
+// Random number mean: 0.5000
+//
diff --git a/snippets/fsharp/System/Random/Overview/unique.fs b/snippets/fsharp/System/Random/Overview/unique.fs
new file mode 100644
index 00000000000..2a0d2c1466e
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/unique.fs
@@ -0,0 +1,46 @@
+module Unique
+
+//
+open System
+open System.Threading
+
+printfn "Instantiating two random number generators..."
+let rnd1 = Random()
+Thread.Sleep 2000
+let rnd2 = Random()
+
+printfn "\nThe first random number generator:"
+for _ = 1 to 10 do
+ printfn $" {rnd1.Next()}"
+
+printfn "\nThe second random number generator:"
+for _ = 1 to 10 do
+ printfn $" {rnd2.Next()}"
+
+// The example displays output like the following:
+// Instantiating two random number generators...
+//
+// The first random number generator:
+// 643164361
+// 1606571630
+// 1725607587
+// 2138048432
+// 496874898
+// 1969147632
+// 2034533749
+// 1840964542
+// 412380298
+// 47518930
+//
+// The second random number generator:
+// 1251659083
+// 1514185439
+// 1465798544
+// 517841554
+// 1821920222
+// 195154223
+// 1538948391
+// 1548375095
+// 546062716
+// 897797880
+//
diff --git a/snippets/fsharp/System/Random/Overview/uniquearray1.fs b/snippets/fsharp/System/Random/Overview/uniquearray1.fs
new file mode 100644
index 00000000000..5cae6445e6d
--- /dev/null
+++ b/snippets/fsharp/System/Random/Overview/uniquearray1.fs
@@ -0,0 +1,78 @@
+module UniqueArray1
+
+//
+open System
+
+type Suit =
+ | Clubs
+ | Diamonds
+ | Hearts
+ | Spades
+
+type Face =
+ | Ace | Two | Three
+ | Four | Five | Six
+ | Seven | Eight | Nine
+ | Ten | Jack | Queen | King
+
+type Card = { Face: Face; Suit: Suit }
+
+let suits = [ Clubs; Diamonds; Hearts; Spades ]
+let faces = [ Ace; Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten; Jack; Queen; King ]
+
+type Dealer() =
+ let rnd = Random()
+ let mutable pos = 0
+ // Parallel array for sorting cards.
+ let order = Array.init (suits.Length * faces.Length) (fun _ -> rnd.NextDouble() )
+ // A deck of cards, without Jokers.
+ let deck = [|
+ for s in suits do
+ for f in faces do
+ { Face = f; Suit = s } |]
+ // Shuffle the deck.
+ do Array.Sort(order, deck)
+
+ // Deal a number of cards from the deck, return None if failed
+ member _.Deal(numberToDeal) : Card [] option =
+ if numberToDeal = 0 || pos = deck.Length then
+ printfn "There are no cards left in the deck"
+ None
+ else
+ let cards = deck.[pos .. numberToDeal + pos - 1]
+ if numberToDeal > deck.Length - pos then
+ printfn "Can only deal the %i cards remaining on the deck." (deck.Length - pos)
+ pos <- min (pos + numberToDeal) deck.Length
+ Some cards
+
+let showCards cards =
+ for card in cards do
+ printfn $"{card.Face} of {card.Suit}"
+
+let dealer = Dealer()
+
+dealer.Deal 20
+|> Option.iter showCards
+
+// The example displays output like the following:
+// Six of Diamonds
+// King of Clubs
+// Eight of Clubs
+// Seven of Clubs
+// Queen of Clubs
+// King of Hearts
+// Three of Spades
+// Ace of Clubs
+// Four of Hearts
+// Three of Diamonds
+// Nine of Diamonds
+// Two of Hearts
+// Ace of Hearts
+// Three of Hearts
+// Four of Spades
+// Eight of Hearts
+// Queen of Diamonds
+// Two of Clubs
+// Four of Diamonds
+// Jack of Hearts
+//
diff --git a/snippets/fsharp/System/Single/CompareTo/compareto2.fs b/snippets/fsharp/System/Single/CompareTo/compareto2.fs
new file mode 100644
index 00000000000..e097f734ffc
--- /dev/null
+++ b/snippets/fsharp/System/Single/CompareTo/compareto2.fs
@@ -0,0 +1,13 @@
+module compareto2
+
+//
+let value1 = 16.5457f
+let operand = 3.8899982f
+let value2 = value1 * operand / operand
+printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
+printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
+// The example displays the following output:
+// Comparing 16.5457 and 16.5457: -1
+//
+// Comparing 16.5457 and 16.545702: -1
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/Single/CompareTo/compareto3.fs b/snippets/fsharp/System/Single/CompareTo/compareto3.fs
new file mode 100644
index 00000000000..d541c895f14
--- /dev/null
+++ b/snippets/fsharp/System/Single/CompareTo/compareto3.fs
@@ -0,0 +1,13 @@
+module compareto3
+
+//
+let value1 = 16.5457f
+let operand = 3.8899982f
+let value2 = box (value1 * operand / operand)
+printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
+printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
+// The example displays the following output:
+// Comparing 16.5457 and 16.5457: -1
+//
+// Comparing 16.5457 and 16.545702: -1
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/Single/Epsilon/epsilon.fs b/snippets/fsharp/System/Single/Epsilon/epsilon.fs
new file mode 100644
index 00000000000..ef10b674b18
--- /dev/null
+++ b/snippets/fsharp/System/Single/Epsilon/epsilon.fs
@@ -0,0 +1,17 @@
+module epsilon
+
+//
+open System
+
+let values = [ 0f; Single.Epsilon; Single.Epsilon * 0.5f ]
+
+for i = 0 to values.Length - 2 do
+ for i2 = i + 1 to values.Length - 1 do
+ printfn $"{values[i]:r} = {values[i2]:r}: {values[i].Equals(values[i2])}"
+ printfn ""
+// The example displays the following output:
+// 0 = 1.401298E-45: False
+// 0 = 0: True
+//
+// 1.401298E-45 = 0: False
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/Single/Epsilon/epsilon1.fs b/snippets/fsharp/System/Single/Epsilon/epsilon1.fs
new file mode 100644
index 00000000000..ae40c7d0c64
--- /dev/null
+++ b/snippets/fsharp/System/Single/Epsilon/epsilon1.fs
@@ -0,0 +1,44 @@
+module epsilon1
+
+//
+open System
+
+let getComponentParts (value: float32) =
+ let result = $"{value:R}: "
+ let indent = result.Length
+
+ // Convert the single to a 4-byte array.
+ let bytes = BitConverter.GetBytes value
+ let formattedSingle = BitConverter.ToInt32(bytes, 0)
+
+ // Get the sign bit (byte 3, bit 7).
+ let result = result + $"""Sign: {if formattedSingle >>> 31 <> 0 then "1 (-)" else "0 (+)"}\n"""
+
+ // Get the exponent (byte 2 bit 7 to byte 3, bits 6)
+ let exponent = (formattedSingle >>> 23) &&& 0x000000FF
+ let adjustment = if exponent <> 0 then 127 else 126
+ let result = result + $"{String(' ', indent)}Exponent: 0x{exponent - adjustment:X4} ({exponent - adjustment})\n"
+
+ // Get the significand (bits 0-22)
+ let significand =
+ if exponent <> 0 then
+ (formattedSingle &&& 0x007FFFFF) ||| 0x800000
+ else
+ formattedSingle &&& 0x007FFFFF
+
+ result + $"{String(' ', indent)}Mantissa: 0x{significand:X13}\n"
+
+
+let values = [ 0f; Single.Epsilon ]
+for value in values do
+ printfn $"{getComponentParts value}\n"
+// // The example displays the following output:
+// 0: Sign: 0 (+)
+// Exponent: 0xFFFFFF82 (-126)
+// Mantissa: 0x0000000000000
+//
+//
+// 1.401298E-45: Sign: 0 (+)
+// Exponent: 0xFFFFFF82 (-126)
+// Mantissa: 0x0000000000001
+//
diff --git a/snippets/fsharp/System/Single/Equals/equalsabs1.fs b/snippets/fsharp/System/Single/Equals/equalsabs1.fs
new file mode 100644
index 00000000000..be7777d1a44
--- /dev/null
+++ b/snippets/fsharp/System/Single/Equals/equalsabs1.fs
@@ -0,0 +1,27 @@
+module equalsabs1
+
+//
+open System
+
+let hasMinimalDifference (value1: float32) (value2: float32) units =
+ let bytes = BitConverter.GetBytes value1
+ let iValue1 = BitConverter.ToInt32(bytes, 0)
+ let bytes = BitConverter.GetBytes(value2)
+ let iValue2 = BitConverter.ToInt32(bytes, 0)
+
+ // If the signs are different, return false except for +0 and -0.
+ if (iValue1 >>> 31) <> (iValue2 >>> 31) then
+ value1 = value2
+ else
+ let diff = abs (iValue1 - iValue2)
+ diff <= units
+
+let value1 = 0.1f * 10f
+let value2 =
+ List.replicate 10 0.1f
+ |> List.sum
+
+printfn $"{value1:R} = {value2:R}: {hasMinimalDifference value1 value2 1}"
+// The example displays the following output:
+// 1 = 1.0000001: True
+//
diff --git a/snippets/fsharp/System/String/.ctor/char2_ctor.fs b/snippets/fsharp/System/String/.ctor/char2_ctor.fs
new file mode 100644
index 00000000000..159ab3913a0
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/char2_ctor.fs
@@ -0,0 +1,29 @@
+module char2_ctor
+
+//
+#nowarn "9"
+open System
+open FSharp.NativeInterop
+
+let characters =
+ [| 'H'; 'e'; 'l'; 'l'; 'o'; ' '
+ 'w'; 'o'; 'r'; 'l'; 'd'; '!'; '\u0000' |]
+
+[]
+let main _ =
+ use charPtr = fixed characters
+ let mutable length = 0
+ let mutable iterator = charPtr
+ let mutable broken = false
+ while not broken && NativePtr.read iterator <> '\u0000' do
+ if NativePtr.read iterator = '!' || NativePtr.read iterator = '.' then
+ broken <- true
+ else
+ iterator <- NativePtr.add iterator 1
+ length <- length + 1
+ String(charPtr, 0, length)
+ |> printfn "%s"
+ 0
+// The example displays the following output:
+// Hello World
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/.ctor/chptrctor_null.fs b/snippets/fsharp/System/String/.ctor/chptrctor_null.fs
new file mode 100644
index 00000000000..b973bca4ee7
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/chptrctor_null.fs
@@ -0,0 +1,26 @@
+module chptrctor_null
+
+//
+#nowarn "9"
+open System
+
+let chars = [| 'a'; 'b'; 'c'; 'd'; '\000'; 'A'; 'B'; 'C'; 'D'; '\000' |]
+let s =
+ use chPtr = fixed chars
+ String(chPtr, 0, chars.Length)
+
+for ch in s do
+ printf $"{uint16 ch:X4} "
+printfn ""
+
+let s2 =
+ use chPtr = fixed chars
+ String chPtr
+
+for ch in s2 do
+ printf $"{uint16 ch:X4} "
+printfn ""
+// The example displays the following output:
+// 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000
+// 0061 0062 0063 0064
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/.ctor/ctor1.fs b/snippets/fsharp/System/String/.ctor/ctor1.fs
new file mode 100644
index 00000000000..b44b9bb30d6
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/ctor1.fs
@@ -0,0 +1,11 @@
+module ctor1
+
+//
+let value1 = "This is a string."
+let value2 = value1
+printfn "%s" value1
+printfn "%s" value2
+// The example displays the following output:
+// This is a string.
+// This is a string.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/.ctor/ctor2.fs b/snippets/fsharp/System/String/.ctor/ctor2.fs
new file mode 100644
index 00000000000..1042312649a
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/ctor2.fs
@@ -0,0 +1,18 @@
+module ctor2
+
+//
+#nowarn "9"
+open System
+
+let characters =
+ [| 'H'; 'e'; 'l'; 'l'; 'o'; ' '
+ 'w'; 'o'; 'r'; 'l'; 'd'; '!'; '\u0000' |]
+
+let value =
+ use charPtr = fixed characters
+ String charPtr
+
+printfn $"{value}"
+// The example displays the following output:
+// Hello world!
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/.ctor/ptrctor_null.fs b/snippets/fsharp/System/String/.ctor/ptrctor_null.fs
new file mode 100644
index 00000000000..e3753c593e0
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/ptrctor_null.fs
@@ -0,0 +1,28 @@
+module ptrctor_null
+
+//
+#nowarn "9"
+open System
+
+let bytes =
+ [| 0x61y; 0x62y; 0x063y; 0x064y; 0x00y; 0x41y; 0x42y; 0x43y; 0x44y; 0x00y |]
+
+let s =
+ use bytePtr = fixed bytes
+ String(bytePtr, 0, bytes.Length)
+
+for ch in s do
+ printf $"{uint16 ch:X4} "
+printfn ""
+
+let s2 =
+ use bytePtr = fixed bytes
+ String bytePtr
+
+for ch in s do
+ printf $"{uint16 ch:X4} "
+printfn ""
+// The example displays the following output:
+// 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000
+// 0061 0062 0063 0064
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/.ctor/source.fs b/snippets/fsharp/System/String/.ctor/source.fs
new file mode 100644
index 00000000000..017dbeeaaba
--- /dev/null
+++ b/snippets/fsharp/System/String/.ctor/source.fs
@@ -0,0 +1,70 @@
+module source
+
+open System
+open System.Text
+
+do
+//
+ // Unicode Mathematical operators
+ let charArr1 = [| '\u2200'; '\u2202'; '\u200F'; '\u2205' |]
+ let szMathSymbols = String charArr1
+
+ // Unicode Letterlike Symbols
+ let charArr2 = [| '\u2111'; '\u2118'; '\u2122'; '\u2126' |]
+ let szLetterLike = String charArr2
+
+ // Compare Strings - the result is false
+ printfn $"The Strings are equal? %b{String.Compare(szMathSymbols, szLetterLike) = 0}"
+//
+//
+ // Null terminated ASCII characters in an sbyte array
+ let szAsciiUpper =
+ let sbArr1 = [| 0x41y; 0x42y; 0x43y; 0x00y |]
+ // Instruct the Garbage Collector not to move the memory
+ use pAsciiUpper = fixed sbArr1
+ String pAsciiUpper
+
+ let szAsciiLower =
+ let sbArr2 = [| 0x61y; 0x62y; 0x63y; 0x00y |]
+ // Instruct the Garbage Collector not to move the memory
+ use pAsciiLower = fixed sbArr2
+ String(pAsciiLower, 0, sbArr2.Length)
+
+ // Prints "ABC abc"
+ printfn $"{szAsciiUpper} {szAsciiLower}"
+
+ // Compare Strings - the result is true
+ printfn $"The Strings are equal when capitalized ? %b{String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper()) = 0}"
+
+ // This is the effective equivalent of another Compare method, which ignores case
+ printfn $"The Strings are equal when capitalized ? %b{String.Compare(szAsciiUpper, szAsciiLower, true) = 0}"
+//
+//
+ // Create a Unicode String with 5 Greek Alpha characters
+ let szGreekAlpha = String('\u0391',5)
+ // Create a Unicode String with a Greek Omega character
+ let szGreekOmega = String([| '\u03A9'; '\u03A9'; '\u03A9' |],2,1)
+
+ let szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone())
+
+ // Examine the result
+ printfn $"{szGreekLetters}"
+
+ // The first index of Alpha
+ let ialpha = szGreekLetters.IndexOf '\u0391'
+ // The last index of Omega
+ let iomega = szGreekLetters.LastIndexOf '\u03A9'
+
+ printfn $"The Greek letter Alpha first appears at index {ialpha} and Omega last appears at index {iomega} in this String."
+//
+
+//
+ let utfeightstring =
+ let asciiChars = [| 0x51y; 0x52y; 0x53y; 0x54y; 0x54y; 0x56y |]
+ let encoding = UTF8Encoding(true, true)
+
+ // Instruct the Garbage Collector not to move the memory
+ use pAsciiChars = fixed asciiChars
+ String(pAsciiChars,0,asciiChars.Length,encoding)
+ printfn $"The UTF8 String is {utfeightstring}" // prints "QRSTTV"
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/Format/Example1.fs b/snippets/fsharp/System/String/Format/Example1.fs
new file mode 100644
index 00000000000..6f76849429c
--- /dev/null
+++ b/snippets/fsharp/System/String/Format/Example1.fs
@@ -0,0 +1,19 @@
+module Example1
+
+//
+open System
+
+let values= [| Int16.MinValue; -27s; 0s; 1042s; Int16.MaxValue |]
+printfn "%10s %10s\n" "Decimal" "Hex"
+for value in values do
+ String.Format("{0,10:G}: {0,10:X}", value)
+ |> printfn "%s"
+// The example displays the following output:
+// Decimal Hex
+//
+// -32768: 8000
+// -27: FFE5
+// 0: 0
+// 1042: 412
+// 32767: 7FFF
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/String/Intern/Intern1.fs b/snippets/fsharp/System/String/Intern/Intern1.fs
new file mode 100644
index 00000000000..8443ea61d61
--- /dev/null
+++ b/snippets/fsharp/System/String/Intern/Intern1.fs
@@ -0,0 +1,11 @@
+module Intern1.fs
+open System
+open System.Text
+
+//
+let s1 = "MyTest"
+let s2 = StringBuilder().Append("My").Append("Test").ToString()
+let s3 = String.Intern s2
+printfn $"{s2 :> obj = s1 :> obj}" // Different references.
+printfn $"{s3 :> obj = s1 :> obj}" // The same reference.
+//
diff --git a/snippets/fsharp/System/String/Intern/fs.fsproj b/snippets/fsharp/System/String/Intern/fs.fsproj
index 6d494f6fa40..b7ebb91ff77 100644
--- a/snippets/fsharp/System/String/Intern/fs.fsproj
+++ b/snippets/fsharp/System/String/Intern/fs.fsproj
@@ -6,5 +6,6 @@
+
diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs b/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs
new file mode 100644
index 00000000000..21f1ee3462c
--- /dev/null
+++ b/snippets/fsharp/System/String/IsNullOrEmpty/NullString1.fs
@@ -0,0 +1,19 @@
+namespace IsNullOrEmpty
+open System
+
+module NullString1 =
+
+ //
+ let (s: string) = null
+
+ printfn "The value of the string is '%s'" s
+
+ try
+ printfn "String length is %d" s.Length
+ with
+ | :? NullReferenceException as ex -> printfn "%s" ex.Message
+
+ // The example displays the following output:
+ // The value of the string is ''
+ // Object reference not set to an instance of an object.
+ //
diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs b/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs
new file mode 100644
index 00000000000..d8c8fad9c8c
--- /dev/null
+++ b/snippets/fsharp/System/String/IsNullOrEmpty/NullString2.fs
@@ -0,0 +1,12 @@
+namespace IsNullOrEmpty
+open System
+
+module NullString2 =
+
+ //
+ let s = ""
+ printfn "The length of '%s' is %d." s s.Length
+
+ // The example displays the following output:
+ // The length of '' is 0.
+ //
diff --git a/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs b/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs
new file mode 100644
index 00000000000..7f4e29126fe
--- /dev/null
+++ b/snippets/fsharp/System/String/IsNullOrEmpty/isnullorempty1.fs
@@ -0,0 +1,19 @@
+namespace IsNullOrEmpty
+open System
+
+module IsNullOrEmpty1 =
+
+ //
+ let testForNullOrEmpty (s: string): bool =
+ s = null || s = String.Empty
+
+ let s1 = null
+ let s2 = ""
+
+ printfn "%b" (testForNullOrEmpty s1)
+ printfn "%b" (testForNullOrEmpty s2)
+
+ // The example displays the following output:
+ // true
+ // true
+ //
diff --git a/snippets/fsharp/System/TimeSpan/Overview/fs.fsproj b/snippets/fsharp/System/TimeSpan/Overview/fs.fsproj
index 575e4fe0f4e..90dd1791048 100644
--- a/snippets/fsharp/System/TimeSpan/Overview/fs.fsproj
+++ b/snippets/fsharp/System/TimeSpan/Overview/fs.fsproj
@@ -7,9 +7,6 @@
-
-
-
-
\ No newline at end of file
+
diff --git a/snippets/fsharp/System/TimeSpan/Overview/legacycode1.fs b/snippets/fsharp/System/TimeSpan/Overview/legacycode1.fs
deleted file mode 100644
index 69be56c0f5a..00000000000
--- a/snippets/fsharp/System/TimeSpan/Overview/legacycode1.fs
+++ /dev/null
@@ -1,37 +0,0 @@
-module legacycode1
-
-open System
-
-//
-let showFormattingCode () =
- let interval = TimeSpan(12, 30, 45)
- try
- $"{interval:r}"
- with :? FormatException ->
- "Invalid Format"
- |> printfn "%s"
-
-let showParsingCode () =
- let value = "000000006"
- try
- let interval = TimeSpan.Parse value
- printfn $"{value} --> {interval}"
- with
- | :? FormatException ->
- printfn $"{value}: Bad Format"
- | :? OverflowException ->
- printfn $"{value}: Overflow"
-
-showFormattingCode ()
-// Output from .NET Framework 3.5 and earlier versions:
-// 12:30:45
-// Output from .NET Framework 4:
-// Invalid Format
-
-printfn "---"
-
-showParsingCode ()
-// Output:
-// 000000006 --> 6.00:00:00
-
-//
\ No newline at end of file
diff --git a/snippets/fsharp/System/TimeSpan/Overview/perappdomain1.fs b/snippets/fsharp/System/TimeSpan/Overview/perappdomain1.fs
deleted file mode 100644
index a0c611ba3ce..00000000000
--- a/snippets/fsharp/System/TimeSpan/Overview/perappdomain1.fs
+++ /dev/null
@@ -1,10 +0,0 @@
-module perappdomain1
-
-//
-open System
-
-let appSetup = AppDomainSetup()
-appSetup.SetCompatibilitySwitches [| "NetFx40_TimeSpanLegacyFormatMode" |]
-let legacyDomain = AppDomain.CreateDomain("legacyDomain", null, appSetup)
-legacyDomain.ExecuteAssembly "ShowTimeSpan.exe" |> ignore
-//
\ No newline at end of file
diff --git a/snippets/fsharp/System/TimeSpan/Overview/showtimespan.fs b/snippets/fsharp/System/TimeSpan/Overview/showtimespan.fs
deleted file mode 100644
index 7c0fa6da583..00000000000
--- a/snippets/fsharp/System/TimeSpan/Overview/showtimespan.fs
+++ /dev/null
@@ -1,10 +0,0 @@
-module showtimespan
-
-//
-open System
-
-let interval = DateTime.Now - DateTime.Now.Date
-printfn $"Elapsed Time Today: {interval:d} hours."
-// The example displays the following output:
-// Elapsed Time Today: 01:40:52.2524662 hours.
-//
\ No newline at end of file
diff --git a/snippets/fsharp/System/TimeSpan/Parse/parsefailure1.fs b/snippets/fsharp/System/TimeSpan/Parse/parsefailure1.fs
new file mode 100644
index 00000000000..54ff371765d
--- /dev/null
+++ b/snippets/fsharp/System/TimeSpan/Parse/parsefailure1.fs
@@ -0,0 +1,23 @@
+module parsefailure1
+
+//
+open System
+
+let values = [| "000000006"; "12.12:12:12.12345678" |]
+for value in values do
+ try
+ let interval = TimeSpan.Parse value
+ printfn $"{value} --> {interval}"
+ with
+ | :? FormatException ->
+ printfn $"{value}: Bad Format"
+ | :? OverflowException ->
+ printfn $"{value}: Overflow"
+
+// Output from .NET Framework 3.5 and earlier versions:
+// 000000006 --> 6.00:00:00
+// 12.12:12:12.12345678: Bad Format
+// Output from .NET Framework 4 and later versions or .NET Core:
+// 000000006: Overflow
+// 12.12:12:12.12345678: Overflow
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/Type/GetType/source.fs b/snippets/fsharp/System/Type/GetType/source.fs
new file mode 100644
index 00000000000..9ec4c202801
--- /dev/null
+++ b/snippets/fsharp/System/Type/GetType/source.fs
@@ -0,0 +1,37 @@
+module source
+
+open System
+open System.Reflection
+
+do
+ let test = "System.Collections.Generic.Dictionary`2[System.String,[MyNamespace.MyType, MyAssembly]]"
+ //
+ let t =
+ Type.GetType(test,
+ (fun aName ->
+ if aName.Name = "MyAssembly" then
+ Assembly.LoadFrom @".\MyPath\v5.0\MyAssembly.dll"
+ else null),
+ fun assem name ignr ->
+ if assem = null then
+ Type.GetType(name, false, ignr)
+ else
+ assem.GetType(name, false, ignr))
+ //
+ printfn $"{t}"
+
+ let test = "System.Collections.Generic.Dictionary`2[[YourNamespace.YourType, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], [MyNamespace.MyType, MyAssembly]]"
+ //
+ let t2 =
+ Type.GetType(test,
+ (fun aName ->
+ if aName.Name = "MyAssembly" then
+ Assembly.LoadFrom @".\MyPath\v5.0\MyAssembly.dll"
+ else Assembly.Load aName),
+ (fun assem name ignr ->
+ if assem = null then
+ Type.GetType(name, false, ignr)
+ else
+ assem.GetType(name, false, ignr)), true)
+ //
+ printfn $"{t2}"
diff --git a/snippets/fsharp/System/Type/MakeGenericType/remarks.fs b/snippets/fsharp/System/Type/MakeGenericType/remarks.fs
new file mode 100644
index 00000000000..bece475149a
--- /dev/null
+++ b/snippets/fsharp/System/Type/MakeGenericType/remarks.fs
@@ -0,0 +1,5 @@
+module remarks
+//
+type Base<'T, 'U>() = class end
+type Derived<'V>() = inherit Base()
+//
diff --git a/snippets/fsharp/System/Version/Overview/fs.fsproj b/snippets/fsharp/System/Version/Overview/fs.fsproj
index 301ed38758c..847eb1a60c8 100644
--- a/snippets/fsharp/System/Version/Overview/fs.fsproj
+++ b/snippets/fsharp/System/Version/Overview/fs.fsproj
@@ -8,9 +8,8 @@
-
-
\ No newline at end of file
+
diff --git a/snippets/fsharp/System/Version/Overview/specificassem.fs b/snippets/fsharp/System/Version/Overview/specificassem.fs
deleted file mode 100644
index f930a4460da..00000000000
--- a/snippets/fsharp/System/Version/Overview/specificassem.fs
+++ /dev/null
@@ -1,12 +0,0 @@
-module specificassem
-
-//
-open System.Reflection
-
-// Get the version of a specific assembly.
-let filename = @".\StringLibrary.dll"
-let assem = Assembly.ReflectionOnlyLoadFrom filename
-let assemName = assem.GetName()
-let ver = assemName.Version
-printfn $"{assemName.Name}, Version {ver}"
-//
\ No newline at end of file
diff --git a/snippets/visualbasic/System/Double/Overview/Project.vbproj b/snippets/visualbasic/System/Double/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Double/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Double/Overview/precisionlist1.vb b/snippets/visualbasic/System/Double/Overview/precisionlist1.vb
new file mode 100644
index 00000000000..efbde3e8e96
--- /dev/null
+++ b/snippets/visualbasic/System/Double/Overview/precisionlist1.vb
@@ -0,0 +1,15 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example10
+ Public Sub Main()
+ Dim value1 As Double = 1 / 3
+ Dim sValue2 As Single = 1 / 3
+ Dim value2 As Double = CDbl(sValue2)
+ Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
+ End Sub
+End Module
+' The example displays the following output:
+' 0.33333333333333331 = 0.3333333432674408: False
+'
diff --git a/snippets/visualbasic/System/Random/Overview/Project.vbproj b/snippets/visualbasic/System/Random/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Random/Overview/Random1.vb b/snippets/visualbasic/System/Random/Overview/Random1.vb
new file mode 100644
index 00000000000..997827ecf77
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/Random1.vb
@@ -0,0 +1,55 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'Imports System.Threading
+
+'
+Module modMain
+
+ Public Sub Main()
+ Dim bytes1(99), bytes2(99) As Byte
+ Dim rnd1 As New Random()
+ Dim rnd2 As New Random()
+
+ rnd1.NextBytes(bytes1)
+ rnd2.NextBytes(bytes2)
+
+ Console.WriteLine("First Series:")
+ For ctr As Integer = bytes1.GetLowerBound(0) to bytes1.GetUpperBound(0)
+ Console.Write("{0, 5}", bytes1(ctr))
+ If (ctr + 1) Mod 10 = 0 Then Console.WriteLine()
+ Next
+ Console.WriteLine()
+ Console.WriteLine("Second Series:")
+ For ctr As Integer = bytes2.GetLowerBound(0) to bytes2.GetUpperBound(0)
+ Console.Write("{0, 5}", bytes2(ctr))
+ If (ctr + 1) Mod 10 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' First Series:
+' 97 129 149 54 22 208 120 105 68 177
+' 113 214 30 172 74 218 116 230 89 18
+' 12 112 130 105 116 180 190 200 187 120
+' 7 198 233 158 58 51 50 170 98 23
+' 21 1 113 74 146 245 34 255 96 24
+' 232 255 23 9 167 240 255 44 194 98
+' 18 175 173 204 169 171 236 127 114 23
+' 167 202 132 65 253 11 254 56 214 127
+' 145 191 104 163 143 7 174 224 247 73
+' 52 6 231 255 5 101 83 165 160 231
+'
+' Second Series:
+' 97 129 149 54 22 208 120 105 68 177
+' 113 214 30 172 74 218 116 230 89 18
+' 12 112 130 105 116 180 190 200 187 120
+' 7 198 233 158 58 51 50 170 98 23
+' 21 1 113 74 146 245 34 255 96 24
+' 232 255 23 9 167 240 255 44 194 98
+' 18 175 173 204 169 171 236 127 114 23
+' 167 202 132 65 253 11 254 56 214 127
+' 145 191 104 163 143 7 174 224 247 73
+' 52 6 231 255 5 101 83 165 160 231
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/Random2.vb b/snippets/visualbasic/System/Random/Overview/Random2.vb
index 8d5de19cef6..a165a36d04e 100644
--- a/snippets/visualbasic/System/Random/Overview/Random2.vb
+++ b/snippets/visualbasic/System/Random/Overview/Random2.vb
@@ -2,48 +2,48 @@
Option Strict On
'
-Module Example
- Public Sub Main()
- ' Instantiate random number generator using system-supplied value as seed.
- Dim rand As New Random()
- ' Generate and display 5 random byte (integer) values.
- Dim bytes(4) As Byte
- rand.NextBytes(bytes)
- Console.WriteLine("Five random byte values:")
- For Each byteValue As Byte In bytes
- Console.Write("{0, 5}", byteValue)
- Next
- Console.WriteLine()
- ' Generate and display 5 random integers.
- Console.WriteLine("Five random integer values:")
- For ctr As Integer = 0 To 4
- Console.Write("{0,15:N0}", rand.Next)
- Next
- Console.WriteLine()
- ' Generate and display 5 random integers between 0 and 100.'
- Console.WriteLine("Five random integers between 0 and 100:")
- For ctr As Integer = 0 To 4
- Console.Write("{0,8:N0}", rand.Next(101))
- Next
- Console.WriteLine()
- ' Generate and display 5 random integers from 50 to 100.
- Console.WriteLine("Five random integers between 50 and 100:")
- For ctr As Integer = 0 To 4
- Console.Write("{0,8:N0}", rand.Next(50, 101))
- Next
- Console.WriteLine()
- ' Generate and display 5 random floating point values from 0 to 1.
- Console.WriteLine("Five Doubles.")
- For ctr As Integer = 0 To 4
- Console.Write("{0,8:N3}", rand.NextDouble())
- Next
- Console.WriteLine()
- ' Generate and display 5 random floating point values from 0 to 5.
- Console.WriteLine("Five Doubles between 0 and 5.")
- For ctr As Integer = 0 To 4
- Console.Write("{0,8:N3}", rand.NextDouble() * 5)
- Next
- End Sub
+Module RandomExample2
+ Public Sub Main()
+ ' Instantiate random number generator using system-supplied value as seed.
+ Dim rand As New Random()
+ ' Generate and display 5 random byte (integer) values.
+ Dim bytes(4) As Byte
+ rand.NextBytes(bytes)
+ Console.WriteLine("Five random byte values:")
+ For Each byteValue As Byte In bytes
+ Console.Write("{0, 5}", byteValue)
+ Next
+ Console.WriteLine()
+ ' Generate and display 5 random integers.
+ Console.WriteLine("Five random integer values:")
+ For ctr As Integer = 0 To 4
+ Console.Write("{0,15:N0}", rand.Next)
+ Next
+ Console.WriteLine()
+ ' Generate and display 5 random integers between 0 and 100.'
+ Console.WriteLine("Five random integers between 0 and 100:")
+ For ctr As Integer = 0 To 4
+ Console.Write("{0,8:N0}", rand.Next(101))
+ Next
+ Console.WriteLine()
+ ' Generate and display 5 random integers from 50 to 100.
+ Console.WriteLine("Five random integers between 50 and 100:")
+ For ctr As Integer = 0 To 4
+ Console.Write("{0,8:N0}", rand.Next(50, 101))
+ Next
+ Console.WriteLine()
+ ' Generate and display 5 random floating point values from 0 to 1.
+ Console.WriteLine("Five Doubles.")
+ For ctr As Integer = 0 To 4
+ Console.Write("{0,8:N3}", rand.NextDouble())
+ Next
+ Console.WriteLine()
+ ' Generate and display 5 random floating point values from 0 to 5.
+ Console.WriteLine("Five Doubles between 0 and 5.")
+ For ctr As Integer = 0 To 4
+ Console.Write("{0,8:N3}", rand.NextDouble() * 5)
+ Next
+ End Sub
End Module
' The example displays output like the following:
' Five random byte values:
diff --git a/snippets/visualbasic/System/Random/Overview/array1.vb b/snippets/visualbasic/System/Random/Overview/array1.vb
new file mode 100644
index 00000000000..1e229ea93a4
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/array1.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim cities() As String = {"Atlanta", "Boston", "Chicago", "Detroit",
+ "Fort Wayne", "Greensboro", "Honolulu", "Indianapolis",
+ "Jersey City", "Kansas City", "Los Angeles",
+ "Milwaukee", "New York", "Omaha", "Philadelphia",
+ "Raleigh", "San Francisco", "Tulsa", "Washington"}
+ Dim rnd As New Random()
+ Dim index As Integer = rnd.Next(0, cities.Length)
+ Console.WriteLine("Today's city of the day: {0}",
+ cities(index))
+ End Sub
+End Module
+' The example displays output like the following:
+' Today's city of the day: Honolulu
+'
diff --git a/snippets/visualbasic/System/Random/Overview/booleans1.vb b/snippets/visualbasic/System/Random/Overview/booleans1.vb
new file mode 100644
index 00000000000..5f9c954af26
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/booleans1.vb
@@ -0,0 +1,43 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example2
+ Public Sub Main()
+ ' Instantiate the Boolean generator.
+ Dim boolGen As New BooleanGenerator()
+ Dim totalTrue, totalFalse As Integer
+
+ ' Generate 1,0000 random Booleans, and keep a running total.
+ For ctr As Integer = 0 To 9999999
+ Dim value As Boolean = boolGen.NextBoolean()
+ If value Then
+ totalTrue += 1
+ Else
+ totalFalse += 1
+ End If
+ Next
+ Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
+ totalTrue,
+ totalTrue / (totalTrue + totalFalse))
+ Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
+ totalFalse,
+ totalFalse / (totalTrue + totalFalse))
+ End Sub
+End Module
+
+Public Class BooleanGenerator
+ Dim rnd As Random
+
+ Public Sub New()
+ rnd = New Random()
+ End Sub
+
+ Public Function NextBoolean() As Boolean
+ Return Convert.ToBoolean(rnd.Next(0, 2))
+ End Function
+End Class
+' The example displays the following output:
+' Number of true values: 500,004 (50.000 %)
+' Number of false values: 499,996 (50.000 %)
+'
diff --git a/snippets/visualbasic/System/Random/Overview/booleans2.vb b/snippets/visualbasic/System/Random/Overview/booleans2.vb
new file mode 100644
index 00000000000..7d6de5554e5
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/booleans2.vb
@@ -0,0 +1,34 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example3
+ Public Sub Main()
+ Dim totalTrue, totalFalse As Integer
+
+ ' Generate 1,0000 random Booleans, and keep a running total.
+ For ctr As Integer = 0 To 9999999
+ Dim value As Boolean = NextBoolean()
+ If value Then
+ totalTrue += 1
+ Else
+ totalFalse += 1
+ End If
+ Next
+ Console.WriteLine("Number of true values: {0,7:N0} ({1:P3})",
+ totalTrue,
+ totalTrue / (totalTrue + totalFalse))
+ Console.WriteLine("Number of false values: {0,7:N0} ({1:P3})",
+ totalFalse,
+ totalFalse / (totalTrue + totalFalse))
+ End Sub
+
+ Public Function NextBoolean() As Boolean
+ Static rnd As New Random()
+ Return Convert.ToBoolean(rnd.Next(0, 2))
+ End Function
+End Module
+' The example displays the following output:
+' Number of true values: 499,777 (49.978 %)
+' Number of false values: 500,223 (50.022 %)
+'
diff --git a/snippets/visualbasic/System/Random/Overview/bytes1.vb b/snippets/visualbasic/System/Random/Overview/bytes1.vb
new file mode 100644
index 00000000000..0aac57c768a
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/bytes1.vb
@@ -0,0 +1,64 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example4
+ Public Sub Main()
+ Dim rnd As New Random2()
+ Dim bytes(9999) As Byte
+ Dim total(100) As Integer
+ rnd.NextBytes(bytes, 0, 101)
+
+ ' Calculate how many of each value we have.
+ For Each value In bytes
+ total(value) += 1
+ Next
+
+ ' Display the results.
+ For ctr As Integer = 0 To total.Length - 1
+ Console.Write("{0,3}: {1,-3} ", ctr, total(ctr))
+ If (ctr + 1) Mod 5 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+
+Public Class Random2 : Inherits Random
+ Public Sub New()
+ MyBase.New()
+ End Sub
+
+ Public Sub New(seed As Integer)
+ MyBase.New(seed)
+ End Sub
+
+ Public Overloads Sub NextBytes(bytes() As Byte,
+ minValue As Byte, maxValue As Byte)
+ For ctr As Integer = bytes.GetLowerbound(0) To bytes.GetUpperBound(0)
+ bytes(ctr) = CByte(MyBase.Next(minValue, maxValue))
+ Next
+ End Sub
+End Class
+' The example displays output like the following:
+' 0: 115 1: 119 2: 92 3: 98 4: 92
+' 5: 102 6: 103 7: 84 8: 93 9: 116
+' 10: 91 11: 98 12: 106 13: 91 14: 92
+' 15: 101 16: 100 17: 96 18: 97 19: 100
+' 20: 101 21: 106 22: 112 23: 82 24: 85
+' 25: 102 26: 107 27: 98 28: 106 29: 102
+' 30: 109 31: 108 32: 94 33: 101 34: 107
+' 35: 101 36: 86 37: 100 38: 101 39: 102
+' 40: 113 41: 95 42: 96 43: 89 44: 99
+' 45: 81 46: 89 47: 105 48: 100 49: 85
+' 50: 103 51: 103 52: 93 53: 89 54: 91
+' 55: 97 56: 105 57: 97 58: 110 59: 86
+' 60: 116 61: 94 62: 117 63: 98 64: 110
+' 65: 93 66: 102 67: 100 68: 105 69: 83
+' 70: 81 71: 97 72: 85 73: 70 74: 98
+' 75: 100 76: 110 77: 114 78: 83 79: 90
+' 80: 96 81: 112 82: 102 83: 102 84: 99
+' 85: 81 86: 100 87: 93 88: 99 89: 118
+' 90: 95 91: 124 92: 108 93: 96 94: 104
+' 95: 106 96: 99 97: 99 98: 92 99: 99
+' 100: 108
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/doublerange1.vb b/snippets/visualbasic/System/Random/Overview/doublerange1.vb
new file mode 100644
index 00000000000..86f6298c6c3
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/doublerange1.vb
@@ -0,0 +1,42 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example5
+ Public Sub Main()
+ Const ONE_TENTH As Long = 922337203685477581
+
+ Dim rnd As New Random()
+ Dim number As Long
+ Dim count(9) As Integer
+
+ ' Generate 20 million integer values.
+ For ctr As Integer = 1 To 20000000
+ number = CLng(rnd.NextDouble() * Int64.MaxValue)
+ ' Categorize random numbers.
+ count(CInt(number \ ONE_TENTH)) += 1
+ Next
+ ' Display breakdown by range.
+ Console.WriteLine("{0,28} {1,32} {2,7}", "Range", "Count", "Pct.")
+ Console.WriteLine()
+ For ctr As Integer = 0 To 9
+ Console.WriteLine("{0,25:N0}-{1,25:N0} {2,8:N0} {3,7:P2}", ctr * ONE_TENTH,
+ If(ctr < 9, ctr * ONE_TENTH + ONE_TENTH - 1, Int64.MaxValue),
+ count(ctr), count(ctr) / 20000000)
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' Range Count Pct.
+'
+' 0- 922,337,203,685,477,580 1,996,148 9.98 %
+' 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+' 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+' 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+' 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+' 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+' 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+' 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+' 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+' 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+'
diff --git a/snippets/visualbasic/System/Random/Overview/doublerange2.vb b/snippets/visualbasic/System/Random/Overview/doublerange2.vb
new file mode 100644
index 00000000000..9ece828201a
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/doublerange2.vb
@@ -0,0 +1,24 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example6
+ Public Sub Main()
+ Dim rnd As New Random()
+ For ctr As Integer = 1 To 10
+ Console.WriteLine(rnd.NextDouble() - 1)
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' -0.930412760437658
+' -0.164699016215605
+' -0.9851692803135
+' -0.43468508843085
+' -0.177202483255976
+' -0.776813320245972
+' -0.0713201854710096
+' -0.0912875561468711
+' -0.540621722368813
+' -0.232211863730201
+'
diff --git a/snippets/visualbasic/System/Random/Overview/doublerange3.vb b/snippets/visualbasic/System/Random/Overview/doublerange3.vb
new file mode 100644
index 00000000000..0a6c9701504
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/doublerange3.vb
@@ -0,0 +1,36 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example7
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim lowerBound As Integer = 10
+ Dim upperBound As Integer = 11
+ Dim range(9) As Integer
+ For ctr As Integer = 1 To 1000000
+ Dim value As Double = rnd.NextDouble() * (upperBound - lowerBound) + lowerBound
+ range(CInt(Math.Truncate((value - lowerBound) * 10))) += 1
+ Next
+
+ For ctr As Integer = 0 To 9
+ Dim lowerRange As Double = 10 + ctr * 0.1
+ Console.WriteLine("{0:N1} to {1:N1}: {2,8:N0} ({3,7:P2})",
+ lowerRange, lowerRange + 0.1, range(ctr),
+ range(ctr) / 1000000.0)
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 10.0 to 10.1: 99,929 ( 9.99 %)
+' 10.1 to 10.2: 100,189 (10.02 %)
+' 10.2 to 10.3: 99,384 ( 9.94 %)
+' 10.3 to 10.4: 100,240 (10.02 %)
+' 10.4 to 10.5: 99,397 ( 9.94 %)
+' 10.5 to 10.6: 100,580 (10.06 %)
+' 10.6 to 10.7: 100,293 (10.03 %)
+' 10.7 to 10.8: 100,135 (10.01 %)
+' 10.8 to 10.9: 99,905 ( 9.99 %)
+' 10.9 to 11.0: 99,948 ( 9.99 %)
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/long1.vb b/snippets/visualbasic/System/Random/Overview/long1.vb
new file mode 100644
index 00000000000..5123939faf9
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/long1.vb
@@ -0,0 +1,42 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example8
+ Public Sub Main()
+ Const ONE_TENTH As Long = 922337203685477581
+
+ Dim rnd As New Random()
+ Dim number As Long
+ Dim count(9) As Integer
+
+ ' Generate 20 million long integers.
+ For ctr As Integer = 1 To 20000000
+ number = CLng(rnd.NextDouble() * Int64.MaxValue)
+ ' Categorize random numbers.
+ count(CInt(number \ ONE_TENTH)) += 1
+ Next
+ ' Display breakdown by range.
+ Console.WriteLine("{0,28} {1,32} {2,7}", "Range", "Count", "Pct.")
+ Console.WriteLine()
+ For ctr As Integer = 0 To 9
+ Console.WriteLine("{0,25:N0}-{1,25:N0} {2,8:N0} {3,7:P2}", ctr * ONE_TENTH,
+ If(ctr < 9, ctr * ONE_TENTH + ONE_TENTH - 1, Int64.MaxValue),
+ count(ctr), count(ctr) / 20000000)
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' Range Count Pct.
+'
+' 0- 922,337,203,685,477,580 1,996,148 9.98 %
+' 922,337,203,685,477,581-1,844,674,407,370,955,161 2,000,293 10.00 %
+' 1,844,674,407,370,955,162-2,767,011,611,056,432,742 2,000,094 10.00 %
+' 2,767,011,611,056,432,743-3,689,348,814,741,910,323 2,000,159 10.00 %
+' 3,689,348,814,741,910,324-4,611,686,018,427,387,904 1,999,552 10.00 %
+' 4,611,686,018,427,387,905-5,534,023,222,112,865,485 1,998,248 9.99 %
+' 5,534,023,222,112,865,486-6,456,360,425,798,343,066 2,000,696 10.00 %
+' 6,456,360,425,798,343,067-7,378,697,629,483,820,647 2,001,637 10.01 %
+' 7,378,697,629,483,820,648-8,301,034,833,169,298,228 2,002,870 10.01 %
+' 8,301,034,833,169,298,229-9,223,372,036,854,775,807 2,000,303 10.00 %
+'
diff --git a/snippets/visualbasic/System/Random/Overview/next2.vb b/snippets/visualbasic/System/Random/Overview/next2.vb
index fe358629f8c..74fce58aa35 100644
--- a/snippets/visualbasic/System/Random/Overview/next2.vb
+++ b/snippets/visualbasic/System/Random/Overview/next2.vb
@@ -2,30 +2,30 @@
Option Strict On
'
-Module Example
- Public Sub Main()
- Dim rnd As New Random()
+Module NextExample2
+ Public Sub Main()
+ Dim rnd As New Random()
- Console.WriteLine("20 random integers from -100 to 100:")
- For ctr As Integer = 1 To 20
- Console.Write("{0,6}", rnd.Next(-100, 101))
- If ctr Mod 5 = 0 Then Console.WriteLine()
- Next
- Console.WriteLine()
-
- Console.WriteLine("20 random integers from 1000 to 10000:")
- For ctr As Integer = 1 To 20
- Console.Write("{0,8}", rnd.Next(1000, 10001))
- If ctr Mod 5 = 0 Then Console.WriteLine()
- Next
- Console.WriteLine()
-
- Console.WriteLine("20 random integers from 1 to 10:")
- For ctr As Integer = 1 To 20
- Console.Write("{0,6}", rnd.Next(1, 11))
- If ctr Mod 5 = 0 Then Console.WriteLine()
- Next
- End Sub
+ Console.WriteLine("20 random integers from -100 to 100:")
+ For ctr As Integer = 1 To 20
+ Console.Write("{0,6}", rnd.Next(-100, 101))
+ If ctr Mod 5 = 0 Then Console.WriteLine()
+ Next
+ Console.WriteLine()
+
+ Console.WriteLine("20 random integers from 1000 to 10000:")
+ For ctr As Integer = 1 To 20
+ Console.Write("{0,8}", rnd.Next(1000, 10001))
+ If ctr Mod 5 = 0 Then Console.WriteLine()
+ Next
+ Console.WriteLine()
+
+ Console.WriteLine("20 random integers from 1 to 10:")
+ For ctr As Integer = 1 To 20
+ Console.Write("{0,6}", rnd.Next(1, 11))
+ If ctr Mod 5 = 0 Then Console.WriteLine()
+ Next
+ End Sub
End Module
' The example displays output similar to the following:
' 20 random integers from -100 to 100:
diff --git a/snippets/visualbasic/System/Random/Overview/next3.vb b/snippets/visualbasic/System/Random/Overview/next3.vb
index 08a59076fac..11fa60964d9 100644
--- a/snippets/visualbasic/System/Random/Overview/next3.vb
+++ b/snippets/visualbasic/System/Random/Overview/next3.vb
@@ -2,19 +2,19 @@
Option Strict On
'
-Module Example
- Public Sub Main()
- Console.Write("Number of random numbers to generate: ")
- Dim line As String = Console.ReadLine()
- Dim numbers As UInteger = 0
- Dim rnd As New Random()
-
- If Not UInt32.TryParse(line, numbers) Then numbers = 10
-
- For ctr As UInteger = 1 To numbers
- Console.WriteLine("{0,15:N0}", rnd.Next())
- Next
- End Sub
+Module NextExample3
+ Public Sub Main()
+ Console.Write("Number of random numbers to generate: ")
+ Dim line As String = Console.ReadLine()
+ Dim numbers As UInteger = 0
+ Dim rnd As New Random()
+
+ If Not UInt32.TryParse(line, numbers) Then numbers = 10
+
+ For ctr As UInteger = 1 To numbers
+ Console.WriteLine("{0,15:N0}", rnd.Next())
+ Next
+ End Sub
End Module
' The example displays output like the following when asked to generate
' 15 random numbers:
diff --git a/snippets/visualbasic/System/Random/Overview/next4.vb b/snippets/visualbasic/System/Random/Overview/next4.vb
index 01f103fa4d2..e972ae6caa5 100644
--- a/snippets/visualbasic/System/Random/Overview/next4.vb
+++ b/snippets/visualbasic/System/Random/Overview/next4.vb
@@ -2,25 +2,25 @@
Option Strict On
'
-Module Example
- Public Sub Main()
- Dim rnd As New Random()
- Dim malePetNames() As String = { "Rufus", "Bear", "Dakota", "Fido",
- "Vanya", "Samuel", "Koani", "Volodya",
- "Prince", "Yiska" }
- Dim femalePetNames() As String = { "Maggie", "Penny", "Saya", "Princess",
- "Abby", "Laila", "Sadie", "Olivia",
- "Starlight", "Talla" }
-
- ' Generate random indexes for pet names.
- Dim mIndex As Integer = rnd.Next(0, malePetNames.Length)
- Dim fIndex As Integer = rnd.Next(0, femalePetNames.Length)
-
- ' Display the result.
- Console.WriteLine("Suggested pet name of the day: ")
- Console.WriteLine(" For a male: {0}", malePetNames(mIndex))
- Console.WriteLine(" For a female: {0}", femalePetNames(fIndex))
- End Sub
+Module NextExample4
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim malePetNames() As String = {"Rufus", "Bear", "Dakota", "Fido",
+ "Vanya", "Samuel", "Koani", "Volodya",
+ "Prince", "Yiska"}
+ Dim femalePetNames() As String = {"Maggie", "Penny", "Saya", "Princess",
+ "Abby", "Laila", "Sadie", "Olivia",
+ "Starlight", "Talla"}
+
+ ' Generate random indexes for pet names.
+ Dim mIndex As Integer = rnd.Next(0, malePetNames.Length)
+ Dim fIndex As Integer = rnd.Next(0, femalePetNames.Length)
+
+ ' Display the result.
+ Console.WriteLine("Suggested pet name of the day: ")
+ Console.WriteLine(" For a male: {0}", malePetNames(mIndex))
+ Console.WriteLine(" For a female: {0}", femalePetNames(fIndex))
+ End Sub
End Module
' The example displays output like the following:
' Suggested pet name of the day:
diff --git a/snippets/visualbasic/System/Random/Overview/nextbytes1.vb b/snippets/visualbasic/System/Random/Overview/nextbytes1.vb
new file mode 100644
index 00000000000..0f900436114
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/nextbytes1.vb
@@ -0,0 +1,19 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example9
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim bytes(19) As Byte
+ rnd.NextBytes(bytes)
+ For ctr As Integer = 1 To bytes.Length
+ Console.Write("{0,3} ", bytes(ctr - 1))
+ If ctr Mod 10 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 141 48 189 66 134 212 211 71 161 56
+' 181 166 220 133 9 252 222 57 62 62
+'
diff --git a/snippets/visualbasic/System/Random/Overview/nextdoubleex1.vb b/snippets/visualbasic/System/Random/Overview/nextdoubleex1.vb
new file mode 100644
index 00000000000..d8c63d68654
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/nextdoubleex1.vb
@@ -0,0 +1,19 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example10
+ Public Sub Main()
+ Dim rnd As New Random()
+ For ctr As Integer = 0 To 9
+ Console.Write("{0,-19:R} ", rnd.NextDouble())
+ If (ctr + 1) Mod 3 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 0.7911680553998649 0.0903414949264105 0.79776258291572455
+' 0.615568345233597 0.652644504165577 0.84023809378977776
+' 0.099662564741290441 0.91341467383942321 0.96018602045261581
+' 0.74772306473354022
+'
diff --git a/snippets/visualbasic/System/Random/Overview/nextex1.vb b/snippets/visualbasic/System/Random/Overview/nextex1.vb
new file mode 100644
index 00000000000..c7fec4d9a8a
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/nextex1.vb
@@ -0,0 +1,15 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example11
+ Public Sub Main()
+ Dim rnd As New Random()
+ For ctr As Integer = 0 To 9
+ Console.Write("{0,3} ", rnd.Next(-10, 11))
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 2 9 -3 2 4 -7 -3 -8 -8 5
+'
diff --git a/snippets/visualbasic/System/Random/Overview/range1.vb b/snippets/visualbasic/System/Random/Overview/range1.vb
new file mode 100644
index 00000000000..d8a9beacb5d
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/range1.vb
@@ -0,0 +1,18 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example12
+ Public Sub Main()
+ Dim rnd As New Random()
+ For ctr As Integer = 1 To 15
+ Console.Write("{0,3} ", rnd.Next(-10, 11))
+ If ctr Mod 5 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' -2 -5 -1 -2 10
+' -3 6 -4 -8 3
+' -7 10 5 -2 4
+'
diff --git a/snippets/visualbasic/System/Random/Overview/range2.vb b/snippets/visualbasic/System/Random/Overview/range2.vb
new file mode 100644
index 00000000000..f2fa6e2d382
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/range2.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example13
+ Public Sub Main()
+ Dim rnd As New Random()
+ For ctr As Integer = 1 To 50
+ Console.Write("{0,3} ", rnd.Next(1000, 10000))
+ If ctr Mod 10 = 0 Then Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 9570 8979 5770 1606 3818 4735 8495 7196 7070 2313
+' 5279 6577 5104 5734 4227 3373 7376 6007 8193 5540
+' 7558 3934 3819 7392 1113 7191 6947 4963 9179 7907
+' 3391 6667 7269 1838 7317 1981 5154 7377 3297 5320
+' 9869 8694 2684 4949 2999 3019 2357 5211 9604 2593
+'
diff --git a/snippets/visualbasic/System/Random/Overview/same1.vb b/snippets/visualbasic/System/Random/Overview/same1.vb
new file mode 100644
index 00000000000..b7afc104908
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/same1.vb
@@ -0,0 +1,88 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.IO
+
+Module Example14
+ Public Sub Main()
+ Dim seed As Integer = 100100
+ ShowRandomNumbers(seed)
+ Console.WriteLine()
+
+ PersistSeed(seed)
+
+ DisplayNewRandomNumbers()
+ End Sub
+
+ Private Sub ShowRandomNumbers(seed As Integer)
+ Dim rnd As New Random(seed)
+ For ctr As Integer = 0 To 20
+ Console.WriteLine(rnd.NextDouble())
+ Next
+ End Sub
+
+ Private Sub PersistSeed(seed As Integer)
+ Dim fs As New FileStream(".\seed.dat", FileMode.Create)
+ Dim bin As New BinaryWriter(fs)
+ bin.Write(seed)
+ bin.Close()
+ End Sub
+
+ Private Sub DisplayNewRandomNumbers()
+ Dim fs As New FileStream(".\seed.dat", FileMode.Open)
+ Dim bin As New BinaryReader(fs)
+ Dim seed As Integer = bin.ReadInt32()
+ bin.Close()
+
+ Dim rnd As New Random(seed)
+ For ctr As Integer = 0 To 20
+ Console.WriteLine(rnd.NextDouble())
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' 0.500193602172748
+' 0.0209461245783354
+' 0.465869495396442
+' 0.195512794514891
+' 0.928583675496552
+' 0.729333720509584
+' 0.381455668891527
+' 0.0508996467343064
+' 0.019261200921266
+' 0.258578445417145
+' 0.0177532266908107
+' 0.983277184415272
+' 0.483650274334313
+' 0.0219647376900375
+' 0.165910115077118
+' 0.572085966622497
+' 0.805291457942357
+' 0.927985211335116
+' 0.4228545699375
+' 0.523320379910674
+' 0.157783938645285
+'
+' 0.500193602172748
+' 0.0209461245783354
+' 0.465869495396442
+' 0.195512794514891
+' 0.928583675496552
+' 0.729333720509584
+' 0.381455668891527
+' 0.0508996467343064
+' 0.019261200921266
+' 0.258578445417145
+' 0.0177532266908107
+' 0.983277184415272
+' 0.483650274334313
+' 0.0219647376900375
+' 0.165910115077118
+' 0.572085966622497
+' 0.805291457942357
+' 0.927985211335116
+' 0.4228545699375
+' 0.523320379910674
+' 0.157783938645285
+'
diff --git a/snippets/visualbasic/System/Random/Overview/threadsafeex1.vb b/snippets/visualbasic/System/Random/Overview/threadsafeex1.vb
new file mode 100644
index 00000000000..bf1411760da
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/threadsafeex1.vb
@@ -0,0 +1,144 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Threading
+
+Module Example15
+ Dim previous As Double = 0.0
+ Dim perThreadCtr As Integer = 0
+ Dim perThreadTotal As Double = 0.0
+ Dim source As New CancellationTokenSource()
+ Dim countdown As New CountdownEvent(1)
+ Dim randLock As New Object()
+ Dim numericLock As New Object()
+ Dim rand As New Random()
+ Dim totalValue As Double = 0.0
+ Dim totalCount As Integer = 0
+
+ Public Sub Main()
+ Thread.CurrentThread.Name = "Main"
+
+ Dim token As CancellationToken = source.Token
+ For threads As Integer = 1 To 10
+ Dim newThread As New Thread(AddressOf GetRandomNumbers)
+ newThread.Name = threads.ToString()
+ newThread.Start(token)
+ Next
+ GetRandomNumbers(token)
+
+ countdown.Signal()
+ ' Make sure all threads have finished.
+ countdown.Wait()
+
+ Console.WriteLine()
+ Console.WriteLine("Total random numbers generated: {0:N0}", totalCount)
+ Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue)
+ Console.WriteLine("Random number mean: {0:N4}", totalValue / totalCount)
+ End Sub
+
+ Private Sub GetRandomNumbers(o As Object)
+ Dim token As CancellationToken = CType(o, CancellationToken)
+ Dim result As Double = 0.0
+ countdown.AddCount(1)
+
+ Try
+ For ctr As Integer = 1 To 2000000
+ ' Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ SyncLock randLock
+ result = rand.NextDouble()
+ End SyncLock
+ ' Check for corruption of Random instance.
+ If result = previous AndAlso result = 0 Then
+ source.Cancel()
+ Else
+ previous = result
+ End If
+ perThreadCtr += 1
+ perThreadTotal += result
+ Next
+
+ Console.WriteLine("Thread {0} finished execution.",
+ Thread.CurrentThread.Name)
+ Console.WriteLine("Random numbers generated: {0:N0}", perThreadCtr)
+ Console.WriteLine("Sum of random numbers: {0:N2}", perThreadTotal)
+ Console.WriteLine("Random number mean: {0:N4}", perThreadTotal / perThreadCtr)
+ Console.WriteLine()
+
+ ' Update overall totals.
+ SyncLock numericLock
+ totalCount += perThreadCtr
+ totalValue += perThreadTotal
+ End SyncLock
+ Catch e As OperationCanceledException
+ Console.WriteLine("Corruption in Thread {1}", e.GetType().Name, Thread.CurrentThread.Name)
+ Finally
+ countdown.Signal()
+ source.Dispose()
+ End Try
+ End Sub
+End Module
+' The example displays output like the following:
+' Thread 6 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,491.05
+' Random number mean: 0.5002
+'
+' Thread 10 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,329.64
+' Random number mean: 0.4997
+'
+' Thread 4 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,166.89
+' Random number mean: 0.5001
+'
+' Thread 8 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,628.37
+' Random number mean: 0.4998
+'
+' Thread Main finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,920.89
+' Random number mean: 0.5000
+'
+' Thread 3 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,370.45
+' Random number mean: 0.4997
+'
+' Thread 7 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,330.92
+' Random number mean: 0.4997
+'
+' Thread 9 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,172.79
+' Random number mean: 0.5001
+'
+' Thread 5 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,079.43
+' Random number mean: 0.5000
+'
+' Thread 1 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,817.91
+' Random number mean: 0.4999
+'
+' Thread 2 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,930.63
+' Random number mean: 0.5000
+'
+'
+' Total random numbers generated: 22,000,000
+' Total sum of all random numbers: 10,998,238.98
+' Random number mean: 0.4999
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/threadsafeex2.vb b/snippets/visualbasic/System/Random/Overview/threadsafeex2.vb
new file mode 100644
index 00000000000..2e01f27849d
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/threadsafeex2.vb
@@ -0,0 +1,143 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Collections.Generic
+Imports System.Threading
+Imports System.Threading.Tasks
+
+Module Example16
+ Dim source As New CancellationTokenSource()
+ Dim randLock As New Object()
+ Dim numericLock As New Object()
+ Dim rand As New Random()
+ Dim totalValue As Double = 0.0
+ Dim totalCount As Integer = 0
+
+ Public Sub Main()
+ Dim tasks As New List(Of Task)()
+
+ For ctr As Integer = 1 To 10
+ Dim token As CancellationToken = source.Token
+ Dim taskNo As Integer = ctr
+ tasks.Add(Task.Run(
+ Sub()
+ Dim previous As Double = 0.0
+ Dim taskCtr As Integer = 0
+ Dim taskTotal As Double = 0.0
+ Dim result As Double = 0.0
+
+ For n As Integer = 1 To 2000000
+ ' Make sure there's no corruption of Random.
+ token.ThrowIfCancellationRequested()
+
+ SyncLock randLock
+ result = rand.NextDouble()
+ End SyncLock
+ ' Check for corruption of Random instance.
+ If result = previous AndAlso result = 0 Then
+ source.Cancel()
+ Else
+ previous = result
+ End If
+ taskCtr += 1
+ taskTotal += result
+ Next
+
+ ' Show result.
+ Console.WriteLine("Task {0} finished execution.", taskNo)
+ Console.WriteLine("Random numbers generated: {0:N0}", taskCtr)
+ Console.WriteLine("Sum of random numbers: {0:N2}", taskTotal)
+ Console.WriteLine("Random number mean: {0:N4}", taskTotal / taskCtr)
+ Console.WriteLine()
+
+ ' Update overall totals.
+ SyncLock numericLock
+ totalCount += taskCtr
+ totalValue += taskTotal
+ End SyncLock
+ End Sub, token))
+ Next
+
+ Try
+ Task.WaitAll(tasks.ToArray())
+ Console.WriteLine()
+ Console.WriteLine("Total random numbers generated: {0:N0}", totalCount)
+ Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue)
+ Console.WriteLine("Random number mean: {0:N4}", totalValue / totalCount)
+ Catch e As AggregateException
+ For Each inner As Exception In e.InnerExceptions
+ Dim canc As TaskCanceledException = TryCast(inner, TaskCanceledException)
+ If canc IsNot Nothing Then
+ Console.WriteLine("Task #{0} cancelled.", canc.Task.Id)
+ Else
+ Console.WriteLine("Exception: {0}", inner.GetType().Name)
+ End If
+ Next
+ Finally
+ source.Dispose()
+ End Try
+ End Sub
+End Module
+' The example displays output like the following:
+' Task 1 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,502.47
+' Random number mean: 0.5003
+'
+' Task 0 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,445.63
+' Random number mean: 0.5002
+'
+' Task 2 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,556.04
+' Random number mean: 0.5003
+'
+' Task 3 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,178.87
+' Random number mean: 0.5001
+'
+' Task 4 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,819.17
+' Random number mean: 0.4999
+'
+' Task 5 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,190.58
+' Random number mean: 0.5001
+'
+' Task 6 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,720.21
+' Random number mean: 0.4999
+'
+' Task 7 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,000.96
+' Random number mean: 0.4995
+'
+' Task 8 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,499.33
+' Random number mean: 0.4997
+'
+' Task 9 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 1,000,193.25
+' Random number mean: 0.5001
+'
+' Task 10 finished execution.
+' Random numbers generated: 2,000,000
+' Sum of random numbers: 999,960.82
+' Random number mean: 0.5000
+'
+'
+' Total random numbers generated: 22,000,000
+' Total sum of all random numbers: 11,000,067.33
+' Random number mean: 0.5000
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/unique.vb b/snippets/visualbasic/System/Random/Overview/unique.vb
new file mode 100644
index 00000000000..102b020518c
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/unique.vb
@@ -0,0 +1,54 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Threading
+
+Module Example17
+ Public Sub Main()
+ Console.WriteLine("Instantiating two random number generators...")
+ Dim rnd1 As New Random()
+ Thread.Sleep(2000)
+ Dim rnd2 As New Random()
+ Console.WriteLine()
+
+ Console.WriteLine("The first random number generator:")
+ For ctr As Integer = 1 To 10
+ Console.WriteLine(" {0}", rnd1.Next())
+ Next
+ Console.WriteLine()
+
+ Console.WriteLine("The second random number generator:")
+ For ctr As Integer = 1 To 10
+ Console.WriteLine(" {0}", rnd2.Next())
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' Instantiating two random number generators...
+'
+' The first random number generator:
+' 643164361
+' 1606571630
+' 1725607587
+' 2138048432
+' 496874898
+' 1969147632
+' 2034533749
+' 1840964542
+' 412380298
+' 47518930
+'
+' The second random number generator:
+' 1251659083
+' 1514185439
+' 1465798544
+' 517841554
+' 1821920222
+' 195154223
+' 1538948391
+' 1548375095
+' 546062716
+' 897797880
+'
+
diff --git a/snippets/visualbasic/System/Random/Overview/uniquearray1.vb b/snippets/visualbasic/System/Random/Overview/uniquearray1.vb
new file mode 100644
index 00000000000..bc7c09df6df
--- /dev/null
+++ b/snippets/visualbasic/System/Random/Overview/uniquearray1.vb
@@ -0,0 +1,125 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+' A class that represents an individual card in a playing deck.
+Public Class Card
+ Public Suit As Suit
+ Public FaceValue As FaceValue
+
+ Public Overrides Function ToString() As String
+ Return String.Format("{0:F} of {1:F}", Me.FaceValue, Me.Suit)
+ End Function
+End Class
+
+Public Enum Suit As Integer
+ Hearts = 0
+ Diamonds = 1
+ Spades = 2
+ Clubs = 3
+End Enum
+
+Public Enum FaceValue As Integer
+ Ace = 1
+ Two = 2
+ Three = 3
+ Four = 4
+ Five = 5
+ Six = 6
+ Seven = 7
+ Eight = 8
+ Nine = 9
+ Ten = 10
+ Jack = 11
+ Queen = 12
+ King = 13
+End Enum
+
+Public Class Dealer
+ Dim rnd As Random
+ ' A deck of cards, without Jokers.
+ Dim deck(51) As Card
+ ' Parallel array for sorting cards.
+ Dim order(51) As Double
+ ' A pointer to the next card to deal.
+ Dim ptr As Integer = 0
+ ' A flag to indicate the deck is used.
+ Dim mustReshuffle As Boolean
+
+ Public Sub New()
+ rnd = New Random()
+ ' Initialize the deck.
+ Dim deckCtr As Integer = 0
+ For Each Suit In [Enum].GetValues(GetType(Suit))
+ For Each faceValue In [Enum].GetValues(GetType(FaceValue))
+ Dim card As New Card()
+ card.Suit = CType(Suit, Suit)
+ card.FaceValue = CType(faceValue, FaceValue)
+ deck(deckCtr) = card
+ deckCtr += 1
+ Next
+ Next
+ For ctr As Integer = 0 To order.Length - 1
+ order(ctr) = rnd.NextDouble()
+ Next
+ Array.Sort(order, deck)
+ End Sub
+
+ Public Function Deal(numberToDeal As Integer) As Card()
+ If mustReshuffle Then
+ Console.WriteLine("There are no cards left in the deck")
+ Return Nothing
+ End If
+
+ Dim cardsDealt(numberToDeal - 1) As Card
+ For ctr As Integer = 0 To numberToDeal - 1
+ cardsDealt(ctr) = deck(ptr)
+ ptr += 1
+ If ptr = deck.Length Then
+ mustReshuffle = True
+ End If
+ If mustReshuffle And ctr < numberToDeal - 1
+ Console.WriteLine("Can only deal the {0} cards remaining on the deck.",
+ ctr + 1)
+ Return cardsDealt
+ End If
+ Next
+ Return cardsDealt
+ End Function
+End Class
+
+Public Module UniqueArrayExample
+ Public Sub Main()
+ Dim dealer As New Dealer()
+ ShowCards(dealer.Deal(20))
+ End Sub
+
+ Private Sub ShowCards(cards() As Card)
+ For Each card In cards
+ If card IsNot Nothing Then _
+ Console.WriteLine("{0} of {1}", card.FaceValue, card.Suit)
+ Next
+ End Sub
+End Module
+' The example displays output like the following:
+' Six of Diamonds
+' King of Clubs
+' Eight of Clubs
+' Seven of Clubs
+' Queen of Clubs
+' King of Hearts
+' Three of Spades
+' Ace of Clubs
+' Four of Hearts
+' Three of Diamonds
+' Nine of Diamonds
+' Two of Hearts
+' Ace of Hearts
+' Three of Hearts
+' Four of Spades
+' Eight of Hearts
+' Queen of Diamonds
+' Two of Clubs
+' Four of Diamonds
+' Jack of Hearts
+'
diff --git a/snippets/visualbasic/System/Single/CompareTo/Project.vbproj b/snippets/visualbasic/System/Single/CompareTo/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Single/CompareTo/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Single/CompareTo/compareto2.vb b/snippets/visualbasic/System/Single/CompareTo/compareto2.vb
new file mode 100644
index 00000000000..11204a93390
--- /dev/null
+++ b/snippets/visualbasic/System/Single/CompareTo/compareto2.vb
@@ -0,0 +1,21 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example
+ Public Sub Main()
+ Dim value1 As Single = 16.5457
+ Dim value2 As Single = value1 * CSng(3.8899982) / CSng(3.8899982)
+ Console.WriteLine("Comparing {0} and {1}: {2}",
+ value1, value2, value1.CompareTo(value2))
+ Console.WriteLine()
+ Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
+ value1, value2, value1.CompareTo(value2))
+ End Sub
+End Module
+' The example displays the following output:
+' Comparing 16.5457 and 16.5457: -1
+'
+' Comparing 16.5457 and 16.545702: -1
+'
+
diff --git a/snippets/visualbasic/System/Single/CompareTo/compareto3.vb b/snippets/visualbasic/System/Single/CompareTo/compareto3.vb
new file mode 100644
index 00000000000..1822759f127
--- /dev/null
+++ b/snippets/visualbasic/System/Single/CompareTo/compareto3.vb
@@ -0,0 +1,21 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example2
+ Public Sub Main()
+ Dim value1 As Single = 16.5457
+ Dim value2 As Object = value1 * CSng(3.8899982) / CSng(3.8899982)
+ Console.WriteLine("Comparing {0} and {1}: {2}",
+ value1, value2, value1.CompareTo(value2))
+ Console.WriteLine()
+ Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
+ value1, value2, value1.CompareTo(value2))
+ End Sub
+End Module
+' The example displays the following output:
+' Comparing 16.5457 and 16.5457: -1
+'
+' Comparing 16.5457 and 16.545702: -1
+'
+
diff --git a/snippets/visualbasic/System/Single/Epsilon/Project.vbproj b/snippets/visualbasic/System/Single/Epsilon/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Epsilon/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Single/Epsilon/SingleEquals_25051.vb b/snippets/visualbasic/System/Single/Epsilon/SingleEquals_25051.vb
new file mode 100644
index 00000000000..257091aabb5
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Epsilon/SingleEquals_25051.vb
@@ -0,0 +1,71 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module modMain
+ Public Sub Main()
+ CompareUsingEquals()
+ Console.WriteLine()
+ CompareApproximateValues()
+ Console.WriteLine()
+ CompareObjectsUsingEquals()
+ Console.WriteLine()
+ CompareApproximateObjectValues()
+ End Sub
+
+ Private Sub CompareUsingEquals()
+ '
+ ' Initialize two singles with apparently identical values
+ Dim single1 As Single = .33333
+ Dim single2 As Single = 1/3
+ ' Compare them for equality
+ Console.WriteLine(single1.Equals(single2)) ' displays False
+ '
+ End Sub
+
+ Private Sub CompareApproximateValues()
+ '
+ ' Initialize two singles with apparently identical values
+ Dim single1 As Single = .33333
+ Dim single2 As Single = 1/3
+ ' Define the tolerance for variation in their values
+ Dim difference As Single = Math.Abs(single1 * .0001f)
+
+ ' Compare the values
+ ' The output to the console indicates that the two values are equal
+ If Math.Abs(single1 - single2) <= difference Then
+ Console.WriteLine("single1 and single2 are equal.")
+ Else
+ Console.WriteLine("single1 and single2 are unequal.")
+ End If
+ '
+ End Sub
+
+ Private Sub CompareObjectsUsingEquals()
+ '
+ ' Initialize two singles with apparently identical values
+ Dim single1 As Single = .33333
+ Dim single2 As Object = 1/3
+ ' Compare them for equality
+ Console.WriteLine(single1.Equals(single2)) ' displays False
+ '
+ End Sub
+
+ Private Sub CompareApproximateObjectValues()
+ '
+ ' Initialize two singles with apparently identical values
+ Dim single1 As Single = .33333
+ Dim single2 As Object = 1/3
+ ' Define the tolerance for variation in their values
+ Dim difference As Single = Math.Abs(single1 * .0001f)
+
+ ' Compare the values
+ ' The output to the console indicates that the two values are equal
+ If Math.Abs(single1 - CSng(single2)) <= difference Then
+ Console.WriteLine("single1 and single2 are equal.")
+ Else
+ Console.WriteLine("single1 and single2 are unequal.")
+ End If
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/Single/Epsilon/epsilon.vb b/snippets/visualbasic/System/Single/Epsilon/epsilon.vb
new file mode 100644
index 00000000000..09b88e9a915
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Epsilon/epsilon.vb
@@ -0,0 +1,24 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim values() As Single = {0, Single.Epsilon, Single.Epsilon * 0.5}
+
+ For ctr As Integer = 0 To values.Length - 2
+ For ctr2 As Integer = ctr + 1 To values.Length - 1
+ Console.WriteLine("{0:r} = {1:r}: {2}",
+ values(ctr), values(ctr2),
+ values(ctr).Equals(values(ctr2)))
+ Next
+ Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' 0 = 1.401298E-45: False
+' 0 = 0: True
+'
+' 1.401298E-45 = 0: False
+'
diff --git a/snippets/visualbasic/System/Single/Epsilon/epsilon1.vb b/snippets/visualbasic/System/Single/Epsilon/epsilon1.vb
new file mode 100644
index 00000000000..f002bf8ccef
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Epsilon/epsilon1.vb
@@ -0,0 +1,54 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example2
+ Public Sub Main()
+ Dim values() As Single = {0.0, Single.Epsilon}
+ For Each value In values
+ Console.WriteLine(GetComponentParts(value))
+ Console.WriteLine()
+ Next
+ End Sub
+
+ Private Function GetComponentParts(value As Single) As String
+ Dim result As String = String.Format("{0:R}: ", value)
+ Dim indent As Integer = result.Length
+
+ ' Convert the single to an 8-byte array.
+ Dim bytes() As Byte = BitConverter.GetBytes(value)
+ Dim formattedSingle As Integer = BitConverter.ToInt32(bytes, 0)
+
+ ' Get the sign bit (byte 3, bit 7).
+ result += String.Format("Sign: {0}{1}",
+ If(formattedSingle >> 31 <> 0, "1 (-)", "0 (+)"),
+ vbCrLf)
+
+ ' Get the exponent (byte 2 bit 7 to byte 3, bits 6)
+ Dim exponent As Integer = (formattedSingle >> 23) And &HFF
+ Dim adjustment As Integer = If(exponent <> 0, 127, 126)
+ result += String.Format("{0}Exponent: 0x{1:X4} ({1}){2}",
+ New String(" "c, indent), exponent - adjustment,
+ vbCrLf)
+
+ ' Get the significand (bits 0-22)
+ Dim significand As Long = If(exponent <> 0,
+ (formattedSingle And &H7FFFFF) Or &H800000,
+ formattedSingle And &H7FFFFF)
+ result += String.Format("{0}Mantissa: 0x{1:X13}{2}",
+ New String(" "c, indent), significand, vbCrLf)
+
+ Return result
+ End Function
+End Module
+' The example displays the following output:
+' 0: Sign: 0 (+)
+' Exponent: 0xFFFFFF82 (-126)
+' Mantissa: 0x0000000000000
+'
+'
+' 1.401298E-45: Sign: 0 (+)
+' Exponent: 0xFFFFFF82 (-126)
+' Mantissa: 0x0000000000001
+'
+
diff --git a/snippets/visualbasic/System/Single/Equals/Project.vbproj b/snippets/visualbasic/System/Single/Equals/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Equals/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Single/Equals/equalsabs1.vb b/snippets/visualbasic/System/Single/Equals/equalsabs1.vb
new file mode 100644
index 00000000000..cbab954266a
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Equals/equalsabs1.vb
@@ -0,0 +1,44 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim value1 As Single = .1 * 10
+ Dim value2 As Single = 0
+ For ctr As Integer = 0 To 9
+ value2 += CSng(.1)
+ Next
+
+ Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2,
+ HasMinimalDifference(value1, value2, 1))
+ End Sub
+
+ Public Function HasMinimalDifference(value1 As Single, value2 As Single, units As Integer) As Boolean
+ Dim bytes() As Byte = BitConverter.GetBytes(value1)
+ Dim iValue1 As Integer = BitConverter.ToInt32(bytes, 0)
+
+ bytes = BitConverter.GetBytes(value2)
+ Dim iValue2 As Integer = BitConverter.ToInt32(bytes, 0)
+
+ ' If the signs are different, Return False except for +0 and -0.
+ If ((iValue1 >> 31) <> (iValue2 >> 31)) Then
+ If value1 = value2 Then
+ Return True
+ End If
+ Return False
+ End If
+
+ Dim diff As Integer = Math.Abs(iValue1 - iValue2)
+
+ If diff <= units Then
+ Return True
+ End If
+
+ Return False
+ End Function
+End Module
+' The example displays the following output:
+' 1 = 1.00000012: True
+'
+
diff --git a/snippets/visualbasic/System/Single/Equals/equalsoverl.vb b/snippets/visualbasic/System/Single/Equals/equalsoverl.vb
index aa588582fa9..da4a9f69d90 100644
--- a/snippets/visualbasic/System/Single/Equals/equalsoverl.vb
+++ b/snippets/visualbasic/System/Single/Equals/equalsoverl.vb
@@ -2,14 +2,14 @@
Option Strict On
'
-Module Example
+Module Example2
Dim value As Single = 112
-
+
Public Sub Main()
Dim byte1 As Byte = 112
Console.WriteLine("value = byte1: {0,16}", value.Equals(byte1))
TestObjectForEquality(byte1)
-
+
Dim short1 As Short = 112
Console.WriteLine("value = short1: {0,16}", value.Equals(short1))
TestObjectForEquality(short1)
@@ -25,7 +25,7 @@ Module Example
Dim sbyte1 As SByte = 112
Console.WriteLine("value = sbyte1: {0,16}", value.Equals(sbyte1))
TestObjectForEquality(sbyte1)
-
+
Dim ushort1 As UShort = 112
Console.WriteLine("value = ushort1: {0,16}", value.Equals(ushort1))
TestObjectForEquality(ushort1)
@@ -37,7 +37,7 @@ Module Example
Dim ulong1 As ULong = 112
Console.WriteLine("value = ulong1: {0,17}", value.Equals(ulong1))
TestObjectForEquality(ulong1)
-
+
Dim dec1 As Decimal = 112d
Console.WriteLine("value = dec1: {0,20}", value.Equals(dec1))
TestObjectForEquality(dec1)
@@ -46,7 +46,7 @@ Module Example
Console.WriteLine("value = dbl1: {0,20}", value.Equals(dbl1))
TestObjectForEquality(dbl1)
End Sub
-
+
Private Sub TestObjectForEquality(obj As Object)
Console.WriteLine("{0} ({1}) = {2} ({3}): {4}",
value, value.GetType().Name,
diff --git a/snippets/visualbasic/System/Single/Overview/PrecisionList4a.vb b/snippets/visualbasic/System/Single/Overview/PrecisionList4a.vb
new file mode 100644
index 00000000000..7e9692ec3f7
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/PrecisionList4a.vb
@@ -0,0 +1,37 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Imports System.IO
+
+Module Example11
+ Public Sub Main()
+ '
+ Dim sw As New StreamWriter(".\Singles.dat")
+ Dim values() As Single = {3.2 / 1.11, 1.0 / 3, CSng(Math.PI)}
+ For ctr As Integer = 0 To values.Length - 1
+ sw.Write(values(ctr).ToString())
+ If ctr <> values.Length - 1 Then sw.Write("|")
+ Next
+ sw.Close()
+
+ Dim restoredValues(values.Length - 1) As Single
+ Dim sr As New StreamReader(".\Singles.dat")
+ Dim temp As String = sr.ReadToEnd()
+ Dim tempStrings() As String = temp.Split("|"c)
+ For ctr As Integer = 0 To tempStrings.Length - 1
+ restoredValues(ctr) = Single.Parse(tempStrings(ctr))
+ Next
+
+ For ctr As Integer = 0 To values.Length - 1
+ Console.WriteLine("{0} {2} {1}", values(ctr),
+ restoredValues(ctr),
+ If(values(ctr).Equals(restoredValues(ctr)), "=", "<>"))
+ Next
+
+ ' The example displays the following output on .NET Framework:
+ ' 2.882883 <> 2.882883
+ ' 0.3333333 <> 0.3333333
+ ' 3.141593 <> 3.141593
+ '
+ End Sub
+End Module
diff --git a/snippets/visualbasic/System/Single/Overview/Project.vbproj b/snippets/visualbasic/System/Single/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/Single/Overview/comparison1.vb b/snippets/visualbasic/System/Single/Overview/comparison1.vb
new file mode 100644
index 00000000000..dca470b4395
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/comparison1.vb
@@ -0,0 +1,15 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim value1 As Single = 0.3333333
+ Dim value2 As Single = 1 / 3
+ Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
+ End Sub
+End Module
+' The example displays the following output:
+' 0.3333333 = 0.333333343: False
+'
+
diff --git a/snippets/visualbasic/System/Single/Overview/comparison2.vb b/snippets/visualbasic/System/Single/Overview/comparison2.vb
new file mode 100644
index 00000000000..d56467bdaa7
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/comparison2.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example2
+ Public Sub Main()
+ '
+ Dim value1 As Single = 10.201438
+ value1 = CSng(Math.Sqrt(CSng(Math.Pow(value1, 2))))
+ Dim value2 As Single = CSng(Math.Pow(value1 * CSng(3.51), 2))
+ value2 = CSng(Math.Sqrt(value2) / CSng(3.51))
+ Console.WriteLine("{0} = {1}: {2}",
+ value1, value2, value1.Equals(value2))
+
+ ' The example displays the following output on .NET:
+ ' 10.201438 = 10.201439: False
+ ' The example displays the following output on .NET Framework:
+ ' 10.20144 = 10.20144: False
+ '
+ End Sub
+End Module
diff --git a/snippets/visualbasic/System/Single/Overview/comparison3.vb b/snippets/visualbasic/System/Single/Overview/comparison3.vb
new file mode 100644
index 00000000000..db9d08a7e8b
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/comparison3.vb
@@ -0,0 +1,18 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example3
+ Public Sub Main()
+ Dim value1 As Single = 0.3333333
+ Dim value2 As Single = 1 / 3
+ Dim precision As Integer = 7
+ value1 = CSng(Math.Round(value1, precision))
+ value2 = CSng(Math.Round(value2, precision))
+ Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
+ End Sub
+End Module
+' The example displays the following output:
+' 0.3333333 = 0.3333333: True
+'
+
diff --git a/snippets/visualbasic/System/Single/Overview/comparison4.vb b/snippets/visualbasic/System/Single/Overview/comparison4.vb
new file mode 100644
index 00000000000..f05888920ea
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/comparison4.vb
@@ -0,0 +1,43 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example4
+ '
+ Public Sub Main()
+ Dim one1 As Single = 0.1 * 10
+ Dim one2 As Single = 0
+ For ctr As Integer = 1 To 10
+ one2 += CSng(0.1)
+ Next
+ Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2))
+ Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}",
+ one1, one2,
+ IsApproximatelyEqual(one1, one2, 0.000001))
+ End Sub
+
+ Function IsApproximatelyEqual(value1 As Single, value2 As Single,
+ epsilon As Single) As Boolean
+ ' If they are equal anyway, just return True.
+ If value1.Equals(value2) Then Return True
+
+ ' Handle NaN, Infinity.
+ If Single.IsInfinity(value1) Or Single.IsNaN(value1) Then
+ Return value1.Equals(value2)
+ ElseIf Single.IsInfinity(value2) Or Single.IsNaN(value2) Then
+ Return value1.Equals(value2)
+ End If
+
+ ' Handle zero to avoid division by zero.
+ Dim divisor As Single = Math.Max(value1, value2)
+ If divisor.Equals(0) Then
+ divisor = Math.Min(value1, value2)
+ End If
+
+ Return Math.Abs(value1 - value2) / divisor <= epsilon
+ End Function
+
+ ' The example displays the following output:
+ ' 1 = 1.0000001: False
+ ' 1 is approximately equal to 1.0000001: True
+ '
+End Module
diff --git a/snippets/visualbasic/System/Single/Overview/convert1.vb b/snippets/visualbasic/System/Single/Overview/convert1.vb
new file mode 100644
index 00000000000..9b4c7fffb53
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/convert1.vb
@@ -0,0 +1,49 @@
+' Visual Basic .NET Document
+'Option Strict On
+
+'
+Module Example5
+ Public Sub Main()
+ Dim values() As Object = {Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
+ Decimal.MaxValue, Double.MinValue, Double.MaxValue,
+ Int16.MinValue, Int16.MaxValue, Int32.MinValue,
+ Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
+ SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
+ UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
+ UInt64.MinValue, UInt64.MaxValue}
+ Dim sngValue As Single
+ For Each value In values
+ If value.GetType() = GetType(Double) Then
+ sngValue = CSng(value)
+ Else
+ sngValue = value
+ End If
+ Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
+ value, value.GetType().Name,
+ sngValue, sngValue.GetType().Name)
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' 0 (Byte) --> 0 (Single)
+' 255 (Byte) --> 255 (Single)
+' -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single)
+' 79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single)
+' -1.79769313486232E+308 (Double) --> -Infinity (Single)
+' 1.79769313486232E+308 (Double) --> Infinity (Single)
+' -32768 (Int16) --> -32768 (Single)
+' 32767 (Int16) --> 32767 (Single)
+' -2147483648 (Int32) --> -2.14748365E+09 (Single)
+' 2147483647 (Int32) --> 2.14748365E+09 (Single)
+' -9223372036854775808 (Int64) --> -9.223372E+18 (Single)
+' 9223372036854775807 (Int64) --> 9.223372E+18 (Single)
+' -128 (SByte) --> -128 (Single)
+' 127 (SByte) --> 127 (Single)
+' 0 (UInt16) --> 0 (Single)
+' 65535 (UInt16) --> 65535 (Single)
+' 0 (UInt32) --> 0 (Single)
+' 4294967295 (UInt32) --> 4.2949673E+09 (Single)
+' 0 (UInt64) --> 0 (Single)
+' 18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)
+'
+
diff --git a/snippets/visualbasic/System/Single/Overview/convert2.vb b/snippets/visualbasic/System/Single/Overview/convert2.vb
new file mode 100644
index 00000000000..c2192caf9cd
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/convert2.vb
@@ -0,0 +1,92 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example6
+ Public Sub Main()
+ Dim values() As Single = {Single.MinValue, -67890.1234, -12345.6789,
+ 12345.6789, 67890.1234, Single.MaxValue,
+ Single.NaN, Single.PositiveInfinity,
+ Single.NegativeInfinity}
+ For Each value In values
+ Try
+ Dim lValue As Long = CLng(value)
+ Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
+ value, value.GetType().Name,
+ lValue, lValue.GetType().Name)
+ Catch e As OverflowException
+ Console.WriteLine("Unable to convert {0} to Int64.", value)
+ End Try
+ Try
+ Dim ulValue As UInt64 = CULng(value)
+ Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
+ value, value.GetType().Name,
+ ulValue, ulValue.GetType().Name)
+ Catch e As OverflowException
+ Console.WriteLine("Unable to convert {0} to UInt64.", value)
+ End Try
+ Try
+ Dim dValue As Decimal = CDec(value)
+ Console.WriteLine("{0} ({1}) --> {2} ({3})",
+ value, value.GetType().Name,
+ dValue, dValue.GetType().Name)
+ Catch e As OverflowException
+ Console.WriteLine("Unable to convert {0} to Decimal.", value)
+ End Try
+
+ Dim dblValue As Double = value
+ Console.WriteLine("{0} ({1}) --> {2} ({3})",
+ value, value.GetType().Name,
+ dblValue, dblValue.GetType().Name)
+ Console.WriteLine()
+ Next
+ End Sub
+End Module
+
+' The example displays the following output for conversions performed
+' in a checked context:
+' Unable to convert -3.402823E+38 to Int64.
+' Unable to convert -3.402823E+38 to UInt64.
+' Unable to convert -3.402823E+38 to Decimal.
+' -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
+'
+' -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
+' Unable to convert -67890.13 to UInt64.
+' -67890.13 (Single) --> -67890.12 (Decimal)
+' -67890.13 (Single) --> -67890.125 (Double)
+'
+' -12345.68 (Single) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
+' Unable to convert -12345.68 to UInt64.
+' -12345.68 (Single) --> -12345.68 (Decimal)
+' -12345.68 (Single) --> -12345.6787109375 (Double)
+'
+' 12345.68 (Single) --> 12346 (0x000000000000303A) (Int64)
+' 12345.68 (Single) --> 12346 (0x000000000000303A) (UInt64)
+' 12345.68 (Single) --> 12345.68 (Decimal)
+' 12345.68 (Single) --> 12345.6787109375 (Double)
+'
+' 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
+' 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
+' 67890.13 (Single) --> 67890.12 (Decimal)
+' 67890.13 (Single) --> 67890.125 (Double)
+'
+' Unable to convert 3.402823E+38 to Int64.
+' Unable to convert 3.402823E+38 to UInt64.
+' Unable to convert 3.402823E+38 to Decimal.
+' 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
+'
+' Unable to convert NaN to Int64.
+' Unable to convert NaN to UInt64.
+' Unable to convert NaN to Decimal.
+' NaN (Single) --> NaN (Double)
+'
+' Unable to convert ∞ to Int64.
+' Unable to convert ∞ to UInt64.
+' Unable to convert ∞ to Decimal.
+' ∞ (Single) --> ∞ (Double)
+'
+' Unable to convert -∞ to Int64.
+' Unable to convert -∞ to UInt64.
+' Unable to convert -∞ to Decimal.
+' -∞ (Single) --> -∞ (Double)
+'
diff --git a/snippets/visualbasic/System/Single/Overview/exceptional1.vb b/snippets/visualbasic/System/Single/Overview/exceptional1.vb
new file mode 100644
index 00000000000..330616d78ba
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/exceptional1.vb
@@ -0,0 +1,17 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example7
+ Public Sub Main()
+ Dim value1 As Single = 1.163287E-36
+ Dim value2 As Single = 9.164234E-25
+ Dim result As Single = value1 * value2
+ Console.WriteLine("{0} * {1} = {2:R}", value1, value2, result)
+ Console.WriteLine("{0} = 0: {1}", result, result.Equals(0))
+ End Sub
+End Module
+' The example displays the following output:
+' 1.163287E-36 * 9.164234E-25 = 0
+' 0 = 0: True
+'
diff --git a/snippets/visualbasic/System/Single/Overview/exceptional2.vb b/snippets/visualbasic/System/Single/Overview/exceptional2.vb
new file mode 100644
index 00000000000..1ef9a90b7bb
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/exceptional2.vb
@@ -0,0 +1,29 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example8
+ Public Sub Main()
+ Dim value1 As Single = 3.065E+35
+ Dim value2 As Single = 6.9375E+32
+ Dim result As Single = value1 * value2
+ Console.WriteLine("PositiveInfinity: {0}",
+ Single.IsPositiveInfinity(result))
+ Console.WriteLine("NegativeInfinity: {0}",
+ Single.IsNegativeInfinity(result))
+ Console.WriteLine()
+ value1 = -value1
+ result = value1 * value2
+ Console.WriteLine("PositiveInfinity: {0}",
+ Single.IsPositiveInfinity(result))
+ Console.WriteLine("NegativeInfinity: {0}",
+ Single.IsNegativeInfinity(result))
+ End Sub
+End Module
+' The example displays the following output:
+' PositiveInfinity: True
+' NegativeInfinity: False
+'
+' PositiveInfinity: False
+' NegativeInfinity: True
+'
diff --git a/snippets/visualbasic/System/Single/Overview/precisionlist1.vb b/snippets/visualbasic/System/Single/Overview/precisionlist1.vb
new file mode 100644
index 00000000000..a5812429656
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/precisionlist1.vb
@@ -0,0 +1,13 @@
+Module Example10
+ Public Sub Main()
+ '
+ Dim value1 As Double = 1 / 3
+ Dim sValue2 As Single = 1 / 3
+ Dim value2 As Double = CDbl(sValue2)
+ Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
+
+ ' The example displays the following output:
+ ' 0.3333333333333333 = 0.3333333432674408: False
+ '
+ End Sub
+End Module
diff --git a/snippets/visualbasic/System/Single/Overview/precisionlist3.vb b/snippets/visualbasic/System/Single/Overview/precisionlist3.vb
new file mode 100644
index 00000000000..c859ea1c29a
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/precisionlist3.vb
@@ -0,0 +1,25 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module PrecisionList3
+ Public Sub Main()
+ '
+ Dim values() As Single = {10.01, 2.88, 2.88, 2.88, 9.0}
+ Dim result As Single = 27.65
+ Dim total As Single
+ For Each value In values
+ total += value
+ Next
+ If total.Equals(result) Then
+ Console.WriteLine("The sum of the values equals the total.")
+ Else
+ Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result}).")
+ End If
+ End Sub
+
+ ' The example displays the following output on .NET:
+ ' The sum of the values (27.650002) does not equal the total (27.65).
+ ' The example displays the following output on .NET Framework:
+ ' The sum of the values (27.65) does not equal the total (27.65).
+ '
+End Module
diff --git a/snippets/visualbasic/System/Single/Overview/representation1.vb b/snippets/visualbasic/System/Single/Overview/representation1.vb
new file mode 100644
index 00000000000..fe77569fa9a
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/representation1.vb
@@ -0,0 +1,22 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example13
+ Public Sub Main()
+ Dim value As Single = 0.2
+ Dim result1 As Single = value * 10
+ Dim result2 As Single
+ For ctr As Integer = 1 To 10
+ result2 += value
+ Next
+ Console.WriteLine(".2 * 10: {0:R}", result1)
+ Console.WriteLine(".2 Added 10 times: {0:R}", result2)
+ End Sub
+End Module
+
+' The example displays the following output:
+' .2 * 10: 2
+' .2 Added 10 times: 2.0000002
+'
+
diff --git a/snippets/visualbasic/System/Single/Overview/representation2.vb b/snippets/visualbasic/System/Single/Overview/representation2.vb
new file mode 100644
index 00000000000..3b44c2f25c6
--- /dev/null
+++ b/snippets/visualbasic/System/Single/Overview/representation2.vb
@@ -0,0 +1,15 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example
+ Public Sub Main()
+ Dim value As Single = 123.456
+ Dim additional As Single = Single.Epsilon * 1e15
+ Console.WriteLine($"{value} + {additional} = {value + additional}")
+ End Sub
+End Module
+' The example displays the following output:
+' 123.456 + 1.401298E-30 = 123.456
+'
+
diff --git a/snippets/visualbasic/System/String/.ctor/Project.vbproj b/snippets/visualbasic/System/String/.ctor/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/String/.ctor/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/String/.ctor/ctor1.vb b/snippets/visualbasic/System/String/.ctor/ctor1.vb
new file mode 100644
index 00000000000..07eb0b334d4
--- /dev/null
+++ b/snippets/visualbasic/System/String/.ctor/ctor1.vb
@@ -0,0 +1,16 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example
+ Public Sub Main()
+ Dim value1 As String = "This is a string."
+ Dim value2 As String = value1
+ Console.WriteLine(value1)
+ Console.WriteLine(value2)
+ End Sub
+End Module
+' The example displays the following output:
+' This is a string.
+' This is a string.
+'
diff --git a/snippets/visualbasic/System/String/.ctor/source.vb b/snippets/visualbasic/System/String/.ctor/source.vb
new file mode 100644
index 00000000000..822f09067ac
--- /dev/null
+++ b/snippets/visualbasic/System/String/.ctor/source.vb
@@ -0,0 +1,47 @@
+Option Strict On
+
+Imports System.Text
+
+Class ConsoleApp
+ Public Shared Sub Main()
+ '
+ ' Unicode Mathematical operators
+ Dim charArr1() As Char = {ChrW(&H2200), ChrW(&H2202), _
+ ChrW(&H200F), ChrW(&H2205)}
+ Dim szMathSymbols As New String(charArr1)
+
+ ' Unicode Letterlike Symbols
+ Dim charArr2() As Char = {ChrW(&H2111), ChrW(&H2118), _
+ ChrW(&H2122), ChrW(&H2126)}
+ Dim szLetterLike As New String(charArr2)
+
+ ' Compare Strings - the result is false
+ Console.WriteLine("The strings are equal? " & _
+ CStr(szMathSymbols.Equals(szLetterLike)))
+ '
+
+ '
+ ' Create a Unicode String with 5 Greek Alpha characters
+ Dim szGreekAlpha As New String(ChrW(&H0391), 5)
+ ' Create a Unicode String with a Greek Omega character
+ Dim szGreekOmega As New String(New Char() {ChrW(&H03A9), ChrW(&H03A9), _
+ ChrW(&H03A9)}, 2, 1)
+
+ Dim szGreekLetters As String = String.Concat(szGreekOmega, szGreekAlpha, _
+ szGreekOmega.Clone())
+
+ ' Examine the result
+ Console.WriteLine(szGreekLetters)
+
+ ' The first index of Alpha
+ Dim iAlpha As Integer = szGreekLetters.IndexOf(ChrW(&H0391))
+ ' The last index of Omega
+ Dim iomega As Integer = szGreekLetters.LastIndexOf(ChrW(&H03A9))
+
+ Console.WriteLine("The Greek letter Alpha first appears at index {0}.", _
+ ialpha)
+ Console.WriteLIne("The Greek letter Omega last appears at index {0}.", _
+ iomega)
+ '
+ End Sub
+End Class
diff --git a/snippets/visualbasic/System/String/Format/Example1.vb b/snippets/visualbasic/System/String/Format/Example1.vb
new file mode 100644
index 00000000000..25f6752711a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/Example1.vb
@@ -0,0 +1,24 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim values() As Short = {Int16.MinValue, -27, 0, 1042, Int16.MaxValue}
+ Console.WriteLine("{0,10} {1,10}", "Decimal", "Hex")
+ Console.WriteLine()
+ For Each value As Short In values
+ Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value)
+ Console.WriteLine(formatString)
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Decimal Hex
+'
+' -32768: 8000
+' -27: FFE5
+' 0: 0
+' 1042: 412
+' 32767: 7FFF
+'
diff --git a/snippets/visualbasic/System/String/Format/Example2.vb b/snippets/visualbasic/System/String/Format/Example2.vb
index 240bb492eea..6c7b7168da2 100644
--- a/snippets/visualbasic/System/String/Format/Example2.vb
+++ b/snippets/visualbasic/System/String/Format/Example2.vb
@@ -4,26 +4,26 @@ Option Strict On
'
Imports System.Globalization
-Module Example
+Module Example2
Public Sub Main()
Dim cultureNames() As String = { "en-US", "fr-FR", "de-DE", "es-ES" }
-
+
Dim dateToDisplay As Date = #9/1/2009 6:32PM#
Dim value As Double = 9164.32
Console.WriteLine("Culture Date Value")
- Console.WriteLine()
+ Console.WriteLine()
For Each cultureName As String In cultureNames
Dim culture As New CultureInfo(cultureName)
Dim output As String = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", _
culture.Name, dateToDisplay, value)
Console.WriteLine(output)
- Next
+ Next
End Sub
End Module
' The example displays the following output:
' Culture Date Value
-'
+'
' en-US Tuesday, September 01, 2009 9,164.32
' fr-FR mardi 1 septembre 2009 9 164,32
' de-DE Dienstag, 1. September 2009 9.164,32
diff --git a/snippets/visualbasic/System/String/Format/FormatExample2.vb b/snippets/visualbasic/System/String/Format/FormatExample2.vb
new file mode 100644
index 00000000000..26b3a3e6e19
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/FormatExample2.vb
@@ -0,0 +1,69 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module TestFormatter
+ Public Sub Main()
+ Dim acctNumber As Integer = 79203159
+ Console.WriteLine(String.Format(New CustomerFormatter, "{0}", acctNumber))
+ Console.WriteLine(String.Format(New CustomerFormatter, "{0:G}", acctNumber))
+ Console.WriteLine(String.Format(New CustomerFormatter, "{0:S}", acctNumber))
+ Console.WriteLine(String.Format(New CustomerFormatter, "{0:P}", acctNumber))
+ Try
+ Console.WriteLine(String.Format(New CustomerFormatter, "{0:X}", acctNumber))
+ Catch e As FormatException
+ Console.WriteLine(e.Message)
+ End Try
+ End Sub
+End Module
+
+Public Class CustomerFormatter : Implements IFormatProvider, ICustomFormatter
+ Public Function GetFormat(type As Type) As Object _
+ Implements IFormatProvider.GetFormat
+ If type Is GetType(ICustomFormatter) Then
+ Return Me
+ Else
+ Return Nothing
+ End If
+ End Function
+
+ Public Function Format(fmt As String, _
+ arg As Object, _
+ formatProvider As IFormatProvider) As String _
+ Implements ICustomFormatter.Format
+ If Not Me.Equals(formatProvider) Then
+ Return Nothing
+ Else
+ If String.IsNullOrEmpty(fmt) Then fmt = "G"
+
+ Dim customerString As String = arg.ToString()
+ if customerString.Length < 8 Then _
+ customerString = customerString.PadLeft(8, "0"c)
+
+ Select Case fmt
+ Case "G"
+ Return customerString.Substring(0, 1) & "-" & _
+ customerString.Substring(1, 5) & "-" & _
+ customerString.Substring(6)
+ Case "S"
+ Return customerString.Substring(0, 1) & "/" & _
+ customerString.Substring(1, 5) & "/" & _
+ customerString.Substring(6)
+ Case "P"
+ Return customerString.Substring(0, 1) & "." & _
+ customerString.Substring(1, 5) & "." & _
+ customerString.Substring(6)
+ Case Else
+ Throw New FormatException( _
+ String.Format("The '{0}' format specifier is not supported.", fmt))
+ End Select
+ End If
+ End Function
+End Class
+' The example displays the following output:
+' 7-92031-59
+' 7-92031-59
+' 7/92031/59
+' 7.92031.59
+' The 'X' format specifier is not supported.
+'
diff --git a/snippets/visualbasic/System/String/Format/Project.vbproj b/snippets/visualbasic/System/String/Format/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/String/Format/formatoverload1.vb b/snippets/visualbasic/System/String/Format/formatoverload1.vb
new file mode 100644
index 00000000000..5efa59a782a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/formatoverload1.vb
@@ -0,0 +1,18 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example3
+ Public Sub Main()
+ '
+ Dim dat As Date = #1/17/2012 9:30AM#
+ Dim city As String = "Chicago"
+ Dim temp As Integer = -16
+ Dim output As String = String.Format("At {0} in {1}, the temperature was {2} degrees.",
+ dat, city, temp)
+ Console.WriteLine(output)
+ ' The example displays the following output:
+ ' At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Format/formatoverload2.vb b/snippets/visualbasic/System/String/Format/formatoverload2.vb
new file mode 100644
index 00000000000..d1067b3b567
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/formatoverload2.vb
@@ -0,0 +1,35 @@
+' Visual Basic .NET Document
+Option Strict On
+Option Infer On
+
+'
+Module Example4
+ Public Sub Main()
+ ' Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
+ Dim cities() =
+ {Tuple.Create("Los Angeles", #1/1/1940#, 1504277, #1/1/1950#, 1970358),
+ Tuple.Create("New York", #1/1/1940#, 7454995, #1/1/1950#, 7891957),
+ Tuple.Create("Chicago", #1/1/1940#, 3396808, #1/1/1950#, 3620962),
+ Tuple.Create("Detroit", #1/1/1940#, 1623452, #1/1/1950#, 1849568)}
+
+ ' Display header
+ Dim header As String = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}",
+ "City", "Year", "Population", "Change (%)")
+ Console.WriteLine(header)
+ Console.WriteLine()
+ For Each city In cities
+ Dim output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
+ city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
+ (city.Item5 - city.Item3) / city.Item3)
+ Console.WriteLine(output)
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' City Year Population Year Population Change (%)
+'
+' Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
+' New York 1940 7,454,995 1950 7,891,957 5.9 %
+' Chicago 1940 3,396,808 1950 3,620,962 6.6 %
+' Detroit 1940 1,623,452 1950 1,849,568 13.9 %
+'
diff --git a/snippets/visualbasic/System/String/Format/formatsyntax1.vb b/snippets/visualbasic/System/String/Format/formatsyntax1.vb
new file mode 100644
index 00000000000..701f842ff8c
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/formatsyntax1.vb
@@ -0,0 +1,12 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example5
+ Public Sub Main()
+ 'Dim value As String =
+ '
+ String.Format("{0,-10:C}", 126347.89D)
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Format/interceptor2.vb b/snippets/visualbasic/System/String/Format/interceptor2.vb
new file mode 100644
index 00000000000..a52aaf9e61f
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/interceptor2.vb
@@ -0,0 +1,120 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+
+Public Class InterceptProvider : Implements IFormatProvider, ICustomFormatter
+ Public Function GetFormat(formatType As Type) As Object _
+ Implements IFormatProvider.GetFormat
+ If formatType Is GetType(ICustomFormatter) Then
+ Return Me
+ Else
+ Return Nothing
+ End If
+ End Function
+
+ Public Function Format(fmt As String, obj As Object, provider As IFormatProvider) As String _
+ Implements ICustomFormatter.Format
+
+ Dim formatString As String = If(fmt IsNot Nothing, fmt, "")
+ Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
+ provider, If(obj IsNot Nothing, obj, ""), formatString)
+
+ If obj Is Nothing Then Return String.Empty
+
+ ' If this is a byte and the "R" format string, format it with Roman numerals.
+ If TypeOf(obj) Is Byte AndAlso formatString.ToUpper.Equals("R") Then
+ Dim value As Byte = CByte(obj)
+ Dim remainder As Integer
+ Dim result As Integer
+ Dim returnString As String = String.Empty
+
+ ' Get the hundreds digit(s)
+ result = Math.DivRem(value, 100, remainder)
+ If result > 0 Then returnString = New String("C"c, result)
+ value = CByte(remainder)
+ ' Get the 50s digit
+ result = Math.DivRem(value, 50, remainder)
+ If result = 1 Then returnString += "L"
+ value = CByte(remainder)
+ ' Get the tens digit.
+ result = Math.DivRem(value, 10, remainder)
+ If result > 0 Then returnString += New String("X"c, result)
+ value = CByte(remainder)
+ ' Get the fives digit.
+ result = Math.DivRem(value, 5, remainder)
+ If result > 0 Then returnString += "V"
+ value = CByte(remainder)
+ ' Add the ones digit.
+ If remainder > 0 Then returnString += New String("I"c, remainder)
+
+ ' Check whether we have too many X characters.
+ Dim pos As Integer = returnString.IndexOf("XXXX")
+ If pos >= 0 Then
+ Dim xPos As Integer = returnString.IndexOf("L")
+ If xPos >= 0 And xPos = pos - 1 Then
+ returnString = returnString.Replace("LXXXX", "XC")
+ Else
+ returnString = returnString.Replace("XXXX", "XL")
+ End If
+ End If
+ ' Check whether we have too many I characters
+ pos = returnString.IndexOf("IIII")
+ If pos >= 0 Then
+ If returnString.IndexOf("V") >= 0 Then
+ returnString = returnString.Replace("VIIII", "IX")
+ Else
+ returnString = returnString.Replace("IIII", "IV")
+ End If
+ End If
+ Return returnString
+ End If
+
+ ' Use default for all other formatting.
+ If TypeOf obj Is IFormattable Then
+ Return CType(obj, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
+ Else
+ Return obj.ToString()
+ End If
+ End Function
+End Class
+
+Module Example
+ Public Sub Main()
+ Dim n As Integer = 10
+ Dim value As Double = 16.935
+ Dim day As DateTime = Date.Now
+ Dim provider As New InterceptProvider()
+ Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}", n, value, day))
+ Console.WriteLine()
+ Console.WriteLine(String.Format(provider, "{0}: {1:F}", "Today",
+ CType(Date.Now.DayOfWeek, DayOfWeek)))
+ Console.WriteLine()
+ Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
+ CByte(2), CByte(12), CByte(199)))
+ Console.WriteLine()
+ Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}",
+ CByte(2), CByte(12), CByte(199)))
+ End Sub
+End Module
+' The example displays the following output:
+' Provider: InterceptProvider, Object: 10, Format String: N0
+' Provider: InterceptProvider, Object: 16.935, Format String: C2
+' Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
+' 10: $16.94 on 1/31/2013
+'
+' Provider: InterceptProvider, Object: Today: , Format String:
+' Provider: InterceptProvider, Object: Thursday, Format String: F
+' Today: : Thursday
+'
+' Provider: InterceptProvider, Object: 2, Format String: X
+' Provider: InterceptProvider, Object: 12, Format String:
+' Provider: InterceptProvider, Object: 199, Format String:
+' 2, 12, 199
+'
+' Provider: InterceptProvider, Object: 2, Format String: R
+' Provider: InterceptProvider, Object: 12, Format String: R
+' Provider: InterceptProvider, Object: 199, Format String: R
+' II, XII, CXCIX
+'
diff --git a/snippets/visualbasic/System/String/Format/qa-interpolated1.vb b/snippets/visualbasic/System/String/Format/qa-interpolated1.vb
new file mode 100644
index 00000000000..7444b0bf30a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa-interpolated1.vb
@@ -0,0 +1,20 @@
+
+Module Example12
+ Public Sub Main()
+ Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
+ Dim output = names(0) + ", " + names(1) + ", " + names(2) + ", " +
+ names(3) + ", " + names(4) + ", " + names(5) + ", " +
+ names(6)
+
+ output += vbCrLf
+ Dim dat = DateTime.Now
+ output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
+ dat, dat.DayOfWeek)
+ Console.WriteLine(output)
+ End Sub
+End Module
+' The example displays the following output:
+' Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+' It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+
+
diff --git a/snippets/visualbasic/System/String/Format/qa-interpolated2.vb b/snippets/visualbasic/System/String/Format/qa-interpolated2.vb
new file mode 100644
index 00000000000..9f4953e45f3
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa-interpolated2.vb
@@ -0,0 +1,17 @@
+
+Module Example13
+ Public Sub Main()
+ Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
+ Dim output = $"{names(0)}, {names(1)}, {names(2)}, {names(3)}, {names(4)}, " +
+ $"{names(5)}, {names(6)}"
+
+ Dim dat = DateTime.Now
+ output += $"{vbCrLf}It is {dat:t} on {dat:d}. The day of the week is {dat.DayOfWeek}."
+ Console.WriteLine(output)
+ End Sub
+End Module
+' The example displays the following output:
+' Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
+' It is 10:29 AM on 1/8/2018. The day of the week is Monday.
+
+
diff --git a/snippets/visualbasic/System/String/Format/qa1.vb b/snippets/visualbasic/System/String/Format/qa1.vb
new file mode 100644
index 00000000000..66e34573cf0
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa1.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+
+Module Example5a
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim numbers(3) As Integer
+ Dim total As Integer = 0
+ For ctr = 0 To 2
+ Dim number As Integer = rnd.Next(1001)
+ numbers(ctr) = number
+ total += number
+ Next
+ numbers(3) = total
+ Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
+ End Sub
+End Module
+'
diff --git a/snippets/visualbasic/System/String/Format/qa2.vb b/snippets/visualbasic/System/String/Format/qa2.vb
new file mode 100644
index 00000000000..969c9664337
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa2.vb
@@ -0,0 +1,25 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Collections.Generic
+
+Module Example6
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim numbers(3) As Integer
+ Dim total As Integer = 0
+ For ctr = 0 To 2
+ Dim number As Integer = rnd.Next(1001)
+ numbers(ctr) = number
+ total += number
+ Next
+ numbers(3) = total
+ Dim values(numbers.Length - 1) As Object
+ numbers.CopyTo(values, 0)
+ Console.WriteLine("{0} + {1} + {2} = {3}", values)
+ End Sub
+End Module
+'
+
+
diff --git a/snippets/visualbasic/System/String/Format/qa26.vb b/snippets/visualbasic/System/String/Format/qa26.vb
new file mode 100644
index 00000000000..eab0d501593
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa26.vb
@@ -0,0 +1,23 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example7
+ Public Sub Main()
+ Dim values() As Object = {1603, 1794.68235, 15436.14}
+ Dim result As String
+ For Each value In values
+ result = String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}",
+ value, CDbl(value) / 10000)
+ Console.WriteLine(result)
+ Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
+'
+' $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
+'
+' $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
+'
diff --git a/snippets/visualbasic/System/String/Format/qa27.vb b/snippets/visualbasic/System/String/Format/qa27.vb
new file mode 100644
index 00000000000..faf15d373a3
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa27.vb
@@ -0,0 +1,16 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example8
+ Public Sub Main()
+ Dim value As Decimal = 16309.5436D
+ Dim result As String = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
+ value)
+ Console.WriteLine(result)
+ End Sub
+End Module
+' The example displays the following output:
+' 16309.54360 16,309.54 16309.544
+'
+
diff --git a/snippets/visualbasic/System/String/Format/qa28.vb b/snippets/visualbasic/System/String/Format/qa28.vb
new file mode 100644
index 00000000000..9612caac6ff
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa28.vb
@@ -0,0 +1,16 @@
+'Visual Basic .NET Document
+
+Option Strict On
+
+'
+Module Example9
+ Public Sub Main()
+ Dim value As Integer = 16342
+ Dim result As String = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
+ value)
+ Console.WriteLine(result)
+ End Sub
+End Module
+' The example displays the following output:
+' 00016342 00016342.000 0,000,016,342.0
+'
diff --git a/snippets/visualbasic/System/String/Format/qa29.vb b/snippets/visualbasic/System/String/Format/qa29.vb
new file mode 100644
index 00000000000..70e558104aa
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa29.vb
@@ -0,0 +1,14 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example10
+ Public Sub Main()
+ Dim value As Integer = 1326
+ Dim result As String = String.Format("{0,10:D6} {0,10:X8}", value)
+ Console.WriteLine(result)
+ End Sub
+End Module
+' The example displays the following output:
+' 001326 0000052E
+'
diff --git a/snippets/visualbasic/System/String/Format/qa3.vb b/snippets/visualbasic/System/String/Format/qa3.vb
new file mode 100644
index 00000000000..22254d6772b
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/qa3.vb
@@ -0,0 +1,58 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example11
+ Public Sub Main()
+ WillThrow()
+ Console.WriteLine()
+ WontThrow()
+ Console.WriteLine()
+ Recommended()
+ End Sub
+
+ Public Sub WillThrow()
+ Dim result As String
+ Dim nOpen As Integer = 1
+ Dim nClose As Integer = 2
+ Try
+ '
+ result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
+ nOpen, nClose)
+ '
+ Console.WriteLine(result)
+ Catch e As FormatException
+ Console.WriteLine("FormatException")
+ End Try
+ End Sub
+
+ Public Sub WontThrow()
+ Dim result As String
+ Dim nOpen As Integer = 1
+ Dim nClose As Integer = 2
+ Try
+ '
+ result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
+ nOpen, nClose)
+ '
+ Console.WriteLine(result)
+ Catch e As FormatException
+ Console.WriteLine("FormatException")
+ End Try
+ End Sub
+
+ Public Sub Recommended()
+ Dim result As String
+ Dim nOpen As Integer = 1
+ Dim nClose As Integer = 2
+ Try
+ '
+ result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.",
+ nOpen, "{", nClose, "}")
+ '
+ Console.WriteLine(result)
+ Catch e As FormatException
+ Console.WriteLine("FormatException")
+ End Try
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Format/starting1.vb b/snippets/visualbasic/System/String/Format/starting1.vb
new file mode 100644
index 00000000000..692f6eb6270
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/starting1.vb
@@ -0,0 +1,80 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Imports System.Text
+
+Module Example14
+ Public Sub Main()
+ '
+ Dim temp As Decimal = 20.4D
+ Dim s As String = String.Format("The temperature is {0}°C.", temp)
+ Console.WriteLine(s)
+ ' Displays 'The temperature is 20.4°C.'
+ '
+
+ Snippet31()
+ Snippet32()
+ Snippet33()
+ Snippet34()
+ End Sub
+
+ Private Sub Snippet31()
+ '
+ Dim s As String = String.Format("At {0}, the temperature is {1}°C.",
+ Date.Now, 20.4)
+ ' Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
+ '
+ Console.WriteLine(s)
+ End Sub
+
+ Private Sub Snippet32()
+ '
+ Dim s As String = String.Format("It is now {0:d} at {0:t}",
+ Date.Now)
+ ' Output similar to: 'It is now 4/10/2015 at 10:04 AM'
+ '
+ Console.WriteLine(s)
+ End Sub
+
+ Private Sub Snippet33()
+ '
+ Dim years() As Integer = {2013, 2014, 2015}
+ Dim population() As Integer = {1025632, 1105967, 1148203}
+ Dim sb As New StringBuilder()
+ sb.Append(String.Format("{0,6} {1,15}{2}{2}",
+ "Year", "Population", vbCrLf))
+ For index As Integer = 0 To years.Length - 1
+ sb.AppendFormat("{0,6} {1,15:N0}{2}",
+ years(index), population(index), vbCrLf)
+ Next
+ ' Result:
+ ' Year Population
+ '
+ ' 2013 1,025,632
+ ' 2014 1,105,967
+ ' 2015 1,148,203
+ '
+ Console.WriteLine(sb)
+ End Sub
+
+ Private Sub Snippet34()
+ '
+ Dim years() As Integer = {2013, 2014, 2015}
+ Dim population() As Integer = {1025632, 1105967, 1148203}
+ Dim s As String = String.Format("{0,-10} {1,-10}{2}{2}",
+ "Year", "Population", vbCrLf)
+ For index As Integer = 0 To years.Length - 1
+ s += String.Format("{0,-10} {1,-10:N0}{2}",
+ years(index), population(index), vbCrLf)
+ Next
+ ' Result:
+ ' Year Population
+ '
+ ' 2013 1,025,632
+ ' 2014 1,105,967
+ ' 2015 1,148,203
+ '
+ Console.WriteLine(vbCrLf + s)
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Format/starting2.vb b/snippets/visualbasic/System/String/Format/starting2.vb
new file mode 100644
index 00000000000..9267fd49bae
--- /dev/null
+++ b/snippets/visualbasic/System/String/Format/starting2.vb
@@ -0,0 +1,27 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example15
+ Public Sub Main()
+ '
+ Dim pricePerOunce As Decimal = 17.36D
+ Dim s As String = String.Format("The current price is {0} per ounce.",
+ pricePerOunce)
+ ' Result: The current price is 17.36 per ounce.
+ '
+ Console.WriteLine(s)
+ ShowFormatted()
+ End Sub
+
+ Private Sub ShowFormatted()
+ '
+ Dim pricePerOunce As Decimal = 17.36D
+ Dim s As String = String.Format("The current price is {0:C2} per ounce.",
+ pricePerOunce)
+ ' Result if current culture is en-US:
+ ' The current price is $17.36 per ounce.
+ '
+ Console.WriteLine(s)
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Intern/Intern1.vb b/snippets/visualbasic/System/String/Intern/Intern1.vb
new file mode 100644
index 00000000000..cf5eb1e1d28
--- /dev/null
+++ b/snippets/visualbasic/System/String/Intern/Intern1.vb
@@ -0,0 +1,18 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Imports System.Text
+
+
+Module modMain
+ Public Sub Main()
+ '
+ Dim s1 As String = "MyTest"
+ Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
+ Dim s3 As String = String.Intern(s2)
+ Console.WriteLine(CObj(s2) Is CObj(s1)) ' Different references.
+ Console.WriteLine(CObj(s3) Is CObj(s1)) ' The same reference.
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Intern/string_intern.vb b/snippets/visualbasic/System/String/Intern/string_intern.vb
index c8805b9125a..f007a8cece7 100644
--- a/snippets/visualbasic/System/String/Intern/string_intern.vb
+++ b/snippets/visualbasic/System/String/Intern/string_intern.vb
@@ -3,7 +3,7 @@ Imports System.Text
Class Sample
- Public Shared Sub Main()
+ Public Shared Sub Run()
Dim s1 As String = New StringBuilder().Append("My").Append("Test").ToString()
Dim s2 As String = New StringBuilder().Append("My").Append("Test").ToString()
Console.WriteLine($"s1 = {s1}")
diff --git a/snippets/visualbasic/System/String/IsNullOrEmpty/NullString1.vb b/snippets/visualbasic/System/String/IsNullOrEmpty/NullString1.vb
new file mode 100644
index 00000000000..dbb4879f8dc
--- /dev/null
+++ b/snippets/visualbasic/System/String/IsNullOrEmpty/NullString1.vb
@@ -0,0 +1,34 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example
+ Public Sub Main3()
+ Dim s As String
+
+ Console.WriteLine("The value of the string is '{0}'", s)
+
+ Try
+ Console.WriteLine("String length is {0}", s.Length)
+ Catch e As NullReferenceException
+ Console.WriteLine(e.Message)
+ End Try
+ End Sub
+End Module
+' The example displays the following output:
+' The value of the string is ''
+' Object reference not set to an instance of an object.
+'
+
+Public Class Empty
+ Public Sub Test()
+ '
+ Dim s As String = ""
+ Console.WriteLine("The length of '{0}' is {1}.", s, s.Length)
+ ' The example displays the following output:
+ ' The length of '' is 0.
+ '
+ End Sub
+End Class
+
+
diff --git a/snippets/visualbasic/System/String/IsNullOrEmpty/isnullorempty1.vb b/snippets/visualbasic/System/String/IsNullOrEmpty/isnullorempty1.vb
new file mode 100644
index 00000000000..da31b0e6466
--- /dev/null
+++ b/snippets/visualbasic/System/String/IsNullOrEmpty/isnullorempty1.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example1
+ Public Sub Main1()
+ Dim s1 As String = Nothing
+ Dim s2 As String = ""
+ Console.WriteLine(TestForNullOrEmpty(s1))
+ Console.WriteLine(TestForNullOrEmpty(s2))
+ End Sub
+
+ Private Function TestForNullOrEmpty(s As String) As Boolean
+ Dim result As Boolean
+ '
+ result = s Is Nothing OrElse s = String.Empty
+ '
+ Return result
+ End Function
+End Module
+
diff --git a/snippets/visualbasic/System/String/Overview/Project.vbproj b/snippets/visualbasic/System/String/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/String/Overview/case1.vb b/snippets/visualbasic/System/String/Overview/case1.vb
new file mode 100644
index 00000000000..3c508dc5a1b
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/case1.vb
@@ -0,0 +1,57 @@
+' Visual Basic .NET Document
+Option Strict On
+Option Infer On
+
+'
+Imports System.Globalization
+Imports System.IO
+
+Module Example1
+ Public Sub Main()
+ Dim sw As New StreamWriter(".\case.txt")
+ Dim words As String() = {"file", "sıfır", "Dženana"}
+ Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture,
+ New CultureInfo("en-US"),
+ New CultureInfo("tr-TR")}
+
+ For Each word In words
+ sw.WriteLine("{0}:", word)
+ For Each culture In cultures
+ Dim name As String = If(String.IsNullOrEmpty(culture.Name),
+ "Invariant", culture.Name)
+ Dim upperWord As String = word.ToUpper(culture)
+ sw.WriteLine(" {0,10}: {1,7} {2, 38}", name,
+ upperWord, ShowHexValue(upperWord))
+
+ Next
+ sw.WriteLine()
+ Next
+ sw.Close()
+ End Sub
+
+ Private Function ShowHexValue(s As String) As String
+ Dim retval As String = Nothing
+ For Each ch In s
+ Dim bytes() As Byte = BitConverter.GetBytes(ch)
+ retval += String.Format("{0:X2} {1:X2} ", bytes(1), bytes(0))
+ Next
+ Return retval
+ End Function
+End Module
+' The example displays the following output:
+' file:
+' Invariant: FILE 00 46 00 49 00 4C 00 45
+' en-US: FILE 00 46 00 49 00 4C 00 45
+' tr-TR: FİLE 00 46 01 30 00 4C 00 45
+'
+' sıfır:
+' Invariant: SıFıR 00 53 01 31 00 46 01 31 00 52
+' en-US: SIFIR 00 53 00 49 00 46 00 49 00 52
+' tr-TR: SIFIR 00 53 00 49 00 46 00 49 00 52
+'
+' Dženana:
+' Invariant: DžENANA 01 C5 00 45 00 4E 00 41 00 4E 00 41
+' en-US: DŽENANA 01 C4 00 45 00 4E 00 41 00 4E 00 41
+' tr-TR: DŽENANA 01 C4 00 45 00 4E 00 41 00 4E 00 41
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/case2.vb b/snippets/visualbasic/System/String/Overview/case2.vb
new file mode 100644
index 00000000000..72954381ab8
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/case2.vb
@@ -0,0 +1,41 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+Imports System.Threading
+
+Module Example2
+ Const disallowed = "file"
+
+ Public Sub Main()
+ IsAccessAllowed("FILE:\\\c:\users\user001\documents\FinancialInfo.txt")
+ End Sub
+
+ Private Sub IsAccessAllowed(resource As String)
+ Dim cultures() As CultureInfo = {CultureInfo.CreateSpecificCulture("en-US"),
+ CultureInfo.CreateSpecificCulture("tr-TR")}
+ Dim scheme As String = Nothing
+ Dim index As Integer = resource.IndexOfAny({"\"c, "/"c})
+ If index > 0 Then scheme = resource.Substring(0, index - 1)
+
+ ' Change the current culture and perform the comparison.
+ For Each culture In cultures
+ Thread.CurrentThread.CurrentCulture = culture
+ Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName)
+ Console.WriteLine(resource)
+ Console.WriteLine("Access allowed: {0}",
+ Not String.Equals(disallowed, scheme, StringComparison.CurrentCultureIgnoreCase))
+ Console.WriteLine()
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Culture: English (United States)
+' FILE:\\\c:\users\user001\documents\FinancialInfo.txt
+' Access allowed: False
+'
+' Culture: Turkish (Turkey)
+' FILE:\\\c:\users\user001\documents\FinancialInfo.txt
+' Access allowed: True
+'
diff --git a/snippets/visualbasic/System/String/Overview/compare1.vb b/snippets/visualbasic/System/String/Overview/compare1.vb
new file mode 100644
index 00000000000..3ddd13df075
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/compare1.vb
@@ -0,0 +1,18 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+Imports System.Threading
+
+Module Example3
+ Public Sub Main()
+ Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
+ Console.WriteLine(String.Compare("A", "a", StringComparison.CurrentCulture))
+ Console.WriteLine(String.Compare("A", "a", StringComparison.Ordinal))
+ End Sub
+End Module
+' The example displays the following output:
+' 1
+' -32
+'
diff --git a/snippets/visualbasic/System/String/Overview/compare2.vb b/snippets/visualbasic/System/String/Overview/compare2.vb
new file mode 100644
index 00000000000..13abcbc847a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/compare2.vb
@@ -0,0 +1,66 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Collections
+Imports System.Collections.Generic
+Imports System.Globalization
+
+Module Example4
+ Public Sub Main()
+ Dim strings() As String = {"coop", "co-op", "cooperative",
+ "co" + ChrW(&HAD) + "operative",
+ "cœur", "coeur"}
+
+ ' Perform a word sort using the current (en-US) culture.
+ Dim current(strings.Length - 1) As String
+ strings.CopyTo(current, 0)
+ Array.Sort(current, StringComparer.CurrentCulture)
+
+ ' Perform a word sort using the invariant culture.
+ Dim invariant(strings.Length - 1) As String
+ strings.CopyTo(invariant, 0)
+ Array.Sort(invariant, StringComparer.InvariantCulture)
+
+ ' Perform an ordinal sort.
+ Dim ordinal(strings.Length - 1) As String
+ strings.CopyTo(ordinal, 0)
+ Array.Sort(ordinal, StringComparer.Ordinal)
+
+ ' Perform a string sort using the current culture.
+ Dim stringSort(strings.Length - 1) As String
+ strings.CopyTo(stringSort, 0)
+ Array.Sort(stringSort, New SCompare())
+
+ ' Display array values
+ Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}",
+ "Original", "Word Sort", "Invariant Word",
+ "Ordinal Sort", "String Sort")
+ Console.WriteLine()
+
+ For ctr As Integer = 0 To strings.Length - 1
+ Console.WriteLine("{0,13} {1,13} {2,15} {3,13} {4,13}",
+ strings(ctr), current(ctr), invariant(ctr),
+ ordinal(ctr), stringSort(ctr))
+ Next
+ End Sub
+End Module
+
+' IComparer implementation to perform string sort.
+Friend Class SCompare : Implements IComparer(Of String)
+ Public Function Compare(x As String, y As String) As Integer _
+ Implements IComparer(Of String).Compare
+ Return CultureInfo.CurrentCulture.CompareInfo.Compare(x, y, CompareOptions.StringSort)
+ End Function
+End Class
+' The example displays the following output:
+' Original Word Sort Invariant Word Ordinal Sort String Sort
+'
+' coop cœur cœur co-op co-op
+' co-op coeur coeur coeur cœur
+' cooperative coop coop coop coeur
+' cooperative co-op co-op cooperative coop
+' cœur cooperative cooperative cooperative cooperative
+' coeur cooperative cooperative cœur cooperative
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/compare3.vb b/snippets/visualbasic/System/String/Overview/compare3.vb
new file mode 100644
index 00000000000..c8529fc8a23
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/compare3.vb
@@ -0,0 +1,42 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example5
+ Public Sub Main()
+ ' Search for "oe" and "œu" in "œufs" and "oeufs".
+ Dim s1 As String = "œufs"
+ Dim s2 As String = "oeufs"
+ FindInString(s1, "oe", StringComparison.CurrentCulture)
+ FindInString(s1, "oe", StringComparison.Ordinal)
+ FindInString(s2, "œu", StringComparison.CurrentCulture)
+ FindInString(s2, "œu", StringComparison.Ordinal)
+ Console.WriteLine()
+
+ Dim softHyphen As String = ChrW(&HAD)
+ Dim s3 As String = "co" + softHyphen + "operative"
+ FindInString(s3, softHyphen, StringComparison.CurrentCulture)
+ FindInString(s3, softHyphen, StringComparison.Ordinal)
+ End Sub
+
+ Private Sub FindInString(s As String, substring As String,
+ options As StringComparison)
+ Dim result As Integer = s.IndexOf(substring, options)
+ If result <> -1 Then
+ Console.WriteLine("'{0}' found in {1} at position {2}",
+ substring, s, result)
+ Else
+ Console.WriteLine("'{0}' not found in {1}",
+ substring, s)
+ End If
+ End Sub
+End Module
+' The example displays the following output:
+' 'oe' found in œufs at position 0
+' 'oe' not found in œufs
+' 'œu' found in oeufs at position 0
+' 'œu' not found in oeufs
+'
+' '' found in cooperative at position 0
+' '' found in cooperative at position 2
+'
diff --git a/snippets/visualbasic/System/String/Overview/compare4.vb b/snippets/visualbasic/System/String/Overview/compare4.vb
new file mode 100644
index 00000000000..b7816e2d1a6
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/compare4.vb
@@ -0,0 +1,53 @@
+'
+Imports System.Globalization
+Imports System.Threading
+
+Public Module Example6
+ Public Sub Main()
+ Dim str1 As String = "Apple"
+ Dim str2 As String = "Æble"
+ Dim str3 As String = "AEble"
+
+ ' Set the current culture to Danish in Denmark.
+ Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
+ Console.WriteLine("Current culture: {0}",
+ CultureInfo.CurrentCulture.Name)
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str1, str2, String.Compare(str1, str2))
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str2, str3, String.Compare(str2, str3))
+ Console.WriteLine()
+
+ ' Set the current culture to English in the U.S.
+ Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
+ Console.WriteLine("Current culture: {0}",
+ CultureInfo.CurrentCulture.Name)
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str1, str2, String.Compare(str1, str2))
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str2, str3, String.Compare(str2, str3))
+ Console.WriteLine()
+
+ ' Perform an ordinal comparison.
+ Console.WriteLine("Ordinal comparison")
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str1, str2,
+ String.Compare(str1, str2, StringComparison.Ordinal))
+ Console.WriteLine("Comparison of {0} with {1}: {2}",
+ str2, str3,
+ String.Compare(str2, str3, StringComparison.Ordinal))
+ End Sub
+End Module
+' The example displays the following output:
+' Current culture: da-DK
+' Comparison of Apple with Æble: -1
+' Comparison of Æble with AEble: 1
+'
+' Current culture: en-US
+' Comparison of Apple with Æble: 1
+' Comparison of Æble with AEble: 0
+'
+' Ordinal comparison
+' Comparison of Apple with Æble: -133
+' Comparison of Æble with AEble: 133
+'
diff --git a/snippets/visualbasic/System/String/Overview/equality1.vb b/snippets/visualbasic/System/String/Overview/equality1.vb
new file mode 100644
index 00000000000..9f547d867cf
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/equality1.vb
@@ -0,0 +1,45 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+Imports System.Threading
+
+Module Example7
+ Public Sub Main()
+ Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR")
+
+ Dim filePath As String = "file://c:/notes.txt"
+
+ Console.WriteLine("Culture-sensitive test for equality:")
+ If Not TestForEquality(filePath, StringComparison.CurrentCultureIgnoreCase) Then
+ Console.WriteLine("Access to {0} is allowed.", filePath)
+ Else
+ Console.WriteLine("Access to {0} is not allowed.", filePath)
+ End If
+ Console.WriteLine()
+
+ Console.WriteLine("Ordinal test for equality:")
+ If Not TestForEquality(filePath, StringComparison.OrdinalIgnoreCase) Then
+ Console.WriteLine("Access to {0} is allowed.", filePath)
+ Else
+ Console.WriteLine("Access to {0} is not allowed.", filePath)
+ End If
+ End Sub
+
+ Private Function TestForEquality(str As String, cmp As StringComparison) As Boolean
+ Dim position As Integer = str.IndexOf("://")
+ If position < 0 Then Return False
+
+ Dim substring As String = str.Substring(0, position)
+ Return substring.Equals("FILE", cmp)
+ End Function
+End Module
+' The example displays the following output:
+' Culture-sensitive test for equality:
+' Access to file://c:/notes.txt is allowed.
+'
+' Ordinal test for equality:
+' Access to file://c:/notes.txt is not allowed.
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/format1.vb b/snippets/visualbasic/System/String/Overview/format1.vb
new file mode 100644
index 00000000000..193ff794318
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/format1.vb
@@ -0,0 +1,26 @@
+' Visual Basic .NET Document
+Option Strict On
+Option Infer On
+
+'
+Imports System.Globalization
+
+Module Example8
+ Public Sub Main()
+ Dim dat As Date = #3/1/2011#
+ Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture,
+ New CultureInfo("en-US"),
+ New CultureInfo("fr-FR")}
+
+ For Each culture In cultures
+ Console.WriteLine("{0,-12} {1}", If(String.IsNullOrEmpty(culture.Name),
+ "Invariant", culture.Name),
+ dat.ToString("d", culture))
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Invariant 03/01/2011
+' en-US 3/1/2011
+' fr-FR 01/03/2011
+'
diff --git a/snippets/visualbasic/System/String/Overview/grapheme1.vb b/snippets/visualbasic/System/String/Overview/grapheme1.vb
new file mode 100644
index 00000000000..4bd322a3278
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/grapheme1.vb
@@ -0,0 +1,36 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+Imports System.IO
+
+Module Example9
+ Public Sub Main()
+ Dim sw As New StreamWriter(".\graphemes.txt")
+ Dim grapheme As String = ChrW(&H61) + ChrW(&H308)
+ sw.WriteLine(grapheme)
+
+ Dim singleChar As String = ChrW(&HE4)
+ sw.WriteLine(singleChar)
+
+ sw.WriteLine("{0} = {1} (Culture-sensitive): {2}", grapheme, singleChar,
+ String.Equals(grapheme, singleChar,
+ StringComparison.CurrentCulture))
+ sw.WriteLine("{0} = {1} (Ordinal): {2}", grapheme, singleChar,
+ String.Equals(grapheme, singleChar,
+ StringComparison.Ordinal))
+ sw.WriteLine("{0} = {1} (Normalized Ordinal): {2}", grapheme, singleChar,
+ String.Equals(grapheme.Normalize(),
+ singleChar.Normalize(),
+ StringComparison.Ordinal))
+ sw.Close()
+ End Sub
+End Module
+' The example produces the following output:
+' ä
+' ä
+' ä = ä (Culture-sensitive): True
+' ä = ä (Ordinal): False
+' ä = ä (Normalized Ordinal): True
+'
diff --git a/snippets/visualbasic/System/String/Overview/immutable.vb b/snippets/visualbasic/System/String/Overview/immutable.vb
new file mode 100644
index 00000000000..279b9af029a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/immutable.vb
@@ -0,0 +1,26 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.IO
+Imports System.Text
+
+Module Example10
+ Public Sub Main()
+ Dim rnd As New Random()
+
+ Dim str As String = String.Empty
+ Dim sw As New StreamWriter(".\StringFile.txt",
+ False, Encoding.Unicode)
+
+ For ctr As Integer = 0 To 1000
+ str += ChrW(rnd.Next(1, &H530))
+ If str.Length Mod 60 = 0 Then str += vbCrLf
+ Next
+ sw.Write(str)
+ sw.Close()
+ End Sub
+End Module
+'
+
+
diff --git a/snippets/visualbasic/System/String/Overview/immutable1.vb b/snippets/visualbasic/System/String/Overview/immutable1.vb
new file mode 100644
index 00000000000..bcd5f0abc19
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/immutable1.vb
@@ -0,0 +1,24 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.IO
+Imports System.Text
+
+Module Example11
+ Public Sub Main()
+ Dim rnd As New Random()
+ Dim sb As New StringBuilder()
+ Dim sw As New StreamWriter(".\StringFile.txt",
+ False, Encoding.Unicode)
+
+ For ctr As Integer = 0 To 1000
+ sb.Append(ChrW(rnd.Next(1, &H530)))
+ If sb.Length Mod 60 = 0 Then sb.AppendLine()
+ Next
+ sw.Write(sb.ToString())
+ sw.Close()
+ End Sub
+End Module
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/index1.vb b/snippets/visualbasic/System/String/Overview/index1.vb
new file mode 100644
index 00000000000..80b45c6a808
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/index1.vb
@@ -0,0 +1,23 @@
+' Visual Basic .NET Document
+Option Strict On
+'
+Module Example12
+ Public Sub Main()
+ Dim s1 As String = "This string consists of a single short sentence."
+ Dim nWords As Integer = 0
+
+ s1 = s1.Trim()
+ For ctr As Integer = 0 To s1.Length - 1
+ If Char.IsPunctuation(s1(ctr)) Or Char.IsWhiteSpace(s1(ctr)) Then
+ nWords += 1
+ End If
+ Next
+ Console.WriteLine("The sentence{2} {0}{2}has {1} words.",
+ s1, nWords, vbCrLf)
+ End Sub
+End Module
+' The example displays the following output:
+' The sentence
+' This string consists of a single short sentence.
+' has 8 words.
+'
diff --git a/snippets/visualbasic/System/String/Overview/index2.vb b/snippets/visualbasic/System/String/Overview/index2.vb
new file mode 100644
index 00000000000..37efc190d9a
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/index2.vb
@@ -0,0 +1,23 @@
+' Visual Basic .NET Document
+Option Strict On
+'
+Module Example13
+ Public Sub Main()
+ Dim s1 As String = "This string consists of a single short sentence."
+ Dim nWords As Integer = 0
+
+ s1 = s1.Trim()
+ For Each ch In s1
+ If Char.IsPunctuation(ch) Or Char.IsWhiteSpace(ch) Then
+ nWords += 1
+ End If
+ Next
+ Console.WriteLine("The sentence{2} {0}{2}has {1} words.",
+ s1, nWords, vbCrLf)
+ End Sub
+End Module
+' The example displays the following output:
+' The sentence
+' This string consists of a single short sentence.
+' has 8 words.
+'
diff --git a/snippets/visualbasic/System/String/Overview/index3.vb b/snippets/visualbasic/System/String/Overview/index3.vb
new file mode 100644
index 00000000000..3a121ab0f1c
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/index3.vb
@@ -0,0 +1,76 @@
+' Visual Basic .NET Document
+Option Strict On
+Option Infer On
+
+'
+Imports System.Collections.Generic
+Imports System.Globalization
+
+Module Example14
+ Public Sub Main()
+ ' First sentence of The Mystery of the Yellow Room, by Leroux.
+ Dim opening As String = "Ce n'est pas sans une certaine émotion que " +
+ "je commence à raconter ici les aventures " +
+ "extraordinaires de Joseph Rouletabille."
+ ' Character counters.
+ Dim nChars As Integer = 0
+ ' Objects to store word count.
+ Dim chars As New List(Of Integer)()
+ Dim elements As New List(Of Integer)()
+
+ For Each ch In opening
+ ' Skip the ' character.
+ If ch = ChrW(&H27) Then Continue For
+
+ If Char.IsWhiteSpace(ch) Or Char.IsPunctuation(ch) Then
+ chars.Add(nChars)
+ nChars = 0
+ Else
+ nChars += 1
+ End If
+ Next
+
+ Dim te As TextElementEnumerator = StringInfo.GetTextElementEnumerator(opening)
+ Do While te.MoveNext()
+ Dim s As String = te.GetTextElement()
+ ' Skip the ' character.
+ If s = ChrW(&H27) Then Continue Do
+ If String.IsNullOrEmpty(s.Trim()) Or (s.Length = 1 AndAlso Char.IsPunctuation(Convert.ToChar(s))) Then
+ elements.Add(nChars)
+ nChars = 0
+ Else
+ nChars += 1
+ End If
+ Loop
+
+ ' Display character counts.
+ Console.WriteLine("{0,6} {1,20} {2,20}",
+ "Word #", "Char Objects", "Characters")
+ For ctr As Integer = 0 To chars.Count - 1
+ Console.WriteLine("{0,6} {1,20} {2,20}",
+ ctr, chars(ctr), elements(ctr))
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Word # Char Objects Characters
+' 0 2 2
+' 1 4 4
+' 2 3 3
+' 3 4 4
+' 4 3 3
+' 5 8 8
+' 6 8 7
+' 7 3 3
+' 8 2 2
+' 9 8 8
+' 10 2 1
+' 11 8 8
+' 12 3 3
+' 13 3 3
+' 14 9 9
+' 15 15 15
+' 16 2 2
+' 17 6 6
+' 18 12 12
+'
diff --git a/snippets/visualbasic/System/String/Overview/instantiate1.vb b/snippets/visualbasic/System/String/Overview/instantiate1.vb
new file mode 100644
index 00000000000..7dab99bae97
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/instantiate1.vb
@@ -0,0 +1,84 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example15
+ Public Sub Main()
+ InstantiateByAssignment()
+ Console.WriteLine("-----")
+ CallConstructors()
+ Console.WriteLine("-----")
+ Concatenate()
+ Console.WriteLine("-----")
+ ExtractString()
+ Console.WriteLine("-----")
+ Formatting()
+ End Sub
+
+ Private Sub InstantiateByAssignment()
+ '
+ Dim string1 As String = "This is a string created by assignment."
+ Console.WriteLine(string1)
+ Dim string2 As String = "The path is C:\PublicDocuments\Report1.doc"
+ Console.WriteLine(string2)
+ ' The example displays the following output:
+ ' This is a string created by assignment.
+ ' The path is C:\PublicDocuments\Report1.doc
+ '
+ End Sub
+
+ Private Sub CallConstructors()
+ '
+ Dim chars() As Char = {"w"c, "o"c, "r"c, "d"c}
+
+ ' Create a string from a character array.
+ Dim string1 As New String(chars)
+ Console.WriteLine(string1)
+
+ ' Create a string that consists of a character repeated 20 times.
+ Dim string2 As New String("c"c, 20)
+ Console.WriteLine(string2)
+ ' The example displays the following output:
+ ' word
+ ' cccccccccccccccccccc
+ '
+ End Sub
+
+ Private Sub Concatenate()
+ '
+ Dim string1 As String = "Today is " + Date.Now.ToString("D") + "."
+ Console.WriteLine(string1)
+ Dim string2 As String = "This is one sentence. " + "This is a second. "
+ string2 += "This is a third sentence."
+ Console.WriteLine(string2)
+ ' The example displays output like the following:
+ ' Today is Tuesday, July 06, 2011.
+ ' This is one sentence. This is a second. This is a third sentence.
+ '
+ End Sub
+
+ Private Sub ExtractString()
+ '
+ Dim sentence As String = "This sentence has five words."
+ ' Extract the second word.
+ Dim startPosition As Integer = sentence.IndexOf(" ") + 1
+ Dim word2 As String = sentence.Substring(startPosition,
+ sentence.IndexOf(" ", startPosition) - startPosition)
+ Console.WriteLine("Second word: " + word2)
+ ' The example displays the following output:
+ ' Second word: sentence
+ '
+ End Sub
+
+ Private Sub Formatting()
+ '
+ Dim dateAndTime As DateTime = #07/06/2011 7:32:00AM#
+ Dim temperature As Double = 68.3
+ Dim result As String = String.Format("At {0:t} on {0:D}, the temperature was {1:F1} degrees Fahrenheit.",
+ dateAndTime, temperature)
+ Console.WriteLine(result)
+ ' The example displays the following output:
+ ' At 7:32 AM on Wednesday, July 06, 2011, the temperature was 68.3 degrees Fahrenheit.
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/String/Overview/normalize1.vb b/snippets/visualbasic/System/String/Overview/normalize1.vb
new file mode 100644
index 00000000000..5aab63cf363
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/normalize1.vb
@@ -0,0 +1,96 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+Imports System.IO
+Imports System.Text
+
+Module Example16
+ Private sw As StreamWriter
+
+ Public Sub Main()
+ sw = New StreamWriter(".\TestNorm1.txt")
+
+ ' Define three versions of the same word.
+ Dim s1 As String = "sống" ' create word with U+1ED1
+ Dim s2 As String = "s" + ChrW(&HF4) + ChrW(&H301) + "ng"
+ Dim s3 As String = "so" + ChrW(&H302) + ChrW(&H301) + "ng"
+
+ TestForEquality(s1, s2, s3)
+ sw.WriteLine()
+
+ ' Normalize and compare strings using each normalization form.
+ For Each formName In [Enum].GetNames(GetType(NormalizationForm))
+ sw.WriteLine("Normalization {0}:", formName)
+ Dim nf As NormalizationForm = CType([Enum].Parse(GetType(NormalizationForm), formName),
+ NormalizationForm)
+ Dim sn() As String = NormalizeStrings(nf, s1, s2, s3)
+ TestForEquality(sn)
+ sw.WriteLine(vbCrLf)
+ Next
+
+ sw.Close()
+ End Sub
+
+ Private Sub TestForEquality(ParamArray words As String())
+ For ctr As Integer = 0 To words.Length - 2
+ For ctr2 As Integer = ctr + 1 To words.Length - 1
+ sw.WriteLine("{0} ({1}) = {2} ({3}): {4}",
+ words(ctr), ShowBytes(words(ctr)),
+ words(ctr2), ShowBytes(words(ctr2)),
+ words(ctr).Equals(words(ctr2), StringComparison.Ordinal))
+ Next
+ Next
+ End Sub
+
+ Private Function ShowBytes(str As String) As String
+ Dim result As String = Nothing
+ For Each ch In str
+ result += String.Format("{0} ", Convert.ToUInt16(ch).ToString("X4"))
+ Next
+ Return result.Trim()
+ End Function
+
+ Private Function NormalizeStrings(nf As NormalizationForm, ParamArray words() As String) As String()
+ For ctr As Integer = 0 To words.Length - 1
+ If Not words(ctr).IsNormalized(nf) Then
+ words(ctr) = words(ctr).Normalize(nf)
+ End If
+ Next
+ Return words
+ End Function
+End Module
+' The example displays the following output:
+' sống (0073 1ED1 006E 0067) = sống (0073 00F4 0301 006E 0067): False
+' sống (0073 1ED1 006E 0067) = sống (0073 006F 0302 0301 006E 0067): False
+' sống (0073 00F4 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): False
+'
+' Normalization FormC:
+'
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+'
+'
+' Normalization FormD:
+'
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+'
+'
+' Normalization FormKC:
+'
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+' sống (0073 1ED1 006E 0067) = sống (0073 1ED1 006E 0067): True
+'
+'
+' Normalization FormKD:
+'
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+' sống (0073 006F 0302 0301 006E 0067) = sống (0073 006F 0302 0301 006E 0067): True
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/nullorempty1.vb b/snippets/visualbasic/System/String/Overview/nullorempty1.vb
new file mode 100644
index 00000000000..00973e644ed
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/nullorempty1.vb
@@ -0,0 +1,74 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Imports System.Globalization
+
+Module Example17
+ Public Sub Main()
+ TestForIsNullOrEmpty()
+ Console.WriteLine("-----")
+ TestForIsNullOrEmptyOrWhitespaceOnly()
+ End Sub
+
+ Private Sub TestForIsNullOrEmpty()
+ Dim str As String = Nothing
+ '
+ If str Is Nothing OrElse str.Equals(String.Empty) Then
+ '
+
+ Else
+
+ End If
+ End Sub
+
+ Private Sub TestForIsNullOrEmptyOrWhitespaceOnly()
+ Dim str As String = " "
+ '
+ If str Is Nothing OrElse str.Equals(String.Empty) OrElse str.Trim().Equals(String.Empty) Then
+ '
+ Console.WriteLine("Bad!")
+ Else
+ Console.WriteLine("Good!")
+ End If
+ End Sub
+End Module
+
+Public Class Temperature : Implements IFormattable
+ Dim temp As Double
+
+ Public Sub New(temp As Double)
+ Me.temp = temp
+ End Sub
+
+ Public Overrides Function ToString() As String
+ Return Me.ToString("G", CultureInfo.CurrentCulture)
+ End Function
+
+ Public Overloads Function ToString(fmt As String) As String
+ Return Me.ToString(fmt, CultureInfo.CurrentCulture)
+ End Function
+
+ '
+ Public Overloads Function ToString(fmt As String, provider As IFormatProvider) As String _
+ Implements IFormattable.ToString
+ If String.IsNullOrEmpty(fmt) Then fmt = "G"
+ If provider Is Nothing Then provider = CultureInfo.CurrentCulture
+
+ Select Case fmt.ToUpperInvariant()
+ ' Return degrees in Celsius.
+ Case "G", "C"
+ Return temp.ToString("F2", provider) + "°C"
+ ' Return degrees in Fahrenheit.
+ Case "F"
+ Return (temp * 9 / 5 + 32).ToString("F2", provider) + "°F"
+ ' Return degrees in Kelvin.
+ Case "K"
+ Return (temp + 273.15).ToString()
+ Case Else
+ Throw New FormatException(
+ String.Format("The {0} format string is not supported.",
+ fmt))
+ End Select
+ End Function
+ '
+End Class
diff --git a/snippets/visualbasic/System/String/Overview/parse1.vb b/snippets/visualbasic/System/String/Overview/parse1.vb
new file mode 100644
index 00000000000..939154de358
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/parse1.vb
@@ -0,0 +1,33 @@
+' Visual Basic .NET Document
+Option Strict On
+Option Infer On
+
+'
+Imports System.Globalization
+
+Module Example18
+ Public Sub Main()
+ Dim dateString As String = "07/10/2011"
+ Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture,
+ CultureInfo.CreateSpecificCulture("en-GB"),
+ CultureInfo.CreateSpecificCulture("en-US")}
+ Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}", "Date String", "Culture",
+ "Month", "Day")
+ Console.WriteLine()
+ For Each culture In cultures
+ Dim dat As Date = DateTime.Parse(dateString, culture)
+ Console.WriteLine("{0,-12} {1,10} {2,8} {3,8}", dateString,
+ If(String.IsNullOrEmpty(culture.Name),
+ "Invariant", culture.Name),
+ dat.Month, dat.Day)
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Date String Culture Month Day
+'
+' 07/10/2011 Invariant 7 10
+' 07/10/2011 en-GB 10 7
+' 07/10/2011 en-US 7 10
+'
+
diff --git a/snippets/visualbasic/System/String/Overview/search1.vb b/snippets/visualbasic/System/String/Overview/search1.vb
new file mode 100644
index 00000000000..7b6b60f1623
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/search1.vb
@@ -0,0 +1,30 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Globalization
+
+Module Example19
+ Public Sub Main()
+ Dim cultureNames() As String = {"da-DK", "en-US"}
+ Dim ci As CompareInfo
+ Dim str As String = "aerial"
+ Dim ch As Char = "æ"c ' U+00E6
+
+ Console.Write("Ordinal comparison -- ")
+ Console.WriteLine("Position of '{0}' in {1}: {2}", ch, str,
+ str.IndexOf(ch))
+
+ For Each cultureName In cultureNames
+ ci = CultureInfo.CreateSpecificCulture(cultureName).CompareInfo
+ Console.Write("{0} cultural comparison -- ", cultureName)
+ Console.WriteLine("Position of '{0}' in {1}: {2}", ch, str,
+ ci.IndexOf(str, ch))
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' Ordinal comparison -- Position of 'æ' in aerial: -1
+' da-DK cultural comparison -- Position of 'æ' in aerial: -1
+' en-US cultural comparison -- Position of 'æ' in aerial: 0
+'
diff --git a/snippets/visualbasic/System/String/Overview/sort1.vb b/snippets/visualbasic/System/String/Overview/sort1.vb
new file mode 100644
index 00000000000..3dd6966da04
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/sort1.vb
@@ -0,0 +1,57 @@
+'
+Imports System.Globalization
+Imports System.IO
+Imports System.Threading
+
+Public Class TextToFile
+ Public Shared Sub Main()
+ ' Creates and initializes a new array to store
+ ' these date/time objects.
+ Dim stringArray() As String = { "Apple", "Æble", "Zebra"}
+
+ ' Displays the values of the array.
+ Console.WriteLine("The original string array:")
+ PrintIndexAndValues(stringArray)
+
+ ' Set the CurrentCulture to "en-US".
+ Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
+ ' Sort the values of the Array.
+ Array.Sort(stringArray)
+
+ ' Display the values of the array.
+ Console.WriteLine("After sorting for the ""en-US"" culture:")
+ PrintIndexAndValues(stringArray)
+
+ ' Set the CurrentCulture to "da-DK".
+ Thread.CurrentThread.CurrentCulture = New CultureInfo("da-DK")
+ ' Sort the values of the Array.
+ Array.Sort(stringArray)
+
+ ' Displays the values of the Array.
+ Console.WriteLine("After sorting for the culture ""da-DK"":")
+ PrintIndexAndValues(stringArray)
+ End Sub
+
+ Public Shared Sub PrintIndexAndValues(myArray() As String)
+ For i As Integer = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
+ Console.WriteLine("[{0}]: {1}", i, myArray(i))
+ Next
+ Console.WriteLine()
+ End Sub
+End Class
+' The example displays the following output:
+' The original string array:
+' [0]: Apple
+' [1]: Æble
+' [2]: Zebra
+'
+' After sorting for the "en-US" culture:
+' [0]: Æble
+' [1]: Apple
+' [2]: Zebra
+'
+' After sorting for the culture "da-DK":
+' [0]: Apple
+' [1]: Zebra
+' [2]: Æble
+'
\ No newline at end of file
diff --git a/snippets/visualbasic/System/String/Overview/surrogate1.vb b/snippets/visualbasic/System/String/Overview/surrogate1.vb
new file mode 100644
index 00000000000..9c09c965190
--- /dev/null
+++ b/snippets/visualbasic/System/String/Overview/surrogate1.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example20
+ Public Sub Main()
+ Dim surrogate As String = ChrW(&HD800) + ChrW(&HDC03)
+ For ctr As Integer = 0 To surrogate.Length - 1
+ Console.Write("U+{0:X2} ", Convert.ToUInt16(surrogate(ctr)))
+ Next
+ Console.WriteLine()
+ Console.WriteLine(" Is Surrogate Pair: {0}",
+ Char.IsSurrogatePair(surrogate(0), surrogate(1)))
+ End Sub
+End Module
+
+' The example displays the following output:
+' U+D800 U+DC03
+' Is Surrogate Pair: True
+'
diff --git a/snippets/visualbasic/System/TimeSpan/Overview/Project.vbproj b/snippets/visualbasic/System/TimeSpan/Overview/Project.vbproj
new file mode 100644
index 00000000000..531d891cd55
--- /dev/null
+++ b/snippets/visualbasic/System/TimeSpan/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net48
+
+
+
diff --git a/snippets/visualbasic/System/TimeSpan/Overview/instantiate1.vb b/snippets/visualbasic/System/TimeSpan/Overview/instantiate1.vb
new file mode 100644
index 00000000000..ba36f342218
--- /dev/null
+++ b/snippets/visualbasic/System/TimeSpan/Overview/instantiate1.vb
@@ -0,0 +1,66 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example1
+ Public Sub Main()
+ Implicit()
+ Console.WriteLine()
+ Explicit()
+ Console.WriteLine()
+ TimeSpanOperation()
+ Console.WriteLine()
+ Parse()
+ Console.WriteLine()
+ End Sub
+
+ Private Sub Implicit()
+ '
+ Dim interval As New TimeSpan()
+ Console.WriteLine(interval.Equals(TimeSpan.Zero)) ' Displays "True".
+ '
+ End Sub
+
+ Private Sub Explicit()
+ '
+ Dim interval As New TimeSpan(2, 14, 18)
+ Console.WriteLine(interval.ToString()) ' Displays "02:14:18".
+ '
+ End Sub
+
+ Private Sub TimeSpanOperation()
+ '
+ Dim departure As DateTime = #06/12/2010 6:32PM#
+ Dim arrival As DateTime = #06/13/2010 10:47PM#
+ Dim travelTime As TimeSpan = arrival - departure
+ Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime)
+ ' The example displays the following output:
+ ' 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
+ '
+ End Sub
+
+ Private Sub Parse()
+ '
+ Dim values() As String = {"12", "31.", "5.8:32:16", "12:12:15.95", ".12"}
+ For Each value As String In values
+ Try
+ Dim ts As TimeSpan = TimeSpan.Parse(value)
+ Console.WriteLine("'{0}' --> {1}", value, ts)
+ Catch e As FormatException
+ Console.WriteLine("Unable to parse '{0}'", value)
+ Catch e As OverflowException
+ Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value)
+ End Try
+ Next
+ ' The example displays the following output:
+ ' '12' --> 12.00:00:00
+ ' Unable to parse '31.'
+ ' '5.8:32:16' --> 5.08:32:16
+ ' '12:12:15.95' --> 12:12:15.9500000
+ ' Unable to parse '.12'
+ '
+ End Sub
+End Module
+
+' 02:10:21
+'
+' 22:26:43
diff --git a/snippets/visualbasic/System/TimeSpan/Overview/zero1.vb b/snippets/visualbasic/System/TimeSpan/Overview/zero1.vb
new file mode 100644
index 00000000000..fd9dda98826
--- /dev/null
+++ b/snippets/visualbasic/System/TimeSpan/Overview/zero1.vb
@@ -0,0 +1,27 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module MExample
+ Dim rnd As New Random()
+
+ Public Sub Main()
+ Dim timeSpent As TimeSpan = TimeSpan.Zero
+
+ timeSpent += GetTimeBeforeLunch()
+ timeSpent += GetTimeAfterLunch()
+
+ Console.WriteLine("Total time: {0}", timeSpent)
+ End Sub
+
+ Private Function GetTimeBeforeLunch() As TimeSpan
+ Return New TimeSpan(rnd.Next(3, 6), 0, 0)
+ End Function
+
+ Private Function GetTimeAfterLunch() As TimeSpan
+ Return New TimeSpan(rnd.Next(3, 6), 0, 0)
+ End Function
+End Module
+' The example displays output like the following:
+' Total time: 08:00:00
+'
diff --git a/snippets/visualbasic/System/TimeSpan/Parse/parse1.vb b/snippets/visualbasic/System/TimeSpan/Parse/parse1.vb
index 40e063e41cd..3bdf3be5a1e 100644
--- a/snippets/visualbasic/System/TimeSpan/Parse/parse1.vb
+++ b/snippets/visualbasic/System/TimeSpan/Parse/parse1.vb
@@ -5,7 +5,7 @@ Imports System.Globalization
Imports System.Threading
Module Example1
- Public Sub Main()
+ Public Sub Main1()
'
Dim values() As String = {"6", "6:12", "6:12:14", "6:12:14:45",
"6.12:14:45", "6:12:14:45.3448",
@@ -40,7 +40,7 @@ Module Example1
' 6:12:14:45.3448: Bad Format
' 6:12:14:45,3448 --> 6.12:14:45.3448000
' 6:34:14:45: Overflow
- '
+ '
' Current Culture: en-US
' 6 --> 6.00:00:00
' 6:12 --> 06:12:00
diff --git a/snippets/visualbasic/System/TimeSpan/Parse/parsefailure1.vb b/snippets/visualbasic/System/TimeSpan/Parse/parsefailure1.vb
new file mode 100644
index 00000000000..9aeb188c916
--- /dev/null
+++ b/snippets/visualbasic/System/TimeSpan/Parse/parsefailure1.vb
@@ -0,0 +1,27 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example
+ Public Sub Main()
+ '
+ Dim values() As String = { "000000006", "12.12:12:12.12345678" }
+ For Each value As String In values
+ Try
+ Dim interval As TimeSpan = TimeSpan.Parse(value)
+ Console.WriteLine("{0} --> {1}", value, interval)
+ Catch e As FormatException
+ Console.WriteLine("{0}: Bad Format", value)
+ Catch e As OverflowException
+ Console.WriteLine("{0}: Overflow", value)
+ End Try
+ Next
+ ' Output from .NET Framework 3.5 and earlier versions:
+ ' 000000006 --> 6.00:00:00
+ ' 12.12:12:12.12345678: Bad Format
+ ' Output from .NET Framework 4:
+ ' 000000006: Overflow
+ ' 12.12:12:12.12345678: Overflow
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/Type/MakeGenericType/Project.vbproj b/snippets/visualbasic/System/Type/MakeGenericType/Project.vbproj
index 92e46ddaccf..a15a29bf12c 100644
--- a/snippets/visualbasic/System/Type/MakeGenericType/Project.vbproj
+++ b/snippets/visualbasic/System/Type/MakeGenericType/Project.vbproj
@@ -2,7 +2,7 @@
Exe
- net9.0
+ net10.0
diff --git a/snippets/visualbasic/System/Type/MakeGenericType/remarks.vb b/snippets/visualbasic/System/Type/MakeGenericType/remarks.vb
new file mode 100644
index 00000000000..718a6eba3d1
--- /dev/null
+++ b/snippets/visualbasic/System/Type/MakeGenericType/remarks.vb
@@ -0,0 +1,23 @@
+'
+Public Class Base(Of T, U)
+End Class
+Public Class Derived(Of V)
+ Inherits Base(Of Integer, V)
+End Class
+'
+
+'
+Public Class Outermost(Of T)
+ Public Class Inner(Of U)
+ Public Class Innermost1(Of V)
+ End Class
+ Public Class Innermost2
+ End Class
+ End Class
+End Class
+'
+
+Public Class ProgStubClass
+ Shared Sub Main()
+ End Sub
+End Class
\ No newline at end of file
diff --git a/snippets/visualbasic/System/Type/MakeGenericType/source.vb b/snippets/visualbasic/System/Type/MakeGenericType/source.vb
index 337b298d25f..adc2032fa06 100644
--- a/snippets/visualbasic/System/Type/MakeGenericType/source.vb
+++ b/snippets/visualbasic/System/Type/MakeGenericType/source.vb
@@ -1,9 +1,7 @@
'
-Imports System.Reflection
-Imports System.Collections.Generic
Public Class Test
- Public Shared Sub Main()
+ Public Shared Sub Main2()
Console.WriteLine(vbCrLf & "--- Create a constructed type from the generic Dictionary type.")
' Create a type object representing the generic Dictionary
@@ -16,7 +14,7 @@ Public Class Test
' Create an array of types to substitute for the type
' parameters of Dictionary. The key is of type string, and
' the type to be contained in the Dictionary is Test.
- Dim typeArgs() As Type = { GetType(String), GetType(Test) }
+ Dim typeArgs() As Type = {GetType(String), GetType(Test)}
' Create a Type object representing the constructed generic
' type.
@@ -30,7 +28,7 @@ Public Class Test
Dim t As Type = GetType(Dictionary(Of String, Test))
Console.WriteLine(vbTab & "Are the constructed types equal? " _
& (t Is constructed))
- Console.WriteLine(vbTab & "Are the generic types equal? " _
+ Console.WriteLine(vbTab & "Are the generic types equal? " _
& (t.GetGenericTypeDefinition() Is generic))
End Sub
diff --git a/snippets/visualbasic/System/Type/Overview/Equals1.vb b/snippets/visualbasic/System/Type/Overview/Equals1.vb
new file mode 100644
index 00000000000..b8cd09a6f44
--- /dev/null
+++ b/snippets/visualbasic/System/Type/Overview/Equals1.vb
@@ -0,0 +1,28 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module MExample1
+ Public Sub Main()
+ Dim number1 As Long = 1635429
+ Dim number2 As Integer = 16203
+ Dim number3 As Double = 1639.41
+ Dim number4 As Long = 193685412
+
+ ' Get the type of number1.
+ Dim t As Type = number1.GetType()
+
+ ' Compare types of all objects with number1.
+ Console.WriteLine("Type of number1 and number2 are equal: {0}",
+ Object.ReferenceEquals(t, number2.GetType()))
+ Console.WriteLine("Type of number1 and number3 are equal: {0}",
+ Object.ReferenceEquals(t, number3.GetType()))
+ Console.WriteLine("Type of number1 and number4 are equal: {0}",
+ Object.ReferenceEquals(t, number4.GetType()))
+ End Sub
+End Module
+' The example displays the following output:
+' Type of number1 and number2 are equal: False
+' Type of number1 and number3 are equal: False
+' Type of number1 and number4 are equal: True
+'
diff --git a/snippets/visualbasic/System/Type/Overview/GetType1.vb b/snippets/visualbasic/System/Type/Overview/GetType1.vb
new file mode 100644
index 00000000000..3b2e05951be
--- /dev/null
+++ b/snippets/visualbasic/System/Type/Overview/GetType1.vb
@@ -0,0 +1,20 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example1
+ Public Sub Main()
+ Dim values() As Object = { "word", True, 120, 136.34, "a"c }
+ For Each value In values
+ Console.WriteLine("{0} - type {1}", value,
+ value.GetType().Name)
+ Next
+ End Sub
+End Module
+' The example displays the following output:
+' word - type String
+' True - type Boolean
+' 120 - type Int32
+' 136.34 - type Double
+' a - type Char
+'
diff --git a/snippets/visualbasic/System/Type/Overview/Project.vbproj b/snippets/visualbasic/System/Type/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/Type/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/TypeInitializationException/Overview/CtorException1.vb b/snippets/visualbasic/System/TypeInitializationException/Overview/CtorException1.vb
new file mode 100644
index 00000000000..bd383e0ace6
--- /dev/null
+++ b/snippets/visualbasic/System/TypeInitializationException/Overview/CtorException1.vb
@@ -0,0 +1,34 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Public Class Example1
+ Shared test As New TestClass(3)
+
+ Public Shared Sub Main()
+ Dim ex As New Example1()
+ Console.WriteLine(test.Value)
+ End Sub
+End Class
+
+Public Class TestClass
+ Public ReadOnly Value As Integer
+
+ Public Sub New(value As Integer)
+ If value < 0 Or value > 1 Then
+ Throw New ArgumentOutOfRangeException(NameOf(value))
+ End If
+ Value = value
+ End Sub
+End Class
+
+' The example displays the following output:
+' Unhandled Exception: System.TypeInitializationException:
+' The type initializer for 'Example' threw an exception. --->
+' System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
+' at TestClass..ctor(Int32 value)
+' at Example..cctor()
+' --- End of inner exception stack trace ---
+' at Example.Main()
+'
+
diff --git a/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1.vb b/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1.vb
new file mode 100644
index 00000000000..8568b653323
--- /dev/null
+++ b/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1.vb
@@ -0,0 +1,43 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Module Example3
+ Public Sub Main()
+ Dim p As New Person("John", "Doe")
+ Console.WriteLine(p)
+ End Sub
+End Module
+
+Public Class Person
+ Shared infoModule As InfoModule
+
+ Dim fName As String
+ Dim mName As String
+ Dim lName As String
+
+ Shared Sub New()
+ infoModule = New InfoModule(DateTime.UtcNow)
+ End Sub
+
+ Public Sub New(fName As String, lName As String)
+ Me.fName = fName
+ Me.lName = lName
+ infoModule.Increment()
+ End Sub
+
+ Public Overrides Function ToString() As String
+ Return String.Format("{0} {1}", fName, lName)
+ End Function
+End Class
+' The example displays the following output if missing1a.dll is renamed or removed:
+' Unhandled Exception: System.TypeInitializationException:
+' The type initializer for 'Person' threw an exception. --->
+' System.IO.FileNotFoundException: Could not load file or assembly
+' 'Missing1a, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
+' or one of its dependencies. The system cannot find the file specified.
+' at Person..cctor()
+' --- End of inner exception stack trace ---
+' at Person..ctor(String fName, String lName)
+' at Example.Main()
+'
diff --git a/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1a.vb b/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1a.vb
new file mode 100644
index 00000000000..1ee7064e804
--- /dev/null
+++ b/snippets/visualbasic/System/TypeInitializationException/Overview/Missing1a.vb
@@ -0,0 +1,23 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Public Class InfoModule
+ Private firstUse As DateTime
+ Private ctr As Integer = 0
+
+ Public Sub New(dat As DateTime)
+ firstUse = dat
+ End Sub
+
+ Public Function Increment() As Integer
+ ctr += 1
+ Return ctr
+ End Function
+
+ Public Function GetInitializationTime() As DateTime
+ Return firstUse
+ End Function
+End Class
+'
+
diff --git a/snippets/visualbasic/System/TypeInitializationException/Overview/Project.vbproj b/snippets/visualbasic/System/TypeInitializationException/Overview/Project.vbproj
new file mode 100644
index 00000000000..874c98f3477
--- /dev/null
+++ b/snippets/visualbasic/System/TypeInitializationException/Overview/Project.vbproj
@@ -0,0 +1,8 @@
+
+
+
+ Library
+ net10.0
+
+
+
diff --git a/snippets/visualbasic/System/TypeInitializationException/Overview/Regex1.vb b/snippets/visualbasic/System/TypeInitializationException/Overview/Regex1.vb
new file mode 100644
index 00000000000..2799d477a72
--- /dev/null
+++ b/snippets/visualbasic/System/TypeInitializationException/Overview/Regex1.vb
@@ -0,0 +1,30 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Text.RegularExpressions
+
+Module Example4
+ Public Sub Main()
+ Dim domain As AppDomain = AppDomain.CurrentDomain
+ ' Set a timeout interval of -2 seconds.
+ domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(-2))
+
+ Dim rgx As New Regex("[aeiouy]")
+ Console.WriteLine("Regular expression pattern: {0}", rgx.ToString())
+ Console.WriteLine("Timeout interval for this regex: {0} seconds",
+ rgx.MatchTimeout.TotalSeconds)
+ End Sub
+End Module
+' The example displays the following output:
+' Unhandled Exception: System.TypeInitializationException:
+' The type initializer for 'System.Text.RegularExpressions.Regex' threw an exception. --->
+' System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
+' Parameter name: AppDomain data 'REGEX_DEFAULT_MATCH_TIMEOUT' contains an invalid value or
+' object for specifying a default matching timeout for System.Text.RegularExpressions.Regex.
+' at System.Text.RegularExpressions.Regex.InitDefaultMatchTimeout()
+' at System.Text.RegularExpressions.Regex..cctor()
+' --- End of inner exception stack trace ---
+' at System.Text.RegularExpressions.Regex..ctor(String pattern)
+' at Example.Main()
+'
diff --git a/snippets/visualbasic/System/Version/Overview/GettingVersions1.vb b/snippets/visualbasic/System/Version/Overview/GettingVersions1.vb
new file mode 100644
index 00000000000..52f3c0ae15d
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/GettingVersions1.vb
@@ -0,0 +1,42 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Imports System.Reflection
+
+
+Module modMain
+
+ Public Sub Main()
+ GetOsVersion()
+ Console.WriteLine()
+ GetClrVersion()
+ Console.WriteLine()
+ End Sub
+
+ Private Sub GetOsVersion()
+ '
+ ' Get the operating system version.
+ Dim os As OperatingSystem = Environment.OSVersion
+ Dim ver As Version = os.Version
+ Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString())
+ '
+ End Sub
+
+ Private Sub GetClrVersion()
+ '
+ ' Get the common language runtime version.
+ Dim ver As Version = Environment.Version
+ Console.WriteLine("CLR Version {0}", ver.ToString())
+ '
+ End Sub
+End Module
+
+Public Module Example4
+ Public Sub GetExecutingAssemblyVersion()
+ ' Get the version of the current assembly.
+ Dim assem As Assembly = Assembly.GetExecutingAssembly()
+ Dim assemName As AssemblyName = assem.GetName()
+ Dim ver As Version = assemName.Version
+ Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString())
+ End Sub
+End Module
diff --git a/snippets/visualbasic/System/Version/Overview/Project.vbproj b/snippets/visualbasic/System/Version/Overview/Project.vbproj
new file mode 100644
index 00000000000..96f746920dd
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/Project.vbproj
@@ -0,0 +1,9 @@
+
+
+
+ Library
+ net10.0
+ false
+
+
+
diff --git a/snippets/visualbasic/System/Version/Overview/comparisons1.vb b/snippets/visualbasic/System/Version/Overview/comparisons1.vb
new file mode 100644
index 00000000000..f955dc5688b
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/comparisons1.vb
@@ -0,0 +1,28 @@
+' Visual Basic .NET Document
+Option Strict On
+
+Module Example
+ Public Sub Main()
+ CompareSimple()
+ End Sub
+
+ Private Sub CompareSimple()
+ '
+ Dim v1 As New Version(2,0)
+ Dim v2 As New Version("2.1")
+ Console.Write("Version {0} is ", v1)
+ Select Case v1.CompareTo(v2)
+ Case 0
+ Console.Write("the same as")
+ Case 1
+ Console.Write("later than")
+ Case -1
+ Console.Write("earlier than")
+ End Select
+ Console.WriteLine(" Version {0}.", v2)
+ ' The example displays the following output:
+ ' Version 2.0 is earlier than Version 2.1.
+ '
+ End Sub
+End Module
+
diff --git a/snippets/visualbasic/System/Version/Overview/comparisons2.vb b/snippets/visualbasic/System/Version/Overview/comparisons2.vb
new file mode 100644
index 00000000000..5d3923dff89
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/comparisons2.vb
@@ -0,0 +1,29 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Public Enum VersionTime
+ Earlier = -1
+ Same = 0
+ Later = 1
+End Enum
+
+Module Example2
+ Public Sub Main()
+ Dim v1 As New Version(1, 1)
+ Dim v1a As New Version("1.1.0")
+ ShowRelationship(v1, v1a)
+
+ Dim v1b As New Version(1, 1, 0, 0)
+ ShowRelationship(v1b, v1a)
+ End Sub
+
+ Private Sub ShowRelationship(v1 As Version, v2 As Version)
+ Console.WriteLine("Relationship of {0} to {1}: {2}",
+ v1, v2, CType(v1.CompareTo(v2), VersionTime))
+ End Sub
+End Module
+' The example displays the following output:
+' Relationship of 1.1 to 1.1.0: Earlier
+' Relationship of 1.1.0.0 to 1.1.0: Later
+'
diff --git a/snippets/visualbasic/System/Version/Overview/currentapp.vb b/snippets/visualbasic/System/Version/Overview/currentapp.vb
new file mode 100644
index 00000000000..d4786b97b6b
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/currentapp.vb
@@ -0,0 +1,17 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Reflection
+
+Module Example3
+ Public Sub Main()
+ ' Get the version of the executing assembly (that is, this assembly).
+ Dim assem As Assembly = Assembly.GetEntryAssembly()
+ Dim assemName As AssemblyName = assem.GetName()
+ Dim ver As Version = assemName.Version
+ Console.WriteLine("Application {0}, Version {1}", assemName.Name, ver.ToString())
+ End Sub
+End Module
+'
+
diff --git a/snippets/visualbasic/System/Version/Overview/currentassem.vb b/snippets/visualbasic/System/Version/Overview/currentassem.vb
new file mode 100644
index 00000000000..874730bd3a9
--- /dev/null
+++ b/snippets/visualbasic/System/Version/Overview/currentassem.vb
@@ -0,0 +1,17 @@
+' Visual Basic .NET Document
+Option Strict On
+
+'
+Imports System.Reflection
+
+Module Example1
+ Public Sub Main()
+ ' Get the version of the current assembly.
+ Dim assem As Assembly = Assembly.GetExecutingAssembly()
+ Dim assemName As AssemblyName = assem.GetName()
+ Dim ver As Version = assemName.Version
+ Console.WriteLine("{0}, Version {1}", assemName.Name, ver.ToString())
+ End Sub
+End Module
+'
+
diff --git a/snippets/visualbasic/System/Version/Overview/example1.vb b/snippets/visualbasic/System/Version/Overview/example1.vb
index 4bc4b4983ad..bf25a1d6901 100644
--- a/snippets/visualbasic/System/Version/Overview/example1.vb
+++ b/snippets/visualbasic/System/Version/Overview/example1.vb
@@ -5,16 +5,17 @@ Option Strict On
Imports System.Reflection
-Module Example1
- Public Sub Main()
- Dim thisAssem As Assembly = GetType(Example1).Assembly
- Dim thisAssemName As AssemblyName = thisAssem.GetName()
-
- Dim ver As Version = thisAssemName.Version
-
- Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name)
- End Sub
+Module MExample1
+ Public Sub Main()
+ Dim thisAssem As Assembly = Assembly.GetExecutingAssembly()
+ Dim thisAssemName As AssemblyName = thisAssem.GetName()
+
+ Dim ver As Version = thisAssemName.Version
+
+ Console.WriteLine("This is version {0} of {1}.", ver, thisAssemName.Name)
+ End Sub
End Module
+
' The example displays the following output:
' This is version 2.0.1.0 of Example1.
'
diff --git a/xml/System/Random.xml b/xml/System/Random.xml
index 77b637a8d19..8e8172d1d9e 100644
--- a/xml/System/Random.xml
+++ b/xml/System/Random.xml
@@ -69,8 +69,218 @@
> [!NOTE]
> To generate a cryptographically secure random number, such as one that's suitable for creating a random password, use one of the static methods in the class.
-For more information about this API, see [Supplemental API remarks for Random](/dotnet/fundamentals/runtime-libraries/system-random).
- ]]>
+The class represents a pseudo-random number generator, which is an algorithm that produces a sequence of numbers that meet certain statistical requirements for randomness.
+
+Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The implementation of the class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. *The Art of Computer Programming, Volume 2: Seminumerical Algorithms*. Addison-Wesley, Reading, MA, third edition, 1997.
+
+To generate a cryptographically secure random number, such as one that's suitable for creating a random password, use one of the static methods in the class.
+
+## Instantiate the random number generator
+
+You instantiate the random number generator by providing a seed value (a starting value for the pseudo-random number generation algorithm) to a class constructor. You can supply the seed value either explicitly or implicitly:
+
+- The constructor uses an explicit seed value that you supply.
+- The constructor uses the default seed value. This is the most common way of instantiating the random number generator.
+
+The default seed value is produced by the thread-static, pseudo-random number generator.
+
+If the same seed is used for separate objects, they will generate the same series of random numbers. This can be useful for creating a test suite that processes random values, or for replaying games that derive their data from random numbers.
+
+To produce different sequences of random numbers, you can make the seed value time-dependent, thereby producing a different series with each new instance of . The parameterized constructor can take an value based on the number of ticks in the current time, whereas the parameterless constructor uses the system clock to generate its seed value.
+
+## Thread safety
+
+Instead of instantiating individual objects, we recommend that you create a single instance to generate all the random numbers needed by your app. However, objects are not thread safe. If your app calls methods from multiple threads, you must use a synchronization object to ensure that only one thread can access the random number generator at a time. If you don't ensure that the object is accessed in a thread-safe way, calls to methods that return random numbers return 0.
+
+The following example uses the C# [lock Statement](/dotnet/csharp/language-reference/keywords/lock-statement), the F# [lock function](https://fsharp.github.io/fsharp-core-docs/reference/fsharp-core-operators.html#lock) and the Visual Basic [SyncLock statement](/dotnet/visual-basic/language-reference/statements/synclock-statement) to ensure that a single random number generator is accessed by 11 threads in a thread-safe manner. Each thread generates 2 million random numbers, counts the number of random numbers generated and calculates their sum, and then updates the totals for all threads when it finishes executing.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/threadsafeex1.cs" id="Snippet3":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/threadsafeex1.fs" id="Snippet3":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/threadsafeex1.vb" id="Snippet3":::
+
+The example ensures thread-safety in the following ways:
+
+- The attribute is used to define thread-local variables that track the total number of random numbers generated and their sum for each thread.
+- A lock (the `lock` statement in C#, the `lock` function in F# and the `SyncLock` statement in Visual Basic) protects access to the variables for the total count and sum of all random numbers generated on all threads.
+- A semaphore (the object) is used to ensure that the main thread blocks until all other threads complete execution.
+- The example checks whether the random number generator has become corrupted by determining whether two consecutive calls to random number generation methods return 0. If corruption is detected, the example uses the object to signal that all threads should be canceled.
+- Before generating each random number, each thread checks the state of the object. If cancellation is requested, the example calls the method to cancel the thread.
+
+The following example is identical to the first, except that it uses a object and a lambda expression instead of objects.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/threadsafeex2.cs" id="Snippet4":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/threadsafeex2.fs" id="Snippet4":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/threadsafeex2.vb" id="Snippet4":::
+
+It differs from the first example in the following ways:
+
+- The variables to keep track of the number of random numbers generated and their sum in each task are local to the task, so there is no need to use the attribute.
+- The static method is used to ensure that the main thread doesn't complete before all tasks have finished. There is no need for the object.
+- The exception that results from task cancellation is surfaced in the method. In the previous example, it is handled by each thread.
+
+## Generate different types of random numbers
+
+The random number generator provides methods that let you generate the following kinds of random numbers:
+
+- A series of values. You determine the number of byte values by passing an array initialized to the number of elements you want the method to return to the method. The following example generates 20 bytes.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Random/Overview/nextbytes1.cs" id="Snippet5":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/nextbytes1.fs" id="Snippet5":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/nextbytes1.vb" id="Snippet5":::
+
+- A single integer. You can choose whether you want an integer from 0 to a maximum value ( - 1) by calling the method, an integer between 0 and a specific value by calling the method, or an integer within a range of values by calling the method. In the parameterized overloads, the specified maximum value is exclusive; that is, the actual maximum number generated is one less than the specified value.
+
+ The following example calls the method to generate 10 random numbers between -10 and 10. Note that the second argument to the method specifies the exclusive upper bound of the range of random values returned by the method. In other words, the largest integer that the method can return is one less than this value.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Random/Overview/nextex1.cs" id="Snippet6":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/nextex1.fs" id="Snippet6":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/nextex1.vb" id="Snippet6":::
+
+- A single floating-point value from 0.0 to less than 1.0 by calling the method. The exclusive upper bound of the random number returned by the method is 1, so its actual upper bound is 0.99999999999999978. The following example generates 10 random floating-point numbers.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Random/Overview/nextdoubleex1.cs" id="Snippet7":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/nextdoubleex1.fs" id="Snippet7":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/nextdoubleex1.vb" id="Snippet7":::
+
+> [!IMPORTANT]
+> The method allows you to specify the range of the returned random number. However, the `maxValue` parameter, which specifies the upper range returned number, is an exclusive, not an inclusive, value. This means that the method call `Next(0, 100)` returns a value between 0 and 99, and not between 0 and 100.
+
+You can also use the class for such tasks as generating [random Boolean values](#generate-random-boolean-values), generating [random floating-point values in a specified range](#retrieve-floating-point-values-in-a-specified-range), generating [Generate random 64-bit integers](#generate-random-64-bit-integers), and [retrieving a unique element from an array or collection](#retrieve-a-unique-element-from-an-array-or-collection).
+
+## Substitute your own algorithm
+
+You can implement your own random number generator by inheriting from the class and supplying your random number generation algorithm. To supply your own algorithm, you must override the method, which implements the random number generation algorithm. You should also override the , , and methods to ensure that they call your overridden method. You don't have to override the and methods.
+
+For an example that derives from the class and modifies its default pseudo-random number generator, see the reference page.
+
+## Retrieve the same sequence of random values
+
+Sometimes you want to generate the same sequence of random numbers in software test scenarios and in game playing. Testing with the same sequence of random numbers allows you to detect regressions and confirm bug fixes. Using the same sequence of random number in games allows you to replay previous games.
+
+You can generate the same sequence of random numbers by providing the same seed value to the constructor. The seed value provides a starting value for the pseudo-random number generation algorithm. The following example uses 100100 as an arbitrary seed value to instantiate the object, displays 20 random floating-point values, and persists the seed value. It then restores the seed value, instantiates a new random number generator, and displays the same 20 random floating-point values. Note that the example may produce different sequences of random numbers if run on different versions of .NET.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/same1.cs" id="Snippet12":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/same1.fs" id="Snippet12":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/same1.vb" id="Snippet12":::
+
+## Retrieve unique sequences of random numbers
+
+Providing different seed values to instances of the class causes each random number generator to produce a different sequence of values. You can provide a seed value either explicitly by calling the constructor, or implicitly by calling the constructor. Most developers call the parameterless constructor, which uses the system clock. The following example uses this approach to instantiate two instances. Each instance displays a series of 10 random integers.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/unique.cs" id="Snippet13":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/unique.fs" id="Snippet13":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/unique.vb" id="Snippet13":::
+
+## Retrieve integers in a specified range
+
+You can retrieve integers in a specified range by calling the method, which lets you specify both the lower and the upper bound of the numbers you'd like the random number generator to return. The upper bound is an exclusive, not an inclusive, value. That is, it isn't included in the range of values returned by the method. The following example uses this method to generate random integers between -10 and 10. Note that it specifies 11, which is one greater than the desired value, as the value of the `maxValue` argument in the method call.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/range1.cs" id="Snippet15":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/range1.fs" id="Snippet15":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/range1.vb" id="Snippet15":::
+
+## Retrieve integers with a specified number of digits
+
+You can call the method to retrieve numbers with a specified number of digits. For example, to retrieve numbers with four digits (that is, numbers that range from 1000 to 9999), you call the method with a `minValue` value of 1000 and a `maxValue` value of 10000, as the following example shows.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/range2.cs" id="Snippet16":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/range2.fs" id="Snippet16":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/range2.vb" id="Snippet16":::
+
+## Retrieve floating-point values in a specified range
+
+The method returns random floating-point values that range from 0 to less than 1. However, you'll often want to generate random values in some other range.
+
+If the interval between the minimum and maximum desired values is 1, you can add the difference between the desired starting interval and 0 to the number returned by the method. The following example does this to generate 10 random numbers between -1 and 0.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/doublerange2.cs" id="Snippet17":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/doublerange2.fs" id="Snippet17":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/doublerange2.vb" id="Snippet17":::
+
+To generate random floating-point numbers whose lower bound is 0 but upper bound is greater than 1 (or, in the case of negative numbers, whose lower bound is less than -1 and upper bound is 0), multiply the random number by the non-zero bound. The following example does this to generate 20 million random floating-point numbers that range from 0 to . In also displays the distribution of the random values generated by the method.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/doublerange1.cs" id="Snippet18":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/doublerange1.fs" id="Snippet18":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/doublerange1.vb" id="Snippet18":::
+
+To generate random floating-point numbers between two arbitrary values, like the method does for integers, use the following formula:
+
+```csharp
+Random.NextDouble() * (maxValue - minValue) + minValue
+```
+
+The following example generates 1 million random numbers that range from 10.0 to 11.0, and displays their distribution.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/doublerange3.cs" id="Snippet19":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/doublerange3.fs" id="Snippet19":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/doublerange3.vb" id="Snippet19":::
+
+## Generate random Boolean values
+
+The class doesn't provide methods that generate values. However, you can define your own class or method to do that. The following example defines a class, `BooleanGenerator`, with a single method, `NextBoolean`. The `BooleanGenerator` class stores a object as a private variable. The `NextBoolean` method calls the method and passes the result to the method. Note that 2 is used as the argument to specify the upper bound of the random number. Since this is an exclusive value, the method call returns either 0 or 1.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/booleans1.cs" id="Snippet8":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/booleans1.fs" id="Snippet8":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/booleans1.vb" id="Snippet8":::
+
+Instead of creating a separate class to generate random values, the example could simply have defined a single method. In that case, however, the object should have been defined as a class-level variable to avoid instantiating a new instance in each method call. In Visual Basic, the Random instance can be defined as a [Static](/dotnet/visual-basic/language-reference/modifiers/static) variable in the `NextBoolean` method. The following example provides an implementation.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/booleans2.cs" id="Snippet20":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/booleans2.fs" id="Snippet20":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/booleans2.vb" id="Snippet20":::
+
+## Generate random 64-bit integers
+
+The overloads of the method return 32-bit integers. However, in some cases, you might want to work with 64-bit integers. You can do this as follows:
+
+1. Call the method to retrieve a double-precision floating point value.
+
+2. Multiply that value by .
+
+The following example uses this technique to generate 20 million random long integers and categorizes them in 10 equal groups. It then evaluates the distribution of the random numbers by counting the number in each group from 0 to . As the output from the example shows, the numbers are distributed more or less equally through the range of a long integer.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/long1.cs" id="Snippet14":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/long1.fs" id="Snippet14":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/long1.vb" id="Snippet14":::
+
+An alternative technique that uses bit manipulation does not generate truly random numbers. This technique calls to generate two integers, left-shifts one by 32 bits, and ORs them together. This technique has two limitations:
+
+1. Because bit 31 is the sign bit, the value in bit 31 of the resulting long integer is always 0. This can be addressed by generating a random 0 or 1, left-shifting it 31 bits, and ORing it with the original random long integer.
+
+2. More seriously, because the probability that the value returned by will be 0, there will be few if any random numbers in the range 0x0-0x00000000FFFFFFFF.
+
+## Retrieve bytes in a specified range
+
+The overloads of the method allow you to specify the range of random numbers, but the method does not. The following example implements a `NextBytes` method that lets you specify the range of the returned bytes. It defines a `Random2` class that derives from and overloads its `NextBytes` method.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/bytes1.cs" id="Snippet9":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/bytes1.fs" id="Snippet9":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/bytes1.vb" id="Snippet9":::
+
+The `NextBytes(Byte[], Byte, Byte)` method wraps a call to the method and specifies the minimum value and one greater than the maximum value (in this case, 0 and 101) that we want returned in the byte array. Because we are sure that the integer values returned by the method are within the range of the data type, we can safely cast them (in C# and F#) or convert them (in Visual Basic) from integers to bytes.
+
+## Retrieve an element from an array or collection at random
+
+Random numbers often serve as indexes to retrieve values from arrays or collections. To retrieve a random index value, you can call the method, and use the lower bound of the array as the value of its `minValue` argument and one greater than the upper bound of the array as the value of its `maxValue` argument. For a zero-based array, this is equivalent to its property, or one greater than the value returned by the method. The following example randomly retrieves the name of a city in the United States from an array of cities.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/array1.cs" id="Snippet10":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/array1.fs" id="Snippet10":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/array1.vb" id="Snippet10":::
+
+## Retrieve a unique element from an array or collection
+
+A random number generator can always return duplicate values. As the range of numbers becomes smaller or the number of values generated becomes larger, the probability of duplicates grows. If random values must be unique, more numbers are generated to compensate for duplicates, resulting in increasingly poor performance.
+
+There are a number of techniques to handle this scenario. One common solution is to create an array or collection that contains the values to be retrieved, and a parallel array that contains random floating-point numbers. The second array is populated with random numbers at the time the first array is created, and the method is used to sort the first array by using the values in the parallel array.
+
+For example, if you're developing a Solitaire game, you want to ensure that each card is used only once. Instead of generating random numbers to retrieve a card and tracking whether that card has already been dealt, you can create a parallel array of random numbers that can be used to sort the deck. Once the deck is sorted, your app can maintain a pointer to indicate the index of the next card on the deck.
+
+The following example illustrates this approach. It defines a `Card` class that represents a playing card and a `Dealer` class that deals a deck of shuffled cards. The `Dealer` class constructor populates two arrays: a `deck` array that has class scope and that represents all the cards in the deck; and a local `order` array that has the same number of elements as the `deck` array and is populated with randomly generated values. The method is then called to sort the `deck` array based on the values in the `order` array.
+
+:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/uniquearray1.cs" id="Snippet11":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Random/Overview/uniquearray1.fs" id="Snippet11":::
+:::code language="vb" source="~/snippets/visualbasic/System/Random/Overview/uniquearray1.vb" id="Snippet11":::
+]]>
Represents a single-precision floating-point number.
- For more information about this API, see Supplemental API remarks for Single.
+
+ value type represents a single-precision 32-bit number with values ranging from negative 3.402823e38 to positive 3.402823e38, as well as positive or negative zero, , , and not a number (). It is intended to represent values that are extremely large (such as distances between planets or galaxies) or extremely small (such as the molecular mass of a substance in kilograms) and that often are imprecise (such as the distance from earth to another solar system). The type complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic.
+
+ provides methods to compare instances of this type, to convert the value of an instance to its string representation, and to convert the string representation of a number to an instance of this type. For information about how format specification codes control the string representation of value types, see [Formatting Types](/dotnet/standard/base-types/formatting-types), [Standard Numeric Format Strings](/dotnet/standard/base-types/standard-numeric-format-strings), and [Custom Numeric Format Strings](/dotnet/standard/base-types/custom-numeric-format-strings).
+
+## Floating-point representation and precision
+
+The data type stores single-precision floating-point values in a 32-bit binary format, as shown in the following table:
+
+| Part | Bits |
+|-----------------------------------|-------|
+| Significand or mantissa | 0-22 |
+| Exponent | 23-30 |
+| Sign (0 = positive, 1 = negative) | 31 |
+
+Just as decimal fractions are unable to precisely represent some fractional values (such as 1/3 or ), binary fractions are unable to represent some fractional values. For example, 2/10, which is represented precisely by .2 as a decimal fraction, is represented by .0011111001001100 as a binary fraction, with the pattern "1100" repeating to infinity. In this case, the floating-point value provides an imprecise representation of the number that it represents. Performing additional mathematical operations on the original floating-point value often increases its lack of precision. For example, if you compare the results of multiplying .3 by 10 and adding .3 to .3 nine times, you will see that addition produces the less precise result, because it involves eight more operations than multiplication. Note that this disparity is apparent only if you display the two values by using the "R" [standard numeric format string](/dotnet/standard/base-types/standard-numeric-format-strings), which, if necessary, displays all 9 digits of precision supported by the type.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/representation1.cs" id="Snippet3":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/representation1.fs" id="Snippet3":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/representation1.vb" id="Snippet3":::
+
+Because some numbers cannot be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers.
+
+All floating-point numbers have a limited number of significant digits, which also determines how accurately a floating-point value approximates a real number. A value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. This means that some floating-point operations may lack the precision to change a floating-point value. The following example defines a large single-precision floating-point value, and then adds the product of and one quadrillion to it. However, the product is too small to modify the original floating-point value. Its least significant digit is thousandths, whereas the most significant digit in the product is 10-30.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/representation2.cs" id="Snippet4":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/representation2.fs" id="Snippet4":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/representation2.vb" id="Snippet4":::
+
+The limited precision of a floating-point number has several consequences:
+
+- Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different. In the following example, a series of numbers are added together, and their total is compared with their expected total. A call to the `Equals` method indicates that the values aren't equal.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/precisionlist3.cs" id="Snippet6":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/precisionlist3.fs" id="Snippet6":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/precisionlist3.vb" id="Snippet6":::
+
+ The two values are unequal because of a loss of precision during the addition operations. In this case, the issue can be resolved by calling the method to round the values to the desired precision before performing the comparison.
+
+- A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used, because the binary floating-point number might not equal the decimal number. A previous example illustrated this by displaying the result of multiplying .3 by 10 and adding .3 to .3 nine times.
+
+ When accuracy in numeric operations with fractional values is important, use the type instead of the type. When accuracy in numeric operations with integral values beyond the range of the or types is important, use the type.
+
+- values have less precision than values. A value that is converted to a seemingly equivalent often does not equal the value because of differences in precision. In the following example, the result of identical division operations is assigned to a value and a value. After the value is cast to a , a comparison of the two values shows that they are unequal.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Double/Overview/precisionlist1.cs" id="Snippet5":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Double/Overview/precisionlist1.fs" id="Snippet5":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Double/Overview/precisionlist1.vb" id="Snippet5":::
+
+ To avoid this problem, either use the data type in place of the data type, or use the method so that both values have the same precision.
+
+## Test for equality
+
+To be considered equal, two values must represent identical values. However, because of differences in precision between values, or because of a loss of precision by one or both values, floating-point values that are expected to be identical often turn out to be unequal due to differences in their least significant digits. As a result, calls to the method to determine whether two values are equal, or calls to the method to determine the relationship between two values, often yield unexpected results. This is evident in the following example, where two apparently equal values turn out to be unequal, because the first value has 7 digits of precision, whereas the second value has 9.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison1.cs" id="Snippet9":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison1.fs" id="Snippet9":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/comparison1.vb" id="Snippet9":::
+
+Calculated values that follow different code paths and that are manipulated in different ways often prove to be unequal. In the following example, one value is squared, and then the square root is calculated to restore the original value. A second is multiplied by 3.51 and squared before the square root of the result is divided by 3.51 to restore the original value. Although the two values appear to be identical, a call to the method indicates that they are not equal.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison2.cs" id="Snippet10":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison2.fs" id="Snippet10":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/comparison2.vb" id="Snippet10":::
+
+In cases where a loss of precision is likely to affect the result of a comparison, you can use the following techniques instead of calling the or method:
+
+- Call the method to ensure that both values have the same precision. The following example modifies a previous example to use this approach so that two fractional values are equivalent.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison3.cs" id="Snippet11":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison3.fs" id="Snippet11":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/comparison3.vb" id="Snippet11":::
+
+ The problem of precision still applies to rounding of midpoint values. For more information, see the method.
+
+- Test for approximate equality instead of equality. This technique requires that you define either an absolute amount by which the two values can differ but still be equal, or that you define a relative amount by which the smaller value can diverge from the larger value.
+
+ > [!WARNING]
+ > is sometimes used as an absolute measure of the distance between two values when testing for equality. However, measures the smallest possible value that can be added to, or subtracted from, a whose value is zero. For most positive and negative values, the value of is too small to be detected. Therefore, except for values that are zero, we do not recommend its use in tests for equality.
+
+ The following example uses the latter approach to define an `IsApproximatelyEqual` method that tests the relative difference between two values. It also contrasts the result of calls to the `IsApproximatelyEqual` method and the method.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/comparison4.cs" id="Snippet12":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/comparison4.fs" id="Snippet12":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/comparison4.vb" id="Snippet12":::
+
+## Floating-point values and exceptions
+
+Operations with floating-point values do not throw exceptions, unlike operations with integral types, which throw exceptions in cases of illegal operations such as division by zero or overflow. Instead, in these situations, the result of a floating-point operation is zero, positive infinity, negative infinity, or not a number (NaN):
+
+- If the result of a floating-point operation is too small for the destination format, the result is zero. This can occur when two very small floating-point numbers are multiplied, as the following example shows.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/exceptional1.cs" id="Snippet1":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/exceptional1.fs" id="Snippet1":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/exceptional1.vb" id="Snippet1":::
+
+- If the magnitude of the result of a floating-point operation exceeds the range of the destination format, the result of the operation is or , as appropriate for the sign of the result. The result of an operation that overflows is , and the result of an operation that overflows is , as the following example shows.
+
+ :::code language="csharp" source="~/snippets/csharp/System/Single/Overview/exceptional2.cs" id="Snippet2":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/exceptional2.fs" id="Snippet2":::
+ :::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/exceptional2.vb" id="Snippet2":::
+
+ also results from a division by zero with a positive dividend, and results from a division by zero with a negative dividend.
+
+- If a floating-point operation is invalid, the result of the operation is . For example, results from the following operations:
+
+ - Division by zero with a dividend of zero. Note that other cases of division by zero result in either or .
+ - Any floating-point operation with invalid input. For example, attempting to find the square root of a negative value returns .
+ - Any operation with an argument whose value is .
+
+## Type conversions
+
+The structure does not define any explicit or implicit conversion operators; instead, conversions are implemented by the compiler.
+
+The following table lists the possible conversions of a value of the other primitive numeric types to a value. It also indicates whether the conversion is widening or narrowing and whether the resulting may have less precision than the original value.
+
+| Conversion from | Widening/narrowing | Possible loss of precision |
+|---------------------|--------------------|----------------------------|
+| | Widening | No |
+||Widening
Note that C# requires a cast operator.|Yes. supports 29 decimal digits of precision; supports 9.|
+||Narrowing; out-of-range values are converted to or .|Yes. supports 17 decimal digits of precision; supports 9.|
+| | Widening | No |
+||Widening|Yes. supports 10 decimal digits of precision; supports 9.|
+||Widening|Yes. supports 19 decimal digits of precision; supports 9.|
+||Widening|No|
+||Widening|No|
+||Widening|Yes. supports 10 decimal digits of precision; supports 9.|
+||Widening|Yes. supports 20 decimal digits of precision; supports 9.|
+
+The following example converts the minimum or maximum value of other primitive numeric types to a value.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/convert1.cs" id="Snippet20":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/convert1.fs" id="Snippet20":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/convert1.vb" id="Snippet20":::
+
+In addition, the values , , and convert to , , and , respectively.
+
+Note that the conversion of the value of some numeric types to a value can involve a loss of precision. As the example illustrates, a loss of precision is possible when converting , , , , , and values to values.
+
+The conversion of a value to a is a widening conversion. The conversion may result in a loss of precision if the type does not have a precise representation for the value.
+
+The conversion of a value to a value of any primitive numeric data type other than a is a narrowing conversion and requires a cast operator (in C#) or a conversion method (in Visual Basic). Values that are outside the range of the target data type, which are defined by the target type's `MinValue` and `MaxValue` properties, behave as shown in the following table.
+
+| Target type | Result |
+|-----------------------|-----------------------------------------------|
+| Any integral type | An exception if the conversion occurs in a checked context.
If the conversion occurs in an unchecked context (the default in C#), the conversion operation succeeds but the value overflows. |
+| | An exception. |
+
+In addition, , , and throw an for conversions to integers in a checked context, but these values overflow when converted to integers in an unchecked context. For conversions to , they always throw an . For conversions to , they convert to , , and , respectively.
+
+Note that a loss of precision may result from converting a value to another numeric type. In the case of converting non-integral values, as the output from the example shows, the fractional component is lost when the value is either rounded (as in Visual Basic) or truncated (as in C# and F#). For conversions to values, the value may not have a precise representation in the target data type.
+
+The following example converts a number of values to several other numeric types. The conversions occur in a checked context in Visual Basic (the default), in C# (because of the [checked](/dotnet/csharp/language-reference/keywords/checked) keyword), and in F# (because of the `open Checked` statement). The output from the example shows the result for conversions in both a checked an unchecked context. You can perform conversions in an unchecked context in Visual Basic by compiling with the `/removeintchecks+` compiler switch, in C# by commenting out the `checked` statement, and in F# by commenting out the `open Checked` statement.
+
+:::code language="csharp" source="~/snippets/csharp/System/Single/Overview/convert2.cs" id="Snippet21":::
+:::code language="fsharp" source="~/snippets/fsharp/System/Single/Overview/convert2.fs" id="Snippet21":::
+:::code language="vb" source="~/snippets/visualbasic/System/Single/Overview/convert2.vb" id="Snippet21":::
+
+For more information on the conversion of numeric types, see [Type Conversion in .NET](/dotnet/standard/base-types/type-conversion) and [Type Conversion Tables](/dotnet/standard/base-types/conversion-tables).
+
+## Floating-point functionality
+
+The structure and related types provide methods to perform the following categories of operations:
+
+- **Comparison of values**. You can call the method to determine whether two values are equal, or the method to determine the relationship between two values.
+
+ The structure also supports a complete set of comparison operators. For example, you can test for equality or inequality, or determine whether one value is greater than or equal to another value. If one of the operands is a , the value is converted to a before performing the comparison. If one of the operands is an integral type, it is converted to a before performing the comparison. Although these are widening conversions, they may involve a loss of precision.
+
+ > [!WARNING]
+ > Because of differences in precision, two values that you expect to be equal may turn out to be unequal, which affects the result of the comparison. See the [Test for equality](#test-for-equality) section for more information about comparing two values.
+
+ You can also call the