Skip to content

Instantly share code, notes, and snippets.

@carlosschults
carlosschults / csharp8-using-the-new-operator.cs
Created December 21, 2017 01:40
csharp8-using-the-new-operator.cs
public void Foo(string? bar)
{
if (!bar.IsNullOrEmpty())
{
var length = bar!.Length;
}
}
@carlosschults
carlosschults / csharp8-new-operator.cs
Created December 21, 2017 01:39
csharp8-new-operator.cs
public void Foo(string? bar)
{
if (!bar.IsNullOrEmpty())
{
var length = bar.Length;
}
}
@carlosschults
carlosschults / csharp8-prevent-assignment.cs
Created December 21, 2017 01:37
csharp8-prevent-assignment.cs
string? nullableString = "hello";
string nonNullableString = nullableString;
@carlosschults
carlosschults / csharp8-null-treatment-2.cs
Created December 21, 2017 01:37
csharp8-null-treatment-2cs
public int CalculateSquareOfAge(Person? p)
{
int age = p?.Age ?? 0;
return age * age;
}
@carlosschults
carlosschults / csharp8-null-treatment.cs
Last active December 21, 2017 01:36
csharp8-null-treatment.cs
public int CalculateSquareOfAge(Person? p)
{
var result = 0;
if (p != null)
result = p.Age * p.Age;
return result;
}
@carlosschults
carlosschults / csharp8-null.cs
Created December 21, 2017 01:35
csharp8-null.cs
public int CalculateSquareOfAge(Person? p)
{
int age = p.Age;
return age * age;
}
@carlosschults
carlosschults / csharp8-nonnull.cs
Created December 21, 2017 01:34
csharp8-nonnull.cs
public int CalculateSquareOfAge(Person p)
{
int age = p.Age;
return age * age;
}
@carlosschults
carlosschults / csharp8-interface.cs
Created December 21, 2017 01:33
csharp8-interface.cs
interface IDisplayService
{
void DisplayMessage(string message) { WriteLine(message); }
}
@carlosschults
carlosschults / csharp8-3.cs
Created December 21, 2017 01:33
csharp8-3.cs
int x = int.Parse(Console.Readline());
if (x.Even)
{
Console.WriteLine("You’ve typed an even number.");
}