Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
Could anyone tell me what difference is between those?
public Time2(int h, int m, int s) : this(h, m, s) wit without this keyword
public Time2(int h, int m, int s)


Time2 time = new Time2(2, 30, 45);
Posted
Updated 28-Oct-13 22:10pm
v2

It won't work the way you posted it because both constructors are of the same signature. A slightly changed example:
C#
class Time2
{
    // Constructor with many parameters
    public Time2(int hours, int minutes, int seconds){
        // Implementation
    }

    // Constructor with few parameters
    public Time2(int hours):this(hours, 0, 0)
    {
        // Implementation
    }
}

class Example
{
    private void ExampleMethod()
    {
        Time2 firstTime = new Time2(2, 30, 12);
        Time2 secondTime = new Time2(24);
        Time2 thirdTime = new Time2(24, 0, 0);
    }
}
Here you have two constructors that the compiler can distinguish by their signatures. That means, they have different parameter lists.

The second constructor only takes one parameter. It calls the first constructor, forwards what parameters were given and sets the rest to zero.
Therefore in the example, secondTime and thirdTime use different constructors, but are holding identical values.
 
Share this answer
 
In .NET there are Constructors without parameters, Constructors with parameters, and you can, as you know, have multiple Constructors each of which has a unique parameter list.

At run-time the .NET compiler "decides" which Constructor to call by matching the parameter list of the invocation (the code that calls the Constructor) to the possible Constructors available.

So, what qualifies as a "unique" parameter list:

1. a difference in the Types in the parameter list: that's intuitive for most of us.

2. a difference in the position of the (same) Types in the parameter list: that's something you probably don't want to exploit in your code; why have two Constructors like:
public Class1(int x, string y) {}
public Class1(string y, int x) {}
Just to allow two different orderings of using the same parameters ?

The special syntax of following the closing parenthesis of the parameter list of a Constructor with a colon and the keyword "this," optionally followed by a list of variables in parentheses, allows you to redirect the flow-of-control of your creation of a Class (or Struct) to other Constructors you have defined.
public Class1() {}

public Class1(int x, string y) : this() {}
Is going to call the parameterless Class1 Constructor before it executes any code inside itself.

Just yesterday, the CP member ProgramFox, in response to a QA question here, showed me that you can even redirect execution from the parameterless Constructor to a Constructor with parameters as a way of setting default values:
public Class1() : this(0, String.Empty)

public Class1(int x, string y)
If you wrote in your code Class1 newClass1 = new Class1();

Then the parameterless constructor would execute code in the Constructor with parameters before it executed code in its own body. Notice that in the Constructor with parameters in this example, that we removed the expression ": this()" ... if we didn't remove that you would get a StackOverflow error because the two Constructors would keep calling themselves.

This example of the parameterless Constructor forcing the execution of a Constructor with parameters is, I would guess, quite rare.

Finally, as if you didn't have enough tools to use in creating Constructors for every purpose: you can use optional parameters in Constructor parameter lists; for example:
public Class1(string y, int x, bool z1, bool z2, long l = 123456789, string s = "what ?"){}


Note that here, at the end of the parameter list are two parameters "l" and "s" that are each followed by an equal-sign and a default value.

Now you could call any of:
C#
// both optional parameters omitted
Class1 myClass1 = new Class1("wow", 1, true, true);

// both optional parameters supplied
Class1 myClass1 = new Class1("wow", 1, true, true, 987654321, "whoa");

// one optional parameter supplied, one optional parameter omitted
Class1 myClass1 = new Class1("wow", 1, true, true, "whoa");

// using special named parameter: notation to allow entering parameters in any order
Class1 myClass1 = new Class1(s: "wow", z2:false, l: 987654321, x:100, y:"where ?", z1: false);
All this "freedom" can be intoxicating, but, in practice, most of the time you probably will create multiple Constructors for a good reason: you need to get different sorts of data into the instance of the Class you create ... in different circumstances.

Making sure you understand the parameter order- and type- matching the .NET compiler does in order to determine which Constructor is invoked, and also understanding the use of the ": this(...)" directive are most important.
 
Share this answer
 
Comments
Matt T Heffron 29-Oct-13 13:56pm    
+5
Well, to be fair it looks pretty pointless. But the jist of what is going on is that the Time2 class has multiple constructors.

The first line is passing the values of h, m and s to a different constructor in the class.
However, looking at what you have typed this would never work and would never compile because the two constructors for Time2 are identical.
 
Share this answer
 
Comments
Elham.Deljooei 29-Oct-13 4:24am    
Thanks my friend,
I couldn't find it probably, Could you explain it a little more?
Pheonyx 29-Oct-13 4:34am    
What do you mean? You asked me what the difference was, that is the difference.
Pheonyx 29-Oct-13 4:35am    
Have a read of this site:

http://msdn.microsoft.com/en-us/library/ms173115(v=vs.90).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900