Skip to content

Instantly share code, notes, and snippets.

@georgekosmidis
Last active November 21, 2021 18:25
Show Gist options
  • Save georgekosmidis/22dd9b3f20165dc896f6e386dbd1a036 to your computer and use it in GitHub Desktop.
Save georgekosmidis/22dd9b3f20165dc896f6e386dbd1a036 to your computer and use it in GitHub Desktop.
[Obsolete($"Call {nameof(Discard)} instead")]
LambdaExpression parseExpr = (string s) => int.Parse(s); // Expression<Func<string, int>>
Expression parseExpr = (string s) => int.Parse(s); // Expression<Func<string, int>>
<ItemGroup>
<Using Include="System.IO.Pipes" />
</ItemGroup>
<ItemGroup>
<Using Remove="System.Threading.Tasks" />
</ItemGroup>
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
global using System;
global using static System.Console;
global using Env = System.Environment;
Func<string, int> parse = [SomeAttribute(1)] (s) => int.Parse(s);
var choose = [SomeAttribute(2)][SomeAttribute(3)] object (bool b) => b ? 1 : "two";
int x2;
int y2;
(x2, y2) = (0, 1); // Works in C# 9
(var x, var y) = (0, 1); // Works in C# 9
(x2, var y3) = (0, 1); // Works in C# 10 onwards
namespace MyCompany.MyNamespace;
class MyClass // Note: no indentation, no curly brackets
{
//...
}
var parse = s => int.Parse(s); // ERROR: Not enough type info in the lambda
var parse = (string s) => int.Parse(s);
Func<string, int> parse = (string s) => int.Parse(s);
Func<int> read = Console.Read;
Action<string> write = Console.Write;
var read = Console.Read; // Just one overload; Func<int> inferred
var write = Console.Write; // ERROR: Multiple overloads, can't choose
public record struct Person(string FirstName, string LastName);
object obj = new Person
{
FirstName = "George",
LastName = "Kosmidis",
Address = new Address { City = "Munich" }
};
if (obj is Person { Address: { City: "Munich" } })
Console.WriteLine("Munich");
if (obj is Person { Address.City: "Munich" }) // Extended property pattern
Console.WriteLine("Munich");
public record struct Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
var choose = (bool b) => b ? 1 : "two"; // ERROR: Can't infer return type
var choose = object (bool b) => b ? 1 : "two"; // Func<bool, object>
String.Create(CultureInfo.InvariantCulture, $"The result is {result}");
var sb = new StringBuilder();
sb.Append($"Hello {args[0]}, how are you?");
public struct Address
{
public Address()
{
City = "<unknown>";
}
public string City { get; init; }
}
public struct Address
{
public string City { get; init; } = "<unknown>";
}
var person = person with { LastName = "Kristensen" };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment