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 / MainPage.xaml
Created May 5, 2018 16:24
TreeView template to use an item template
<Style x:Key="TreeViewStyle1" TargetType="TreeView">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<TreeViewList x:Name="ListControl" AllowDrop="False"
CanReorderItems="False"
CanDragItems="False"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
ItemTemplate="{StaticResource CultureItemDataTemplate}">
@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;
@christiannagel
christiannagel / binaryliterals.cs
Created May 5, 2018 21:12
binary literals with C# 7
uint binary1 = 0b1111_0000_1010_0101_1111_0000_1010_0101;
@christiannagel
christiannagel / expressionbodiedmembers.cs
Created May 5, 2018 21:14
Expression-bodied members with C# 7
// C# 6
private string _firstName;
public string FirstName
{
get { return _firstName; }
set { Set(ref _firstName, value); }
}
// C# 7
private string _firstName;
@christiannagel
christiannagel / outvar.cs
Created May 5, 2018 21:17
out var with C# 7
// C# 6
string n = "42";
int result;
if (string.TryParse(n, out result)
{
Console.WriteLine($"Converting to a number was successful: {result}");
}
// C# 7
string n = "42";
@christiannagel
christiannagel / NonTrailingNamedArguments.cs
Created May 5, 2018 21:31
Non-trailing named arguments with C# 7.2
// C# 7.0
if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase: true,
result: out DayOfWeek weekday))
{
reservation.Weekday = weekday;
}
// C# 7.2
if (Enum.TryParse(weekdayRecommendation.Entity, ignoreCase: true,
out DayOfWeek weekday))
@christiannagel
christiannagel / ReadonlyStruct.cs
Last active May 5, 2018 21:32
readonly struct with C# 7.2
public readonly struct Dimensions
{
public double Length { get; }
public double Width { get; }
public Dimensions(double length, double width)
{
Length = length;
Width = width;
}
@christiannagel
christiannagel / InModifier.cs
Created May 5, 2018 21:36
in modifier with C# 7.2
static void CantChange(in AStruct s)
{
// s can’t change
}
@christiannagel
christiannagel / defaultliteral.cs
Last active May 5, 2018 21:40
default literal with C# 7.1
// C# 7.0
int x = default(int);
ImmutableArray<int> arr = default(ImmutableArray<int>);
// C# 7.1
int x = default;
ImmutableArray<int> arr = default;