-1

I'm trying to figure out what is the difference between:

public partial class TestWindow : Window
{
    object obj = new object();
    public TestWindow()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

and:

public partial class TestWindow : Window
{
    object obj;
    public TestWindow()
    {
        InitializeComponent();
        obj = new object();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

and:

public partial class TestWindow : Window
{
    object obj;
    public TestWindow()
    {
        InitializeComponent();

    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        obj = new object();
    }
}

it looks like they all act the same and i wonder if there's any important difference between them, or is it just a "best practice" thing to choose one of them. Thanks in advance

Yoav
  • 3,326
  • 3
  • 32
  • 73
  • 1
    Well may be you should try to debug with F11 and find it out. The initialize component will initialize many things and see where your object is getting initialized and where you actually want it to be initialized. – Milee Apr 19 '12 at 06:52
  • As far as performance is considered, there is no difference. – TRR Apr 19 '12 at 06:52
  • possible duplicate of [Instantiating objects in the constructor](http://stackoverflow.com/questions/4772808/instantiating-objects-in-the-constructor) – V4Vendetta Apr 19 '12 at 06:53
  • You should initialize an object shortly before you use it. I prefer the way to initialize it in the constructor. – Tomtom Apr 19 '12 at 06:54

5 Answers5

3

The answer to your question is you should initialize variables before you use them.

Field initializers run before constructors, and constructors run before any other method, such as your Window_Loaded. It matters because if you try to use your object before it is initialized, you will get a NullReferenceException. If you don't use your object before Window_Loaded is called, then it makes no difference.

Asik
  • 21,506
  • 6
  • 72
  • 131
  • +1. It's also explained in depth in [Effective C#: 50 Specific Ways to Improve your C# by Bill Wagner](http://www.amazon.com/Effective-Covers-4-0-Specific-Development/dp/0321658701) – Eddie Flores Apr 19 '12 at 07:08
0

Initializing field on declaration is the same as to initialize it in the first lines of constructor so

public partial class TestWindow : Window
{
    object obj = new object();
    public TestWindow()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

This one is the same as

public partial class TestWindow : Window
{
    object obj;
    public TestWindow()
    {
        obj = new object();
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

and when you initialize in Window_Load, the object will be initialized only when the event will be fired... which can, or can't be happen :) In case of WinForm or WPF Window_Load will be called only after all ui elements are loaded

Hope this helps

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • AFAIK, in the first one object is inialized before the constructor is called. – daryal Apr 19 '12 at 07:02
  • @daryal The compiler will take care of generating the code at the beginning of each constructor. – Eddie Flores Apr 19 '12 at 07:14
  • @eddflrs there is difference, please check the following http://www.yoda.arachsys.com/csharp/constructors.html ; in fact java and c# has a main difference when employing base classes. – daryal Apr 19 '12 at 07:59
0

The only difference that could be here is a difference implied by your program architecture. Where to initialize is a matter of execution flow, so you should decide where it's a better place to init your obj member. Considering that obj is a global variable of your class you can initilaize it just in place:

public class TestWindow
{
  object obj = new object();
  public TestWindow()
  {
     ...
  }
}

..or if this is a big object and may not be used during execution flow on your form, you may want to init it only on esplicit request of the user, say ButtonClick.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0
public partial class TestWindow : Window
{
    object obj = new object();
    public TestWindow()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

Here object is initialized before the constructor is called.

public partial class TestWindow : Window
{
    object obj;
    public TestWindow()
    {
        InitializeComponent();
        obj = new object();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

Object is initialized in the constructor.

public partial class TestWindow : Window
{
    object obj;
    public TestWindow()
    {
        InitializeComponent();

    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        obj = new object();
    }
}

Object is initialized when window_Loaded is called.

Personally, I never initalize an object before constructor is called. This may lead to some problems if you refactoring a code into IOC version. Also, if you initialize an object in a method, just note that if any method before this one tries to use this object, probably it will fail. On the otherhand, it just depends on what you want to do. All usages are right in some sense.

daryal
  • 14,643
  • 4
  • 38
  • 54
0

There is a main difference in scope.

Like in the first case it is accessible in every method you define in the class In short it is global .

and in second and third case in its declare in function so it is accessible to that function only.

Now the other difference is in first and second case object will declare when you create and object of it but in third case it will declare when actually load it.

Like

TestWindow objTestWindow = new TestWindow(); // will not create object
TestWindow.Show(); // will create an object
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Red
  • 39
  • 2