Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Is there a difference?

Is there a difference?

Scheduled Pinned Locked Moved C#
question
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sps itsec46

    Is there a difference between MyMethod1() and MyMethod2()? First method doesn't use this, 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

    R Offline
    R Offline
    RNEELY
    wrote on last edited by
    #2

    No. Sincerely, -Ron

    1 Reply Last reply
    0
    • S sps itsec46

      Is there a difference between MyMethod1() and MyMethod2()? First method doesn't use this, 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

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #3

      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
      ret

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • S sps itsec46

        Is there a difference between MyMethod1() and MyMethod2()? First method doesn't use this, 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

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #4

        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

        B D 2 Replies Last reply
        0
        • C Colin Angus Mackay

          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

          B Offline
          B Offline
          boogs
          wrote on last edited by
          #5

          boogs guesses c) -> arguments take precedence over members. and no, i didn't test it - that would be cheating ;)

          B 1 Reply Last reply
          0
          • B boogs

            boogs guesses c) -> arguments take precedence over members. and no, i didn't test it - that would be cheating ;)

            B Offline
            B Offline
            boogs
            wrote on last edited by
            #6

            d0h, it was b). for the same reason.

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              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

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #7

              :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

              C 1 Reply Last reply
              0
              • D Dave Kreskowiak

                :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

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #8

                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

                1 Reply Last reply
                0
                • S sps itsec46

                  Is there a difference between MyMethod1() and MyMethod2()? First method doesn't use this, 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

                  M Offline
                  M Offline
                  Meysam Mahfouzi
                  wrote on last edited by
                  #9

                  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...

                  S 1 Reply Last reply
                  0
                  • M Meysam Mahfouzi

                    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...

                    S Offline
                    S Offline
                    sps itsec46
                    wrote on last edited by
                    #10

                    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

                    M 1 Reply Last reply
                    0
                    • S sps itsec46

                      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

                      M Offline
                      M Offline
                      Meysam Mahfouzi
                      wrote on last edited by
                      #11

                      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...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups