Is there a difference?
-
Is there a difference between
MyMethod1()
andMyMethod2()
? First method doesn't usethis
, second method does use it. Same thing in constructor:public class MyClass
{
int i1, i2;
MyClass()
{
i1 = 10;
this.i2 = 20;
}
int MyMethod1()
{
int iRet = i1 + i2;
return iRet;
}
int MyMethod2()
{
int iRet = this.i1 + this.i2;
return iRet;
}
}Regards, mYkel
-
Is there a difference between
MyMethod1()
andMyMethod2()
? First method doesn't usethis
, second method does use it. Same thing in constructor:public class MyClass
{
int i1, i2;
MyClass()
{
i1 = 10;
this.i2 = 20;
}
int MyMethod1()
{
int iRet = i1 + i2;
return iRet;
}
int MyMethod2()
{
int iRet = this.i1 + this.i2;
return iRet;
}
}Regards, mYkel
-
Is there a difference between
MyMethod1()
andMyMethod2()
? First method doesn't usethis
, second method does use it. Same thing in constructor:public class MyClass
{
int i1, i2;
MyClass()
{
i1 = 10;
this.i2 = 20;
}
int MyMethod1()
{
int iRet = i1 + i2;
return iRet;
}
int MyMethod2()
{
int iRet = this.i1 + this.i2;
return iRet;
}
}Regards, mYkel
To explain why RNEELY simple answered "no", it's because the compiler assumes that any un-qualified calls use the
this
reference anyway. It's an implicit object. When it compiles, the exact same Intermediate Language (IL, the language embedded in modules of which an assembly is partly comprised) is produced. In both cases, the optimized body of each method would look something like this:ldfld int32 MyClass::i1
ldfld int32 MyClass::i2
add
retMicrosoft MVP, Visual C# My Articles
-
Is there a difference between
MyMethod1()
andMyMethod2()
? First method doesn't usethis
, second method does use it. Same thing in constructor:public class MyClass
{
int i1, i2;
MyClass()
{
i1 = 10;
this.i2 = 20;
}
int MyMethod1()
{
int iRet = i1 + i2;
return iRet;
}
int MyMethod2()
{
int iRet = this.i1 + this.i2;
return iRet;
}
}Regards, mYkel
Okay... Here's a question for you... [Edit] Ooops! The original version of this program had an error in it that I didn't intend. Here is the correction, the multiple choice answers were as before. [/Edit] What is the output of this program:
01: class App
02: {
03: int i1 = 0;
04: int i2 = 0;
05:
06: public void Method1()
07: {
08: i1 = 2;
09: i2 = 3;
10: }
11:
12: public void Method2(int i2)
13: {
14: i1 = i2;
15: }
16:
17: public static void Main()
18: {
19: App a = new App();
20: a.Go();
21: }
22:
23: public void Go()
24: {
25: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
26: Method1();
27: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
28: Method2(5);
29: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
30: }
31: }Is it: a) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 3, i2 = 3 b) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 3 c) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 5 d) None of the above - it generates a compiler error on line 12
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Okay... Here's a question for you... [Edit] Ooops! The original version of this program had an error in it that I didn't intend. Here is the correction, the multiple choice answers were as before. [/Edit] What is the output of this program:
01: class App
02: {
03: int i1 = 0;
04: int i2 = 0;
05:
06: public void Method1()
07: {
08: i1 = 2;
09: i2 = 3;
10: }
11:
12: public void Method2(int i2)
13: {
14: i1 = i2;
15: }
16:
17: public static void Main()
18: {
19: App a = new App();
20: a.Go();
21: }
22:
23: public void Go()
24: {
25: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
26: Method1();
27: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
28: Method2(5);
29: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
30: }
31: }Is it: a) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 3, i2 = 3 b) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 3 c) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 5 d) None of the above - it generates a compiler error on line 12
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
boogs guesses c) -> arguments take precedence over members. and no, i didn't test it - that would be cheating ;)
-
Okay... Here's a question for you... [Edit] Ooops! The original version of this program had an error in it that I didn't intend. Here is the correction, the multiple choice answers were as before. [/Edit] What is the output of this program:
01: class App
02: {
03: int i1 = 0;
04: int i2 = 0;
05:
06: public void Method1()
07: {
08: i1 = 2;
09: i2 = 3;
10: }
11:
12: public void Method2(int i2)
13: {
14: i1 = i2;
15: }
16:
17: public static void Main()
18: {
19: App a = new App();
20: a.Go();
21: }
22:
23: public void Go()
24: {
25: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
26: Method1();
27: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
28: Method2(5);
29: Console.WriteLine("i1 = {0}, i2 = {1}", i1, i2);
30: }
31: }Is it: a) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 3, i2 = 3 b) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 3 c) i1 = 0, i2 = 0 i1 = 2, i2 = 3 i1 = 5, i2 = 5 d) None of the above - it generates a compiler error on line 12
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
:doh: E) None of the above The signature for Main is an invalid one for an entry point. Also, changing it to static would preclude Main from accessing the public members of the App class without first creating an instance of the class. :laugh: RageInTheMachine9532
-
:doh: E) None of the above The signature for Main is an invalid one for an entry point. Also, changing it to static would preclude Main from accessing the public members of the App class without first creating an instance of the class. :laugh: RageInTheMachine9532
Okay - let's assume I didn't make that error... :doh:
EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
-
Is there a difference between
MyMethod1()
andMyMethod2()
? First method doesn't usethis
, second method does use it. Same thing in constructor:public class MyClass
{
int i1, i2;
MyClass()
{
i1 = 10;
this.i2 = 20;
}
int MyMethod1()
{
int iRet = i1 + i2;
return iRet;
}
int MyMethod2()
{
int iRet = this.i1 + this.i2;
return iRet;
}
}Regards, mYkel
You know,
this
keyword is mostly used whenever in a function, the passed in parameter has a name the same as the name of a field:private string name;
public void SetName(string name)
{
this.name = name;//this.name refers to private field
}
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:
Click Here![^]
I'm thirsty like sun, more landless than wind...
-
You know,
this
keyword is mostly used whenever in a function, the passed in parameter has a name the same as the name of a field:private string name;
public void SetName(string name)
{
this.name = name;//this.name refers to private field
}
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:
Click Here![^]
I'm thirsty like sun, more landless than wind...
Thanks for your comment... what you say makes totally sense! :) OT: You should check the link to the murphy page in your signature it's not "http://www.thecodeproject.com/..." but "http://www.codeproject.com/...". Glad I could help you too :-D Regards, mYkel
-
Thanks for your comment... what you say makes totally sense! :) OT: You should check the link to the murphy page in your signature it's not "http://www.thecodeproject.com/..." but "http://www.codeproject.com/...". Glad I could help you too :-D Regards, mYkel
It's really pleasure that it made sense!;) BTW, thanks for notification about signature
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:
Click Here![^]
I'm thirsty like sun, more landless than wind...