2

I have a tendency to invoke methods directly on a new class instance like this:

new Person().GetAge()

when I don't need the instance of the class (here Person) after the invocation.

However, I have seen many developers recommend using this pattern:

Person p = new Person();
p.GetAge()

What are the benefits of declaring a local variable if the instance it references is not required after a method call?

Note: I know in some cases, such methods can be changed to static methods, but for now assume that is not possible?

  • 5
    I always find that having to new up an instance to invoke a method to be worth investigating and possibly refactoring, It is all a matter of style though. – ChaosPandion Sep 26 '18 at 18:19
  • I think some of the answers are getting tripped up on the fact that with the code as-written, the GetAge() method would return the same result. Perhaps edit your question to read something like new Person("id:34fg76").GetAge() – Graham Oct 22 '18 at 17:21
8

If you compare the IL produced for just using new Person().GetAge() verses assigning to variable before calling GetAge, you'll notice the IL produced is identical.

So, from what happens to the code, there is absolutely no difference.

The benefits, or lack of, therefore lie in other areas. Some might argue that assigning it to a variable makes the code easier to read. Also, it can make debugging easier.

Note: I know in some cases, such methods can be changed to static methods, but for now assume that is not possible?

Sure we can assume this for the purposes of the question. In reality though, I'd argue that creating an instance of a class just to invoke a method should be avoided and this should be made static.

5

Both cases are not very appropriate use of objects. An object is meant to carry some state between method calls. If you (have to) create an object to call only one of its method and then discard it, it would as well be static method in the class. If you happen to often make such calls, you probably need redesign the class to expose a static method.

I don't really see any reason to always prefer any of the ways, sometimes it is clearer to use an explicit variable, sometimes it would distract from some bigger logic.

  • Thanks for advice. I already mentioned in the question, for this questions sake, assume that static class is not an option – digitguy Sep 28 '18 at 18:10
1
new Person().GetAge()

If you're really doing this then I question the design of your application because it's pointless.

You either should pass the instance via dependency injection or you should just make the API static.

I see absolutely no advantange in calling a method on an instance that you immediately throw away.

  • Thanks for advice. I already mentioned in the question, for this questions sake, assume that static class is not an option. – digitguy Sep 28 '18 at 18:10
1

To keep it simple, when you use

Person p = new Person();
p.GetAge();

you create and initialize a new object of the Person() class which you can reuse throughout your application. By doing

new Person().GetAge()

we assume that there is a predefined value (a constant) being returned by the method, which is highly unlikely to be used like that unless it is a private property. Additionally, this is immediately disposed after the code executes.

Here's a small implementation of your class

public class Person
{
    public string Name { get; set}
    public int Age { get; set}
}

Doing this allows you to assign values to the object's properties, so you can create and give your object a name

Person p = new Person();
p.Name = "Bob Smith";

or even an age

p.Age = 29;

and if you want to check or use it later, just simply call it:

Console.WriteLine("Name: {0}" + p.Name);
Console.WriteLine("Age: {0}" + p.Age);

Remember the concept of OOP. Here's a great article on Object-Oriented Programming (C#) that can be great for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.