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. Ref Key word

Ref Key word

Scheduled Pinned Locked Moved C#
4 Posts 3 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.
  • I Offline
    I Offline
    IamADotNetGuy
    wrote on last edited by
    #1

    As we know we can pass the parameters by ref and also by val. Sometimes we will pass object as reference type even though they are already reference type. Please feel free to say dont be stupid

    C 1 Reply Last reply
    0
    • I IamADotNetGuy

      As we know we can pass the parameters by ref and also by val. Sometimes we will pass object as reference type even though they are already reference type. Please feel free to say dont be stupid

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      You need to pass an object by ref, if the method you're passing it to will replace it with another object. This example is useless, but it shows the effect: private void DummyReplaceTwoStrings(String s1, String s2){ s1 = "first text"; s2 = "second text"; } private void ReplaceTwoStrings(ref String s1, ref String s2){ s1 = "first text"; s2 = "second text"; } private void CallerMethod(){ String s1 = "hello"; String s2 = "world"; DummyReplaceTwoStrings(s1, s2); Console.WriteLine(s1); Console.WriteLine(s2); ReplaceTwoStrings(ref s1, ref s2); Console.WriteLine(s1); Console.WriteLine(s2); } _________________________________ Vote '1' if you're too lazy for a discussion

      L 1 Reply Last reply
      0
      • C Corinna John

        You need to pass an object by ref, if the method you're passing it to will replace it with another object. This example is useless, but it shows the effect: private void DummyReplaceTwoStrings(String s1, String s2){ s1 = "first text"; s2 = "second text"; } private void ReplaceTwoStrings(ref String s1, ref String s2){ s1 = "first text"; s2 = "second text"; } private void CallerMethod(){ String s1 = "hello"; String s2 = "world"; DummyReplaceTwoStrings(s1, s2); Console.WriteLine(s1); Console.WriteLine(s2); ReplaceTwoStrings(ref s1, ref s2); Console.WriteLine(s1); Console.WriteLine(s2); } _________________________________ Vote '1' if you're too lazy for a discussion

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Corinna John wrote: ReplaceTwoStrings(s1, s2); ReplaceTwoStrings(ref s1, ref s2); :-> top secret
        Download xacc-ide 0.0.3 now!
        See some screenshots

        C 1 Reply Last reply
        0
        • L leppie

          Corinna John wrote: ReplaceTwoStrings(s1, s2); ReplaceTwoStrings(ref s1, ref s2); :-> top secret
          Download xacc-ide 0.0.3 now!
          See some screenshots

          C Offline
          C Offline
          Corinna John
          wrote on last edited by
          #4

          Better now? You've won a personal extra-ref: r e f _________________________________ Vote '1' if you're too lazy for a discussion

          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