nubela
nubela

Reputation: 17314

How to perform this c# instantiation and method usage in 1 line?

I'm pretty anal when it comes to making my code succinct.

SVNLock lock_obj = new SVNLock(lock_file);
lock_obj.Execute();

Is there any way I can do it

Thanks!

Upvotes: 0

Views: 154

Answers (2)

SLaks
SLaks

Reputation: 888223

If you don't use the lock_obj variable anywhere else, you can write

new SVNLock(lock_file).Execute();

If you do use the variable later, there is nothing you can do, unless you edit the SVNLock class and make Execute return the instance (This is called a Fluent interface). If you really want to, you could do that in an extension method with a different name, but I wouldn't recommend it.

Side note: C# naming conventions frown on underscores; your variables should be called lockFile and lockObj.

Upvotes: 3

alexn
alexn

Reputation: 59012

What does Execute return?

If the constructor does not return anything special, you can do new SVNLock(lock_file).Execute();.

Upvotes: 4

Related Questions