Skip to content

Instantly share code, notes, and snippets.

@filmor
Created November 19, 2021 08:48
Show Gist options
  • Save filmor/33c9276304cc13a94a0bc3927e2fc6a3 to your computer and use it in GitHub Desktop.
Save filmor/33c9276304cc13a94a0bc3927e2fc6a3 to your computer and use it in GitHub Desktop.
using System;
public class Program
{
static int i = 0;
static Test test = new Test();
static Test GetTest => (i % 2) == 0 ? null : test;
static string FuncWithSideEffect() {
Console.WriteLine("Called inner");
return "inner";
}
public static void Main()
{
Console.WriteLine("Started");
Console.WriteLine("GetTest: {0}", GetTest);
GetTest?.Func(FuncWithSideEffect());
GetTest?.Func($"{test}");
i = 1;
Console.WriteLine("===");
Console.WriteLine("GetTest: {0}", GetTest);
GetTest?.Func(FuncWithSideEffect());
GetTest?.Func($"{test}");
i = 2;
Console.WriteLine("===");
Console.WriteLine("GetTest: {0}", GetTest);
GetTest?.Func(FuncWithSideEffect());
GetTest?.Func($"{test}");
Console.WriteLine("Done");
}
}
class Test {
public void Func(string s) {
Console.WriteLine("Called Func({0})", s);
}
public override string ToString() {
Console.WriteLine("ToString called");
return "test";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment