7

In the below two sample code I am trying to instantiate a class named Test by using C# normal method and Object initializer.

DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test();
test.Date = nullDate.Value; //exception will throw here
test.Name = "String";
test.AnotherDate = notNullDate.Value;

In the above sample code, I can clearly understand which property is showing exception while debugging.

DateTime? nullDate = null; //this value will come from somewhere else
DateTime? notNullDate = DateTime.Now;
var test = new Test
{
    Date = nullDate.Value,
    Name = "String",
    AnotherDate = notNullDate.Value
};

In this above code, when I use object initializer I couldn't understand which property is thrown exception. Here I couldn't debug line by line. If I have lot of properties initialized it's very difficult to identify.

Here is my question: How can I identify which property is showing exception from exception window? Right now the inner exception is null.

enter image description here

Peter B
  • 22,460
  • 5
  • 32
  • 69
Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79
  • 2
    You can't - which is why you should do null checks, etc. _before_ calling the initializer. The exception does not give you any context about _what_ is null – D Stanley Nov 23 '16 at 16:04
  • @DStanley I can check null reference but my question is no other way to identify the property which throw exception? – Jameel Moideen Nov 23 '16 at 16:07
  • No - the exception gives you no context about _what_ is null. – D Stanley Nov 23 '16 at 16:10
  • @DStanley let's suppose these values are the parameters of service and my property is not nullable , there could be lot of chance to become null from the client side.in this case I have to look every property what is null or not. – Jameel Moideen Nov 23 '16 at 16:16
  • Yes you do, unless the service architecture has validation for required parameters built-in (like web services does). – D Stanley Nov 23 '16 at 16:41

1 Answers1

0

Object initializers should be used for simple initialization. Where you have a code that throws an exception, you will run into described by you issues.

I know this is not really an answer, but you will not know which property fails. In case of nullables you could use something like this, where you specify the default value.

var test = new Test
        {
            Date = nullDate.GetValueOrDefault(new DateTime()),
            Name = "String",
            AnotherDate = notNullDate.Value
        };
Roland
  • 972
  • 7
  • 15
  • My question is all about , is there any debugging techniques to identify the property? then what is the advantage of using Object Initializers, both not making any difference for me? – Jameel Moideen Nov 23 '16 at 16:10
  • 2
    You can refer to this answers for benefits: http://stackoverflow.com/questions/12842371/is-there-any-benefit-of-using-an-object-initializer – Roland Nov 23 '16 at 16:17
  • I am wondering why visual studio can't provide the property which throw exception. – Jameel Moideen Nov 23 '16 at 16:26
  • 1
    No, because from a VS point of view that's one line of code. You have the same problem if you do something like `prop1.prop2.prop3.ToString()`. If any one of those properties is `null` you will get an exception with no context as to _which_ one is null. The exception handler does not go to the trouble of examining the call stack, local variables, member names, etc. to give you more context. If you _want_ it to, then you can request a feature on http://connect.microsoft.com, but null checks are the standard way to deal with that problem. – D Stanley Nov 23 '16 at 16:44