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
37 changes: 37 additions & 0 deletions snippets/csharp/System/AppContext/Overview/V1/Example4.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <Snippet4>
using System;
using System.Reflection;

[assembly: AssemblyVersion("1.0.0.0")]

public static class StringLibrary1
{
public static int SubstringStartsAt(string fullString, string substr)
{
return fullString.IndexOf(substr, StringComparison.Ordinal);
}
}
// </Snippet4>

namespace App
{
// <Snippet5>
using System;

public class Example1
{
public static void Main()
{
string value = "The archaeologist";
string substring = "archæ";
int position = StringLibrary1.SubstringStartsAt(value, substring);
if (position >= 0)
Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}");
else
Console.WriteLine($"'{substring}' not found in '{value}'");
}
}
// The example displays the following output:
// 'archæ' not found in 'The archaeologist'
// </Snippet5>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
37 changes: 37 additions & 0 deletions snippets/csharp/System/AppContext/Overview/V2/Example6.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <Snippet6>
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.0.0.0")]

public static class StringLibrary2
{
public static int SubstringStartsAt(string fullString, string substr)
{
return fullString.IndexOf(substr, StringComparison.CurrentCulture);
}
}
// </Snippet6>

namespace App
{
// <Snippet7>
using System;

public class Example2
{
public static void Main()
{
string value = "The archaeologist";
string substring = "archæ";
int position = StringLibrary2.SubstringStartsAt(value, substring);
if (position >= 0)
Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}");
else
Console.WriteLine($"'{substring}' not found in '{value}'");
}
}
// The example displays the following output:
// 'archæ' found in 'The archaeologist' starting at position 4
// </Snippet7>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
41 changes: 41 additions & 0 deletions snippets/csharp/System/AppContext/Overview/V3/Example8.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <Snippet8>
using System;
using System.Reflection;

[assembly: AssemblyVersion("2.0.0.0")]

public static class StringLibrary
{
public static int SubstringStartsAt(string fullString, string substr)
{
bool flag;
if (AppContext.TryGetSwitch("StringLibrary.DoNotUseCultureSensitiveComparison", out flag) && flag == true)
return fullString.IndexOf(substr, StringComparison.Ordinal);
else
return fullString.IndexOf(substr, StringComparison.CurrentCulture);
}
}
// </Snippet8>

namespace App
{
// <Snippet9>
using System;

public class Example
{
public static void Main()
{
string value = "The archaeologist";
string substring = "archæ";
int position = StringLibrary.SubstringStartsAt(value, substring);
if (position >= 0)
Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}");
else
Console.WriteLine($"'{substring}' not found in '{value}'");
}
}
// The example displays the following output:
// 'archæ' found in 'The archaeologist' starting at position 4
// </Snippet9>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

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

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

public class Example1
{
public static void Main()
{
bool[] flags = { true, false };
foreach (var flag in flags)
{
// Get binary representation of flag.
Byte value = BitConverter.GetBytes(flag)[0];
Console.WriteLine($"Original value: {flag}");
Console.WriteLine($"Binary value: {value} ({GetBinaryString(value)})");
// Restore the flag from its binary representation.
bool newFlag = BitConverter.ToBoolean(new Byte[] { value }, 0);
Console.WriteLine($"Restored value: {newFlag}{Environment.NewLine}");
}
}

private static string GetBinaryString(Byte value)
{
string retVal = Convert.ToString(value, 2);
return new string('0', 8 - retVal.Length) + retVal;
}
}
// The example displays the following output:
// Original value: True
// Binary value: 1 (00000001)
// Restored value: True
//
// Original value: False
// Binary value: 0 (00000000)
// Restored value: False
// </Snippet1>
32 changes: 32 additions & 0 deletions snippets/csharp/System/Boolean/Overview/conversion1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <Snippet6>
using System;

public class Example2
{
public static void Main()
{
Byte byteValue = 12;
Console.WriteLine(Convert.ToBoolean(byteValue));
Byte byteValue2 = 0;
Console.WriteLine(Convert.ToBoolean(byteValue2));
int intValue = -16345;
Console.WriteLine(Convert.ToBoolean(intValue));
long longValue = 945;
Console.WriteLine(Convert.ToBoolean(longValue));
SByte sbyteValue = -12;
Console.WriteLine(Convert.ToBoolean(sbyteValue));
double dblValue = 0;
Console.WriteLine(Convert.ToBoolean(dblValue));
float sngValue = .0001f;
Console.WriteLine(Convert.ToBoolean(sngValue));
}
}
// The example displays the following output:
// True
// False
// True
// True
// True
// False
// True
// </Snippet6>
32 changes: 32 additions & 0 deletions snippets/csharp/System/Boolean/Overview/conversion3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <Snippet8>
using System;

public class Example3
{
public static void Main()
{
bool flag = true;

byte byteValue;
byteValue = Convert.ToByte(flag);
Console.WriteLine($"{flag} -> {byteValue}");

sbyte sbyteValue;
sbyteValue = Convert.ToSByte(flag);
Console.WriteLine($"{flag} -> {sbyteValue}");

double dblValue;
dblValue = Convert.ToDouble(flag);
Console.WriteLine($"{flag} -> {dblValue}");

int intValue;
intValue = Convert.ToInt32(flag);
Console.WriteLine($"{flag} -> {intValue}");
}
}
// The example displays the following output:
// True -> 1
// True -> 1
// True -> 1
// True -> 1
// </Snippet8>
64 changes: 64 additions & 0 deletions snippets/csharp/System/Boolean/Overview/format3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <Snippet5>
using System;
using System.Globalization;

public class Example4
{
public static void Main()
{
String[] cultureNames = { "", "en-US", "fr-FR", "ru-RU" };
foreach (var cultureName in cultureNames) {
bool value = true;
CultureInfo culture = CultureInfo.CreateSpecificCulture(cultureName);
BooleanFormatter formatter = new BooleanFormatter(culture);

string result = string.Format(formatter, "Value for '{0}': {1}", culture.Name, value);
Console.WriteLine(result);
}
}
}

public class BooleanFormatter : ICustomFormatter, IFormatProvider
{
private CultureInfo culture;

public BooleanFormatter() : this(CultureInfo.CurrentCulture)
{ }

public BooleanFormatter(CultureInfo culture)
{
this.culture = culture;
}

public Object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}

public string Format(string fmt, Object arg, IFormatProvider formatProvider)
{
// Exit if another format provider is used.
if (! formatProvider.Equals(this)) return null;

// Exit if the type to be formatted is not a Boolean
if (! (arg is Boolean)) return null;

bool value = (bool) arg;
return culture.Name switch
{
"en-US" => value.ToString(),
"fr-FR" => value ? "vrai" : "faux",
"ru-RU" => value ? "верно" : "неверно",
_ => value.ToString(),
};
}
}
// The example displays the following output:
// Value for '': True
// Value for 'en-US': True
// Value for 'fr-FR': vrai
// Value for 'ru-RU': верно
// </Snippet5>
Loading
Loading