Last active
January 31, 2020 17:29
-
-
Save geoffreymcgill/c7067a932ee184fb63b8153499b94be5 to your computer and use it in GitHub Desktop.
Deck Samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main() | |
{ | |
var msg = "Hello, World!"; | |
Console.WriteLine(msg); | |
// Try other C# samples from the | |
// "Samples" button in the top right | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Retyped; | |
using static Retyped.dom; | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Built with Retyped | |
// https://retyped.com | |
// Create the Button | |
var button = new HTMLButtonElement(); | |
// Set the Button text | |
button.innerText = "Click me!"; | |
// Add a Bootstrap style | |
button.className = "btn btn-primary"; | |
var count = 1; | |
// Add a click event handler | |
button.onclick += (e) => { | |
button.innerText = $"Clicked {count} times"; | |
// Write a message to Console | |
// on first Button click | |
if (count == 1) { | |
var msg = "Welcome to Bridge.NET"; | |
System.Console.WriteLine(msg); | |
} | |
count++; | |
}; | |
// Add the button to the document body | |
document.body.appendChild(button); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App | |
{ | |
public static void Main() | |
{ | |
// Simple array of strings | |
string[] names = { "Mildred", "Levi", "Nestor", "Clark", "Jamie" }; | |
// Query the array using LINQ | |
IEnumerable query = | |
from n in names | |
where n.Contains("i") | |
orderby n | |
select n; | |
// Loop through the items in the array | |
foreach (string q in query) | |
{ | |
Console.WriteLine(q); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App | |
{ | |
public static int count = 0; | |
public static void Main() | |
{ | |
var body = document.body; | |
var container = new HTMLDivElement | |
{ | |
className = "container-fluid" | |
}; | |
var title = new HTMLHeadingElement | |
{ | |
innerText = "Synchronous vs Asynchronous" | |
}; | |
container.appendChild(title); | |
container.appendChild(new HTMLParagraphElement | |
{ | |
innerHTML = "Each click of a Button will create a " + | |
"random delay, then log a message to the screen." | |
}); | |
container.appendChild(new HTMLParagraphElement | |
{ | |
innerHTML = "Step 1: Rapidly click the Synchronous " + | |
"Button, then wait a few moments for all log " + | |
"messages to appear.<br />Step 2: Rapidly click " + | |
"Asynchronous Button." | |
}); | |
container.appendChild(new HTMLParagraphElement | |
{ | |
innerHTML = "Notice difference in sequence of log " + | |
"index values as thread locking issues have " + | |
"been removed with use Async/Await.", | |
style = | |
{ | |
marginBottom = "30px" | |
} | |
}); | |
var row = new HTMLDivElement | |
{ | |
className = "row" | |
}; | |
var col1 = new HTMLDivElement | |
{ | |
id = "col1", | |
className = "col-xs-6 col-md-2" | |
}; | |
var btn1 = new HTMLButtonElement | |
{ | |
innerHTML = "Synchronous", | |
className = "btn btn-primary", | |
style = | |
{ | |
marginBottom = "20px" | |
}, | |
onclick = (ev) => | |
{ | |
Synchronous.LogData(App.Log); | |
} | |
}; | |
col1.appendChild(btn1); | |
var col2 = new HTMLDivElement | |
{ | |
id = "col2", | |
className = "col-xs-6 col-md-10" | |
}; | |
var btn2 = new HTMLButtonElement | |
{ | |
innerHTML = "Asynchronous", | |
className = "btn btn-success", | |
style = | |
{ | |
marginBottom = "20px" | |
}, | |
onclick = (ev) => | |
{ | |
Asynchronous.LogData(App.Log); | |
} | |
}; | |
col2.appendChild(btn2); | |
row.appendChild(col1); | |
row.appendChild(col2); | |
container.appendChild(row); | |
body.appendChild(container); | |
} | |
private static void Log(string selector, string msg) | |
{ | |
var div = new HTMLDivElement { innerHTML = msg }; | |
jQuery.select(selector).append(div); | |
} | |
} | |
public class Synchronous | |
{ | |
public static void LogData(Action<string, string> log) | |
{ | |
App.count++; | |
var data = Synchronous.LongRunningProcess(App.count); | |
log("#col1", data); | |
} | |
public static string LongRunningProcess(int id) | |
{ | |
// Force a Delay | |
// Simulate a long running process | |
var delay = System.Math.Random() * 2000; | |
var end = DateTime.Now.AddMilliseconds((int)delay).Ticks; | |
while (DateTime.Now.Ticks < end) { } | |
return "Click " + id + ": " + System.Math.Floor(delay) + "ms"; | |
} | |
} | |
public class Asynchronous | |
{ | |
public static async void LogData(Action<string, string> log) | |
{ | |
App.count++; | |
var data = await Asynchronous.LongRunningProcess(App.count); | |
log("#col2", data); | |
} | |
public static async Task<string> LongRunningProcess(int id) | |
{ | |
// Force a Delay | |
// Simulate a long running process | |
var delay = System.Math.Random() * 2000; | |
await Task.Delay((int)delay); | |
return "Click " + id + ": " + System.Math.Floor(delay) + "ms"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class App | |
{ | |
public static void Main() | |
{ | |
// Click a Button and make a AJAX request | |
// to a service that returns a message. | |
// Make a Button | |
var button = new HTMLButtonElement | |
{ | |
innerHTML = "Click Me!", | |
className = "btn btn-primary", | |
onclick = (ev) => | |
{ | |
// A message for the prompt | |
var msg = "Enter a message"; | |
// Make a prompt and get the user input | |
var input = Console.ReadLine(msg); | |
// Send the input to a WebService | |
App.Bounce(input); | |
} | |
}; | |
// Add Button to the page | |
document.body.appendChild(button); | |
} | |
public static void Bounce(string msg) | |
{ | |
// Configure the Ajax request | |
var config = new JQueryAjaxSettings | |
{ | |
url = "../DeckServices.asmx/Bounce", | |
// Serialize the msg into json | |
data = new { value = JsonConvert.SerializeObject(msg) }, | |
// Set the contentType of the request | |
contentType = "application/json; charset=utf-8", | |
// On response, call custom success method | |
success = (data, textStatus, jqXHR) => | |
{ | |
// Output the whole response object. | |
Console.WriteLine(data); | |
// or, output just the message using | |
// the "d" property string indexer. | |
// Console.WriteLine(data["d"]); | |
} | |
}; | |
// Make the Ajax request | |
jQuery.ajax(config); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main() | |
{ | |
Product product = new Product(); | |
product.Name = "Apple"; | |
product.ExpiryDate = new DateTime(2008, 12, 28); | |
product.Price = 3.99; | |
product.Sizes = new string[] { "Small", "Medium", "Large" }; | |
string output = JsonConvert.SerializeObject(product, Formatting.Indented); | |
// Write the json string | |
Console.WriteLine(output); | |
// Deserialize the json back into a real Product | |
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output); | |
// Write the rehydrated values | |
var msg = $"An {deserializedProduct.Name} for ${deserializedProduct.Price}"; | |
Console.WriteLine(msg); | |
} | |
} | |
public class Product | |
{ | |
public string Name { get; set; } | |
public DateTime ExpiryDate { get; set; } | |
public double Price { get; set; } | |
public string[] Sizes { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using static System.Console; | |
// C#7 language samples | |
public class Program | |
{ | |
public static void Main() | |
{ | |
// Small helper functions available only in the scope of parent function | |
NestedFunctions(); | |
// Declare binary literals | |
BinaryLiterals(); | |
// Declare out variable inside the method call | |
OutVariables(); | |
// Declare tuples inline | |
EnhancedTuples(); | |
// Access tuple members directly without using tuple name | |
TupleDeconstruction(); | |
// Use expression bodied methods for properties, constructor and distructor | |
ExpressionBodiedMembers(); | |
// Check for variable type in switch cases | |
SwitchExpressions(); | |
// You can now throw exceptions inline in conditional expressions, null coalescing expressions, and some lambda expressions | |
ThrowExceptionsInExpressions(); | |
} | |
public static void NestedFunctions() | |
{ | |
WriteFeatureHeader("Nested functions", "Small helper functions available only in the scope of parent function"); | |
void Add(int x, int y) | |
{ | |
WriteLine($"Sum of {x} and {y} is : {x + y}"); | |
} | |
void Multiply(int x, int y) | |
{ | |
WriteLine($"Multiply of {x} and {y} is : {x * y}"); | |
Add(30, 10); | |
} | |
Add(20, 50); | |
Multiply(20, 50); | |
} | |
public static void BinaryLiterals() | |
{ | |
WriteFeatureHeader("Binary Literals", "Declare binary literals"); | |
// Represent 50 in decimal, hexadecimal & binary | |
int a = 50; // decimal representation of 50 | |
int b = 0X32; // hexadecimal representation of 50 | |
int c = 0B110010; //binary representation of 50 | |
// Represent 100 in decimal, hexadecimal & binary | |
int d = 50 * 2; // decimal represenation of 100 | |
int e = 0x32 * 2; // hexadecimal represenation of 100 | |
int f = 0b110010 * 2; //binary represenation of 100 | |
WriteLine($"a: {a: 0000} b: {b: 0000} c: {c: 0000}"); | |
WriteLine($"d: {d: 0000} e: {e: 0000} f: {f: 0000}"); | |
} | |
public static void OutVariables() | |
{ | |
WriteFeatureHeader("Enhanced Out Variables", "Declare out variable inside the method call"); | |
string s = "2016-11-26"; | |
if (DateTime.TryParse(s, out DateTime date)) | |
{ | |
WriteLine(date); | |
} | |
} | |
public static void EnhancedTuples() | |
{ | |
WriteFeatureHeader("Enhanced Tuples", "Declare tuples inline"); | |
var numbers = new List<int>{5, 10, 45, 23}; | |
var aggregate = Aggregate(numbers); | |
Console.WriteLine($"Count: {aggregate.count}, Sum: {aggregate.sum}, Avg: {aggregate.avg}"); | |
} | |
static (int count, int sum, double avg)Aggregate(IEnumerable<int> numbers) | |
{ | |
int count = numbers.Count(); | |
int sum = numbers.Sum(); | |
double avg = (double)sum / (double)count; | |
return (count, sum, avg); | |
} | |
public static void TupleDeconstruction() | |
{ | |
WriteFeatureHeader("Tuple Deconstruction", "Access tuple members directly without using tuple name"); | |
var numbers = new List<int>{5, 10, 45, 23}; | |
(int count, int sum, double avg) = Aggregate(numbers); | |
Console.WriteLine($"Count: {count}, Sum: {sum}, Avg: {avg}"); | |
} | |
public static void ExpressionBodiedMembers() | |
{ | |
WriteFeatureHeader("Expression Bodied Members", "Use expression bodied methods for properties, constructor and distructor"); | |
var customer = new Customer(); | |
customer.FirstName = " Joe"; | |
customer.LastName = " Doe "; | |
Console.WriteLine($"Customer Name: {customer.FullName}, Country: {customer.Country}"); | |
} | |
public static void SwitchExpressions() | |
{ | |
WriteFeatureHeader("Switch Expressions", "Check for variable type in switch cases"); | |
List<object> values = new List<object> | |
{ | |
0, | |
1, | |
new List<object> | |
{ | |
2, 3, 4 | |
}, | |
new List<object>(), | |
null, | |
5, | |
6 | |
}; | |
int sum = Sum(values); | |
Console.WriteLine($"Sum is {sum}"); | |
} | |
public static int Sum(IEnumerable<object> values) | |
{ | |
var sum = 0; | |
foreach (var item in values) | |
{ | |
switch (item) | |
{ | |
case 0: | |
break; | |
case int val: | |
sum += val; | |
break; | |
case IEnumerable<object> subList when subList.Any(): //use when to add extra switch condition | |
sum += Sum(subList); | |
break; | |
case IEnumerable<object> subList: | |
break; | |
case null: | |
break; | |
default: | |
throw new InvalidOperationException("unknown item type"); | |
} | |
} | |
return sum; | |
} | |
class Customer | |
{ | |
// Expression bodies constructor | |
public Customer() => Country = "USA"; | |
private string _firstName; | |
public string FirstName | |
{ | |
get => _firstName; //Use expression bodies methods for get/set | |
set => _firstName = value.Trim(); | |
} | |
private string _lastName; | |
public string LastName | |
{ | |
get => _lastName; | |
set => _lastName = value.Trim(); | |
} | |
public string Country | |
{ | |
get; | |
} | |
private string _fullName; | |
public string FullName => $"{FirstName} {LastName}" ; | |
} | |
public static void ThrowExceptionsInExpressions() | |
{ | |
WriteFeatureHeader("Throw Exceptions in Expressions", "You can now throw exceptions inline in conditional expressions, null coalescing expressions, and some lambda expressions"); | |
try | |
{ | |
var a = Divide(10, 0); | |
} | |
catch (DivideByZeroException e) | |
{ | |
Console.WriteLine($"Caught exception: {e.Message}"); | |
} | |
} | |
public static double Divide(int x, int y) | |
{ | |
return y != 0 ? x % y : throw new DivideByZeroException(); | |
} | |
public static void WriteFeatureHeader(string name, string description) | |
{ | |
Console.WriteLine(); | |
Console.WriteLine("=============================="); | |
Console.WriteLine($"{name}".ToUpper()); | |
Console.WriteLine($"{description}"); | |
Console.WriteLine("=============================="); | |
Console.WriteLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment