Taking C# to a whole new level of Evil
-
No language should even be able to do this but it works (complies and runs).
public class BaseClass
{
public string Value { get; set; }
public BaseClass(string val) { Value = val; }
}public class Derived : BaseClass
{
public Derived(string val)
: base(new Func(() =>
{
if (String.IsNullOrEmpty(val))
{
throw new ArgumentNullException("val");
}
else
{
return val;
}
})()){}
}And the error message when the Derived class constructor is passed a null value is equally confusing: System.ArgumentNullException: Value cannot be null. Parameter name: val at TestingApp.Derived.<>c__DisplayClass0_0.<.ctor>b__0() at TestingApp.Derived..ctor(String val) at TestingApp.Program.Main(String[] args) This looks more like a way to program hard to find errors, deliberate back doors, or the hard-to-trace magic self-destruct method then a useful code addition. Is there any instance this could be useful other than a hack or workaround?
if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
-
No language should even be able to do this but it works (complies and runs).
public class BaseClass
{
public string Value { get; set; }
public BaseClass(string val) { Value = val; }
}public class Derived : BaseClass
{
public Derived(string val)
: base(new Func(() =>
{
if (String.IsNullOrEmpty(val))
{
throw new ArgumentNullException("val");
}
else
{
return val;
}
})()){}
}And the error message when the Derived class constructor is passed a null value is equally confusing: System.ArgumentNullException: Value cannot be null. Parameter name: val at TestingApp.Derived.<>c__DisplayClass0_0.<.ctor>b__0() at TestingApp.Derived..ctor(String val) at TestingApp.Program.Main(String[] args) This looks more like a way to program hard to find errors, deliberate back doors, or the hard-to-trace magic self-destruct method then a useful code addition. Is there any instance this could be useful other than a hack or workaround?
if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
If the base class constructor is doing too much, or allocates a lot of memory, this could prevent that work from taking place if the argument isn't valid. If you move the
throw
into theDerived
constructor, the base class constructor has already run by the time the argument is validated. It would definitely be cleaner with the validation code extracted to a real method - possibly in a separate class, if it's the sort of thing you use a lot:public static class Requires
{
public static string NotNullOrEmpty(string value, string parameterName)
{
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentNullException(parameterName);
return value;
}
}public class Derived
{
public Derived(string val) : base(Requires.NotNullOrEmpty(val, nameof(val)))
{
}
}System.ArgumentNullException: Value cannot be null.
Parameter name: val
at TestingApp.Requires.NotNullOrEmpty(String value, String parameterName)
at TestingApp.Derived..ctor(String val)
at TestingApp.Program.Main(String[] args)With C# 7, I believe you'll be able to do something like this:
public class Derived
{
public Derived(string value) : base(string.IsNullOrEmpty(val) ? throw new ArgumentNullException(nameof(val)) : val)
{
}
}C# 7 Additions – Throw Expressions – Structured Sight[^] I'm not sure if that makes it any more readable, though. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer