Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method chaining and exceptions in C#

If I have a method chain like the following:

        var abc = new ABC();
        abc.method1()
           .method2()
           .methodThrowsException()
           .method3()
           ;

assuming I've defined method1(), method2() and method3() as

    public ABC method1() {
        return this;
    }

and methodThrowsException() as

    public ABC method3() {
        throw new ArgumentException();
    }

When running the code, is it possible to know which specific line of code has thrown the Exception, or will it just consider all the method chaining as just one line? I've done a simple test and it seems it considers them all as just one line but Method Chaining says

Putting methods on separate lines also makes debugging easier as error messages and debugger control is usually on a line by line basis.

Am I missing something, or does that just not apply to C#?

Thanks

Edit: here is what i currently get:

alt text http://img163.imageshack.us/img163/4503/83077881.png

like image 979
devoured elysium Avatar asked May 21 '10 15:05

devoured elysium


People also ask

Why do we use method chaining?

Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.

What is method chaining in C#?

Method chaining is a technique in which methods are called on a sequence to form a chain and each of these methods return an instance of a class. These methods can then be chained together so that they form a single statement. A fluent interface is an object-oriented API that depends largely on method chaining.

How do you do chaining method?

Method Chaining is the practice of calling different methods in a single line instead of calling other methods with the same object reference separately. Under this procedure, we have to write the object reference once and then call the methods by separating them with a (dot.).

Is method chaining a design pattern?

This is an example of the Method chaining design pattern in Haxe. Method chaining is calling a method of an object that return the same type of the object multiple times.


1 Answers

If you look at the stack trace, which is displayed as part of the exception details, you should see the exact location of the exception, no matter how you format the code.

I guess that formatting the code differently would be useful if the debugger allowed you to put a breakpoint on a specific line, but in C#, breakpoints are placed on individual expressions, so this won't really help. You'd probably need to rewrite the code like this to allow placing breakpoints:

var abc = new ABC(); 
var abc1 = abc.method1();
var abc2 = abc1.method2();
// etc...

The same applies to highlighting of the current expression (in your screenshot). However, the exact information about the exception are always available in the stack trace.

like image 128
Tomas Petricek Avatar answered Sep 18 '22 22:09

Tomas Petricek