pghtech
pghtech

Reputation: 3702

C# - how can a constructor be created that takes variables before they are declared?

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

Upvotes: 2

Views: 238

Answers (6)

Hans Passant
Hans Passant

Reputation: 941217

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.

Upvotes: 2

this. __curious_geek
this. __curious_geek

Reputation: 43207

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.

Upvotes: 0

Evan Trimboli
Evan Trimboli

Reputation: 30082

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

Upvotes: 0

Itay Karo
Itay Karo

Reputation: 18286

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.

Upvotes: 0

n8wrl
n8wrl

Reputation: 19765

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?

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

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.

Upvotes: 4

Related Questions