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. How do you pass by reference in c#?

How do you pass by reference in c#?

Scheduled Pinned Locked Moved C#
questioncsharp
2 Posts 2 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.
  • H Offline
    H Offline
    honeyman_can
    wrote on last edited by
    #1

    hi guys! I'm a newbie to c# Method signature: public static string Translate(string translationCode, string culture, ref Dictionary> input) I want to pass a dictionary by reference and not the entire object. Is it correct to use 'ref' in the argument? Then how do I insert the dictionary when calling the method? thanks

    G 1 Reply Last reply
    0
    • H honeyman_can

      hi guys! I'm a newbie to c# Method signature: public static string Translate(string translationCode, string culture, ref Dictionary> input) I want to pass a dictionary by reference and not the entire object. Is it correct to use 'ref' in the argument? Then how do I insert the dictionary when calling the method? thanks

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Reference types (objects) are never copied when you use them in a call. It's always the reference to the object that is sent to the method. If you use the ref keyword with a reference type, you will be sending a reference to the reference. Parameters are always sent by value unless you specify the ref keyword. For reference types that means that the value of the referecne is passed to the method. Example:

      void Test(string a, ref string b) {
      a = "1";
      b = "2";
      Console.WriteLine("a: " + a);
      Console.WriteLine("b: " + b);
      }

      string x = "x";
      string y = "y";
      Console.WriteLine("x :" + x);
      Console.WriteLine("y :" + y);
      Test(x, ref y);
      Console.WriteLine("x :" + x);
      Console.WriteLine("y :" + y);

      The output will be: x: x y: y a: 1 b: 2 x: x y: 2

      --- b { font-weight: normal; }

      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