Last active
August 29, 2015 14:23
-
-
Save cawhitworth/64a6062ecdcf27b12d09 to your computer and use it in GitHub Desktop.
Fun with constructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConstructorFun | |
{ | |
class ConstructorLoop | |
{ | |
public ConstructorLoop(int a, int b) : this(a, b, 20) | |
{ } | |
public ConstructorLoop(int a, int b, int c) : this(a,b) | |
{ } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var a = new ConstructorLoop(1,2); | |
} | |
} | |
} |
Oh, even better!
namespace ConstructorFun
{
class ConstructorLoop
{
public static int Counter { get; set; }
public ConstructorLoop(int a, int b) : this(a, b, 20)
{ }
public ConstructorLoop(int a, int b, int c) : this(f(a),b)
{ }
static int f(int f)
{
Counter ++;
if (Counter > 10)
throw new Exception();
return f;
}
public override string ToString()
{
return "Hello world!";
}
}
class Program
{
static void Main(string[] args)
{
try
{
var a = new ConstructorLoop(1, 2);
}
catch (Exception e)
{
// ignored
}
finally
{
Console.WriteLine(ConstructorLoop.Counter);
}
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, this is totally legit C#, apparently. Not even a compiler warning.