tags:

views:

111

answers:

6

Based on my question - take the following code:

class Nevermore60Customer: GenericCustomer  
{  
  public Nevermore60Customer(string name, string referrerName)  
  : base (name)  
  {  
      this.referrerName = referrerName;  
  }  
  private string referrerName;  
  private uint highCostMinutesUsed;  

To me, it appears the variable "referrrerName" is being initialized "after" it is being referenced as a passed parameter in the constructor.

public Nevermore60Customer(string name, string referrerName)

Am I worng, and if so how? Or if I am right and it is being initialized after it is being referenced in the constructor, how is it possible?

Thanks

+4  A: 

The position of the variable declaration compared with the constructor is irrelevant to C#.

It would make this easier to discuss if you had different names for the parameter and field though:

class Test
{
    public Test(string parameter)
    {
        this.field = parameter;
    }

    private string field;
}

Basically the field "exists" before the constructor is called. If the field is declared with an initializer, like this:

private string field = "default value";

then that initializer is run before the constructor, even though it may come after it within the source code.

Jon Skeet
Putting a break point on the code that declares your "NeverMore60Customer" class and stepping in using F11 may help illustrate what's happening here
Stuart Helwig
A: 

Not sure I understand the question. Your constructor has a strign parameter, referrerName, that you are assigning TO a private class variable, also called referrerName. I don't see where this.referrerName is referenced before its initialization?

n8wrl
A: 

this.referrerName refers to the class member declared as private string referrerName;
The referrerName to the right of the = is the parameter to the constructor.

Itay
A: 

It doesn't matter how you order the private members of the class and the constructor, the private members will always be initialized first.

Evan Trimboli
+1  A: 

C# is an object oriented language and you seem to confuse plain C procedural language concepts with C#. Unlike C, in C# the order of declaration does not matter as long as the instance is initialized before accessing and is within the scope.

this. __curious_geek
Well, the order of declaration *can* matter. In particular, initializers are executed in textual order... which becomes ill-defined when you've got a partial class. It's almost always a bug to depend on this of course, but it can make a difference. The position of *methods* (or constructors, or properties etc) makes no defined difference that I'm aware of.
Jon Skeet
But they are all initialized and available before the constructor.
this. __curious_geek
+2  A: 

The constructor argument is not an alias for the field. It hides the field name, this code won't work:

public Nevermore60Customer(string name, string referrerName) : base (name)  
  {  
      referrerName = referrerName;    // bad
  } 

By using the "this." prefix, you can tell the compiler to assign the argument value to the field. It is a very common pattern, avoids having to come up with another name for the argument. Or do something awkward like prefixing the field name with, say, an underscore.

Hans Passant