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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions snippets/csharp/System/Int32/Overview/Formatting1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;

public class Example1
{
public static void Main()
{
CallToString();
Console.WriteLine("-----");
CallConvert();
}

private static void CallToString()
{
// <Snippet1>
int[] numbers = { -1403, 0, 169, 1483104 };
foreach (int number in numbers)
{
// Display value using default formatting.
Console.Write("{0,-8} --> ", number.ToString());
// Display value with 3 digits and leading zeros.
Console.Write("{0,11:D3}", number);
// Display value with 1 decimal digit.
Console.Write("{0,13:N1}", number);
// Display value as hexadecimal.
Console.Write("{0,12:X2}", number);
// Display value with eight hexadecimal digits.
Console.WriteLine("{0,14:X8}", number);
}
// The example displays the following output:
// -1403 --> -1403 -1,403.0 FFFFFA85 FFFFFA85
// 0 --> 000 0.0 00 00000000
// 169 --> 169 169.0 A9 000000A9
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
// </Snippet1>
}

private static void CallConvert()
{
// <Snippet2>
int[] numbers = { -146, 11043, 2781913 };
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}",
"Value", "Binary", "Octal", "Hex");
foreach (int number in numbers)
{
Console.WriteLine("{0,8} {1,32} {2,11} {3,10}",
number, Convert.ToString(number, 2),
Convert.ToString(number, 8),
Convert.ToString(number, 16));
}
// The example displays the following output:
// Value Binary Octal Hex
// -146 11111111111111111111111101101110 37777777556 ffffff6e
// 11043 10101100100011 25443 2b23
// 2781913 1010100111001011011001 12471331 2a72d9
// </Snippet2>
}
}
105 changes: 105 additions & 0 deletions snippets/csharp/System/Int32/Overview/Instantiate1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Numerics;

public class Example
{
public static void Main()
{
InstantiateByAssignment();
Console.WriteLine("----");
InstantiateByNarrowingConversion();
Console.WriteLine("----");
Parse();
Console.WriteLine("----");
InstantiateByWideningConversion();
}

private static void InstantiateByAssignment()
{
// <Snippet1>
int number1 = 64301;
int number2 = 25548612;
// </Snippet1>
Console.WriteLine($"{number1} - {number2}");
}

private static void InstantiateByNarrowingConversion()
{
// <Snippet2>
long lNumber = 163245617;
try {
int number1 = (int) lNumber;
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine($"{lNumber} is out of range of an Int32.");
}

double dbl2 = 35901.997;
try {
int number2 = (int) dbl2;
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine($"{dbl2} is out of range of an Int32.");
}

BigInteger bigNumber = 132451;
try {
int number3 = (int) bigNumber;
Console.WriteLine(number3);
}
catch (OverflowException) {
Console.WriteLine($"{bigNumber} is out of range of an Int32.");
}
// The example displays the following output:
// 163245617
// 35902
// 132451
// </Snippet2>
}

private static void Parse()
{
// <Snippet3>
string string1 = "244681";
try {
int number1 = Int32.Parse(string1);
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine($"'{string1}' is out of range of a 32-bit integer.");
}
catch (FormatException) {
Console.WriteLine($"The format of '{string1}' is invalid.");
}

string string2 = "F9A3C";
try {
int number2 = Int32.Parse(string2,
System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine($"'{string2}' is out of range of a 32-bit integer.");
}
catch (FormatException) {
Console.WriteLine($"The format of '{string2}' is invalid.");
}
// The example displays the following output:
// 244681
// 1022524
// </Snippet3>
}

private static void InstantiateByWideningConversion()
{
// <Snippet4>
sbyte value1 = 124;
short value2 = 1618;

int number1 = value1;
int number2 = value2;
// </Snippet4>
}
}
8 changes: 8 additions & 0 deletions snippets/csharp/System/Int32/Overview/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions snippets/csharp/System/Int64/Overview/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
65 changes: 65 additions & 0 deletions snippets/csharp/System/Int64/Overview/formatting1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;

public class Example1
{
public static void Main()
{
CallToString();
Console.WriteLine("-----");
CallConvert();
}

private static void CallToString()
{
// <Snippet1>
long[] numbers = { -1403, 0, 169, 1483104 };
foreach (var number in numbers)
{
// Display value using default formatting.
Console.Write("{0,-8} --> ", number.ToString());
// Display value with 3 digits and leading zeros.
Console.Write("{0,8:D3}", number);
// Display value with 1 decimal digit.
Console.Write("{0,13:N1}", number);
// Display value as hexadecimal.
Console.Write("{0,18:X2}", number);
// Display value with eight hexadecimal digits.
Console.WriteLine("{0,18:X8}", number);
}
// The example displays the following output:
// -1403 --> -1403 -1,403.0 FFFFFFFFFFFFFA85 FFFFFFFFFFFFFA85
// 0 --> 000 0.0 00 00000000
// 169 --> 169 169.0 A9 000000A9
// 1483104 --> 1483104 1,483,104.0 16A160 0016A160
// </Snippet1>
}

private static void CallConvert()
{
// <Snippet2>
long[] numbers = { -146, 11043, 2781913 };
foreach (var number in numbers)
{
Console.WriteLine($"{number} (Base 10):");
Console.WriteLine($" Binary: {Convert.ToString(number, 2)}");
Console.WriteLine($" Octal: {Convert.ToString(number, 8)}");
Console.WriteLine($" Hex: {Convert.ToString(number, 16)}{Environment.NewLine}");
}
// The example displays the following output:
// -146 (Base 10):
// Binary: 1111111111111111111111111111111111111111111111111111111101101110
// Octal: 1777777777777777777556
// Hex: ffffffffffffff6e
//
// 11043 (Base 10):
// Binary: 10101100100011
// Octal: 25443
// Hex: 2b23
//
// 2781913 (Base 10):
// Binary: 1010100111001011011001
// Octal: 12471331
// Hex: 2a72d9
// </Snippet2>
}
}
107 changes: 107 additions & 0 deletions snippets/csharp/System/Int64/Overview/instantiate1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Numerics;

public class Example
{
public static void Main()
{
InstantiateByAssignment();
Console.WriteLine("----");
InstantiateByNarrowingConversion();
Console.WriteLine("----");
Parse();
Console.WriteLine("----");
InstantiateByWideningConversion();
}

private static void InstantiateByAssignment()
{
// <Snippet1>
long number1 = -64301728;
long number2 = 255486129307;
// </Snippet1>
Console.WriteLine($"{number1} - {number2}");
}

private static void InstantiateByNarrowingConversion()
{
// <Snippet2>
ulong ulNumber = 163245617943825;
try {
long number1 = (long) ulNumber;
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine($"{ulNumber} is out of range of an Int64.");
}

double dbl2 = 35901.997;
try {
long number2 = (long) dbl2;
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine($"{dbl2} is out of range of an Int64.");
}

BigInteger bigNumber = (BigInteger) 1.63201978555e30;
try {
long number3 = (long) bigNumber;
Console.WriteLine(number3);
}
catch (OverflowException) {
Console.WriteLine($"{bigNumber} is out of range of an Int64.");
}
// The example displays the following output:
// 163245617943825
// 35902
// 1,632,019,785,549,999,969,612,091,883,520 is out of range of an Int64.
// </Snippet2>
}

private static void Parse()
{
// <Snippet3>
string string1 = "244681903147";
try {
long number1 = Int64.Parse(string1);
Console.WriteLine(number1);
}
catch (OverflowException) {
Console.WriteLine($"'{string1}' is out of range of a 64-bit integer.");
}
catch (FormatException) {
Console.WriteLine($"The format of '{string1}' is invalid.");
}

string string2 = "F9A3CFF0A";
try {
long number2 = Int64.Parse(string2,
System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(number2);
}
catch (OverflowException) {
Console.WriteLine($"'{string2}' is out of range of a 64-bit integer.");
}
catch (FormatException) {
Console.WriteLine($"The format of '{string2}' is invalid.");
}
// The example displays the following output:
// 244681903147
// 67012198154
// </Snippet3>
}

private static void InstantiateByWideningConversion()
{
// <Snippet4>
sbyte value1 = 124;
short value2 = 1618;
int value3 = Int32.MaxValue;

long number1 = value1;
long number2 = value2;
long number3 = value3;
// </Snippet4>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// <Snippet3>
using System;
using System.Globalization;

public class InterfaceEx
{
public static void Main()
{
var culture = CultureInfo.InvariantCulture;
IFormatProvider provider = culture;

DateTimeFormatInfo dt = (DateTimeFormatInfo)provider;
}
}

// The example displays the following output:
// Unhandled Exception: System.InvalidCastException:
// Unable to cast object of type 'System.Globalization.CultureInfo' to
// type 'System.Globalization.DateTimeFormatInfo'.
// at Example.Main()
// </Snippet3>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// <Snippet4>
public class StringEx
{
public static void Main()
{
object value = 12;
// Cast throws an InvalidCastException exception.
string s = (string)value;
}
}
// </Snippet4>
Loading
Loading