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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BadCallExample.Run();
FixImportAssemblyExample.Run();
ImportAssemblyExample.Run();
MangleMissingExample.Run();
MangleFixedExample.Run();
NoFunctionExample.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@
using System;
using System.Runtime.InteropServices;

public class Example
public class BadCallExample
{
[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )]
public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern int MessageBox(IntPtr hwnd, string text, string caption, uint type);

[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true )]
public static extern int MessageBoxW(IntPtr hwnd, String text, String caption, uint type);
[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern int MessageBoxW(IntPtr hwnd, string text, string caption, uint type);

public static void Main()
{
try {
MessageBox(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
}
catch (EntryPointNotFoundException e) {
Console.WriteLine("{0}:\n {1}", e.GetType().Name,
e.Message);
}
public static void Run()
{
try
{
MessageBox(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
}
catch (EntryPointNotFoundException e)
{
Console.WriteLine($"{e.GetType().Name}:\n {e.Message}");
}

try {
MessageBoxW(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
}
catch (EntryPointNotFoundException e) {
Console.WriteLine("{0}:\n {1}", e.GetType().Name,
e.Message);
}
}
try
{
MessageBoxW(new IntPtr(0), "Calling the MessageBox Function", "Example", 0);
}
catch (EntryPointNotFoundException e)
{
Console.WriteLine($"{e.GetType().Name}:\n {e.Message}");
}
}
}
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// <Snippet5>
using System;

public class Example
public class FixImportAssemblyExample
{
public static void Main()
{
Console.WriteLine(StringUtilities.SayGoodMorning("Dakota"));
}
public static void Run() => Console.WriteLine(StringUtilities.SayGoodMorning("Dakota"));
}
// The example displays the following output:
// A top of the morning to you, Dakota!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
using System;
using System.Runtime.InteropServices;

public class Example
public class ImportAssemblyExample
{
[DllImport("StringUtilities.dll", CharSet = CharSet.Unicode )]
public static extern String SayGoodMorning(String name);
[DllImport("StringUtilities.dll", CharSet = CharSet.Unicode)]
public static extern string SayGoodMorning(string name);

public static void Main()
{
Console.WriteLine(SayGoodMorning("Dakota"));
}
public static void Run() => Console.WriteLine(SayGoodMorning("Dakota"));
}
// The example displays the following output:
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
using System;
using System.Runtime.InteropServices;

public class Example
public class MangleMissingExample
{
[DllImport("TestDll.dll")]
public static extern int Double(int number);
[DllImport("TestDll.dll")]
public static extern int Double(int number);

public static void Main()
{
Console.WriteLine(Double(10));
}
public static void Run() => Console.WriteLine(Double(10));
}
// The example displays the following output:
// Unhandled Exception: System.EntryPointNotFoundException: Unable to find
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
using System;
using System.Runtime.InteropServices;

public class Example
public class MangleFixedExample
{
[DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")]
public static extern int Double(int number);
[DllImport("TestDll.dll", EntryPoint = "?Double@@YAHH@Z")]
public static extern int Double(int number);

public static void Main()
{
Console.WriteLine(Double(10));
}
public static void Run() => Console.WriteLine(Double(10));
}
// The example displays the following output:
// 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
using System;
using System.Runtime.InteropServices;

public class Example
public class NoFunctionExample
{
[DllImport("user32.dll")]
public static extern int GetMyNumber();
[DllImport("user32.dll")]
public static extern int GetMyNumber();

public static void Main()
{
try {
int number = GetMyNumber();
}
catch (EntryPointNotFoundException e) {
Console.WriteLine("{0}:\n {1}", e.GetType().Name,
e.Message);
}
}
public static void Run()
{
try
{
int number = GetMyNumber();
}
catch (EntryPointNotFoundException e)
{
Console.WriteLine($"{e.GetType().Name}:\n {e.Message}");
}
}
}
// The example displays the following output:
// EntryPointNotFoundException:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

public static class StringUtilities
{
public static String SayGoodMorning(String name)
{
return String.Format("A top of the morning to you, {0}!", name);
}
public static string SayGoodMorning(string name) => $"A top of the morning to you, {name}!";
}
// </Snippet3>
// </Snippet3>
16 changes: 9 additions & 7 deletions snippets/csharp/System/Enum/CompareTo/EnumCompareTo.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
//<snippet1>
using System;

public class CompareToTest {
public class CompareToTest
{
enum VehicleDoors { Motorbike = 0, Sportscar = 2, Sedan = 4, Hatchback = 5 };

public static void Main() {
public static void Main()
{
VehicleDoors myVeh = VehicleDoors.Sportscar;
VehicleDoors yourVeh = VehicleDoors.Motorbike;
VehicleDoors otherVeh = VehicleDoors.Sedan;

Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh);
Console.WriteLine( "{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", Environment.NewLine );
Console.WriteLine($"Does a {myVeh} have more doors than a {yourVeh}?");
Console.WriteLine($"{(myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No")}{Environment.NewLine}");

Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh);
Console.WriteLine( "{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );
Console.WriteLine($"Does a {myVeh} have more doors than a {otherVeh}?");
Console.WriteLine($"{(myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No")}");
}
}
// The example displays the following output:
Expand All @@ -22,4 +24,4 @@ public static void Main() {
//
// Does a Sportscar have more doors than a Sedan?
// No
//</snippet1>
//</snippet1>
27 changes: 14 additions & 13 deletions snippets/csharp/System/Enum/Equals/EnumEquals.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
//<snippet1>
using System;

public class EqualsTest {
public class EqualsTest
{
enum Colors { Red, Green, Blue, Yellow };
enum Mammals { Cat, Dog, Horse, Dolphin };

public static void Main() {
public static void Main()
{
Mammals myPet = Mammals.Cat;
Colors myColor = Colors.Red;
Mammals yourPet = Mammals.Dog;
Colors yourColor = Colors.Red;

Console.WriteLine("My favorite animal is a {0}", myPet);
Console.WriteLine("Your favorite animal is a {0}", yourPet);
Console.WriteLine("Do we like the same animal? {0}", myPet.Equals(yourPet) ? "Yes" : "No");
Console.WriteLine($"My favorite animal is a {myPet}");
Console.WriteLine($"Your favorite animal is a {yourPet}");
Console.WriteLine($"Do we like the same animal? {(myPet.Equals(yourPet) ? "Yes" : "No")}");

Console.WriteLine();
Console.WriteLine("My favorite color is {0}", myColor);
Console.WriteLine("Your favorite color is {0}", yourColor);
Console.WriteLine("Do we like the same color? {0}", myColor.Equals(yourColor) ? "Yes" : "No");
Console.WriteLine($"My favorite color is {myColor}");
Console.WriteLine($"Your favorite color is {yourColor}");
Console.WriteLine($"Do we like the same color? {(myColor.Equals(yourColor) ? "Yes" : "No")}");

Console.WriteLine();
Console.WriteLine("The value of my color ({0}) is {1}", myColor, Enum.Format(typeof(Colors), myColor, "d"));
Console.WriteLine("The value of my pet (a {0}) is {1}", myPet, Enum.Format(typeof(Mammals), myPet, "d"));
Console.WriteLine("Even though they have the same value, are they equal? {0}",
myColor.Equals(myPet) ? "Yes" : "No");
Console.WriteLine($"The value of my color ({myColor}) is {Enum.Format(typeof(Colors), myColor, "d")}");
Console.WriteLine($"The value of my pet (a {myPet}) is {Enum.Format(typeof(Mammals), myPet, "d")}");
Console.WriteLine($"Even though they have the same value, are they equal? {(myColor.Equals(myPet) ? "Yes" : "No")}");
}
}
// The example displays the following output:
Expand All @@ -39,4 +40,4 @@ public static void Main() {
// The value of my color (Red) is 0
// The value of my pet (a Cat) is 0
// Even though they have the same value, are they equal? No
//</snippet1>
//</snippet1>
31 changes: 17 additions & 14 deletions snippets/csharp/System/Enum/Equals/enumequals1.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
// <Snippet1>
using System;

public enum SledDog { Unknown=0, AlaskanMalamute=1, Malamute=1,
Husky=2, SiberianHusky=2 };
public enum SledDog
{
Unknown = 0, AlaskanMalamute = 1, Malamute = 1,
Husky = 2, SiberianHusky = 2
};

public enum WorkDog { Unknown=0, Newfoundland=1, GreatPyrennes=2 };
public enum WorkDog { Unknown = 0, Newfoundland = 1, GreatPyrennes = 2 };

public class Example
{
public static void Main()
{
SledDog dog1 = SledDog.Malamute;
SledDog dog2 = SledDog.AlaskanMalamute;
WorkDog dog3 = WorkDog.Newfoundland;
public static void Main()
{
SledDog dog1 = SledDog.Malamute;
SledDog dog2 = SledDog.AlaskanMalamute;
WorkDog dog3 = WorkDog.Newfoundland;

Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}",
dog1, dog2, dog1.Equals(dog2));
Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}",
dog1, dog3, dog1.Equals(dog3));
}
Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}",
dog1, dog2, dog1.Equals(dog2));
Console.WriteLine("{0:F} ({0:D}) = {1:F} ({1:D}): {2}",
dog1, dog3, dog1.Equals(dog3));
}
}
// The example displays the following output:
// Malamute (1) = Malamute (1): True
// Malamute (1) = Newfoundland (1): False
// </Snippet1>
// </Snippet1>
12 changes: 7 additions & 5 deletions snippets/csharp/System/Enum/Format/EnumFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

enum Colors { Red, Green, Blue, Yellow };

public class FormatTest {
public static void Main() {
public class FormatTest
{
public static void Main()
{
Colors myColor = Colors.Blue;

Console.WriteLine("My favorite color is {0}.", myColor);
Console.WriteLine("The value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "d"));
Console.WriteLine("The hex value of my favorite color is {0}.", Enum.Format(typeof(Colors), myColor, "x"));
Console.WriteLine($"My favorite color is {myColor}.");
Console.WriteLine($"The value of my favorite color is {Enum.Format(typeof(Colors), myColor, "d")}.");
Console.WriteLine($"The hex value of my favorite color is {Enum.Format(typeof(Colors), myColor, "x")}.");
}
}
// The example displays the following output:
Expand Down
10 changes: 6 additions & 4 deletions snippets/csharp/System/Enum/GetName/EnumGetName.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//<Snippet1>
using System;

public class GetNameTest {
public class GetNameTest
{
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };

public static void Main() {
public static void Main()
{

Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
Console.WriteLine($"The 4th value of the Colors Enum is {Enum.GetName(typeof(Colors), 3)}");
Console.WriteLine($"The 4th value of the Styles Enum is {Enum.GetName(typeof(Styles), 3)}");
}
}
// The example displays the following output:
Expand Down
Loading
Loading