Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Taking C# to a whole new level of Evil

Taking C# to a whole new level of Evil

Scheduled Pinned Locked Moved C#
csharpdebugginghelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Foothill
    wrote on last edited by
    #1

    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

    Richard DeemingR 1 Reply Last reply
    0
    • F Foothill

      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

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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 the Derived 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups