Skip to content

Instantly share code, notes, and snippets.

View christiannagel's full-sized avatar
💭
working on the new book Pragmatic Microservices with C# and Azure

Christian Nagel christiannagel

💭
working on the new book Pragmatic Microservices with C# and Azure
View GitHub Profile
@christiannagel
christiannagel / EnumerableExtensions.cs
Last active July 22, 2017 07:39
Where with a local function
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return Iterator();
IEnumerable<T> Iterator()
{
foreach (T item in source)
@christiannagel
christiannagel / Quicksort.cs
Created July 22, 2017 07:45
Quicksort with recursive local function
public static void QuickSort<T>(T[] elements) where T : IComparable<T>
{
void Sort(int start, int end)
{
int i = start, j = end;
var pivot = elements[(start + end) / 2];
while (i <= j)
{
while (elements[i].CompareTo(pivot) < 0) i++;
@christiannagel
christiannagel / WhenDoesItEnd.cs
Created July 22, 2017 07:51
Endless recursive call until stack ends
public static void WhenDoesItEnd()
{
Console.WriteLine(nameof(WhenDoesItEnd));
void InnerLoop(int ix)
{
Console.WriteLine(ix++);
InnerLoop(ix);
}
InnerLoop(1);
}
@christiannagel
christiannagel / AsynchronousPattern.cs
Created July 22, 2017 08:03
Using a Lambda Expression within a Method
static void AsynchronousPattern()
{
WebRequest request = WebRequest.Create(url);
IAsyncResult result = request.BeginGetResponse(ar =>
{
using (WebResponse response = request.EndGetResponse(ar))
{
Stream stream = response.GetResponseStream();
var reader = new StreamReader(stream);
@christiannagel
christiannagel / AsynchronousPattern.cs
Created July 22, 2017 08:05
Local Function instead of a Lambda expression
private static void AsynchronousPattern()
{
WebRequest request = WebRequest.Create(url);
IAsyncResult result = request.BeginGetResponse(ReadResponse, null);
void ReadResponse(IAsyncResult ar)
{
using (WebResponse response = request.EndGetResponse(ar))
{
Stream stream = response.GetResponseStream();
@christiannagel
christiannagel / ColumnNames.cs
Last active August 27, 2017 09:29
const string values with nameof
public class ColumnNames
{
public const string LastUpdated = nameof(LastUpdated);
public const string IsDeleted = nameof(IsDeleted);
public const string BookId = nameof(BookId);
}
@christiannagel
christiannagel / CultureData.cs
Created May 5, 2018 15:30
CultureData for showing culture information in a tree control
public class CultureData
{
public CultureInfo CultureInfo { get; set; }
public IList<CultureData> SubCultures { get; set; }
private double _numberSample = 9876543.21;
public string NumberSample => _numberSample.ToString("N", CultureInfo);
public string DateSample => DateTime.Today.ToString("D", CultureInfo);
public string TimeSample => DateTime.Now.ToString("T", CultureInfo);
public RegionInfo RegionInfo
{
@christiannagel
christiannagel / MainPage.xaml
Created May 5, 2018 15:50
Using the TreeView control
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TreeView Style="{StaticResource TreeViewStyle1}" x:Name="treeView1"
ItemInvoked="{x:Bind OnSelectionChanged, Mode=OneTime}"
SelectionMode="Single">
</TreeView>
<local:CultureDetailUC Grid.Column="1" CultureData="{x:Bind ViewModel.SelectedCulture, Mode=OneWay}" />
@christiannagel
christiannagel / MainPage.xaml
Created May 5, 2018 16:26
DataTemplate to display CultureData information in the TreeView
<DataTemplate x:Key="CultureItemDataTemplate">
<StackPanel Orientation="Horizontal" Height="40">
<TextBlock Text="{Binding Content.CultureInfo.EnglishName}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
// C# 6
long n1 = 0x1234567890ABCDEF;
// C# 7
long n2 = 0x1234_5678_90AB_CDEF;
// C# 7.2
long n2 = 0x_1234_5678_90AB_CDEF;