views:

195

answers:

7

If I write:

SomeType simpleName = classWithLongName.otherLongName;

And then use "simpleName" instead of "classWithLongName.otherLongName", will this change the program in any way (for instance performance wise)?

What does the compiler do with this? Does it copy+paste "classWithLongName.otherLongName", everywhere I use "simpleName".

+1  A: 

It depends what "otherLongName" is actually doing. If it's a property, then the difference is between executing the property several times or only executing it once. That may or may not change the behaviour of the program in a significant way, depending on what it's doing.

Jon Skeet
A: 

You can always make it a function.

SomeType simpleName() { return classWithLongName.otherLongName; }
Daniel A. White
This has a larger impact.
Jules
A: 

The compiler is only allowed to cache the value and re-use it itself when you always type "classWithLongName.otherLongName" if it knows that the value will not change in the course. However, this is seldom the case.

Therefore, if "classWithLongName.otherLongName" does perform some computation, you'll usually get better performance by caching it manually in a local variable as you suggested. However, keep in mind that you are working with a cached value and that changes in the original value or property will not be reflected on your cached value.

The length of the name however is just metadata and has no influence whatsoever on runtime performance, since the name is already resolved to an internal handle during compilation.

Lucero
+2  A: 

No, the C# compiler doesn't translate a call to "simpleName" to be the same as copying and pasting "classWithLongName.otherLongName". The difference could be profound or simply semantic, but what you're doing is assigning the value from classWithLongName.otherLongName to simpleName. Whether the type is a value type or a reference type will determine exactly what happens and what will happen if you manipulate that value, but you're not creating a function pointer or delegate in doing that.

Whether it will have an effect on performance really isn't something that can be answered here, other than to say that it won't have a NEGATIVE effect. We can't say if it will have a positive effect, since that would depend on what actually happens when you call classWithLongName.otherLongName. If that's an expensive operation, then this could make it faster, but the downside would be that any differences in value upon subsequent calls to classWithLongName.otherLongName wouldn't be reflected if you cached its value in simpleName.

Adam Robinson
A: 

Is this a question about instances or classes?

For instance

namespace MyCompany.MyApp.LongNamespaceName
{
    public class MyClassWithALongName {

        public SomeType AnInstanceProperty {get;set;}

        public static SomeType AStaticProperty {get { ... }}
    }
}

Now:

//this gets the static property
SomeType simpleName = MyClassWithALongName.AStaticProperty;

Alternatively:

MyClassWithALongName anInstanceWithALongName = new MyClassWithALongName();

//this gets the instance property
SomeType simpleName = anInstanceWithALongName.AnInstanceProperty;

These will behave in different ways.

There's another case here though, you can create an alias for the actual name of the class:

using simpleName = MyCompany.MyApp.LongNamespaceName.MyClassWithALongName;

...
simpleName anInstance = new simpleName ();
Keith
A: 
  • If classWithLongName.otherLongName is a property, than changes to simpleName will NOT change classWithLongName.otherLongName.

  • If classWithLongName.otherLongName is a public data member (a field) of a value type, than changes to simpleName will NOT change classWithLongName.otherLongName.

  • If classWithLongName.otherLongName is a public data member (a field) of a reference type, than changes to simpleName WILL change classWithLongName.otherLongName.

James Curran
A: 

Assuming your type is an object (reference) type then simpleName will end up containing a reference to the object returned by classWithLongName.otherLongName. If you are then going to make lots of calls to properties on that object then you may get a performance improvement, especially if otherLongName is a property as opposed to a field.

open-collar