find a location for property in a new city

Wednesday, 7 December 2016

Reference object with ref keyword on a method. What's the point?

I wondered the other day what the point is in making a method parameter a ref when it is already a reference type. After playing around for a bit I came up with some concise code to illustrate the difference.

The difference is that, although you can modify the reference object that was passed in with both methods. Changing the reference entirely (i.e. pointing to a different object) only has an effect outside the scope of the method if the object was passed through with the ref keyword.

This code should explain better than English:

void Main()
{
 var t1 = new Thing();
 Test(t1);
 t1.Write(); // Chair
 
 var t2 = new Thing();
 Test(ref t2);
 t2.Write(); // Table
}

void Test(Thing t)
{
 t.Name = "Chair";
 t = new Thing { Name = "Table" };
}

void Test(ref Thing t)
{
 t.Name = "Chair";
 t = new Thing { Name = "Table" };
}

class Thing
{
 public string Name { get; set; }
 public void Write() => Console.WriteLine(Name);
}

Follow britishdev on Twitter

30 comments:

  1. I understand what you bring it very meaningful and useful, thanks


    mario | free mahjong  |   atari breakout

    ReplyDelete
  2. Informative post! I really amazed to see the great information that you have shared with us. I want to get more updates to increase my knowledge. Keep doing the great work up. trustable plumbers dallas

    ReplyDelete
  3. Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here. five nights at freddy’s 4 | word cookies answers | hotmail sign in | hill climb racing 2

    ReplyDelete
  4. Thank you for this valuable information. The information you shared is very interesting. Brave Technologies is an leading erp software development company in chennai.
    enterprise resource planning chennai | web development company in chennai

    ReplyDelete
  5. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
    animal jam 2 | five nights at freddys 2 | hotmail login

    ReplyDelete
  6. This is a wonderful post. I enjoyed the information lot. I will bookmark this page. Thanks for sharing this information.
    starfall | barney | abcya

    ReplyDelete
  7. Thanks for sharing.for more information : click here

    ReplyDelete
  8. Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here.
    potty-racers2.com

    ReplyDelete
  9. Here you can find some info on how to Compare and Contrast Essay and how to do it right

    ReplyDelete
  10. Thanks for the information I like to top article We provides list of verified get free quotes # Fooglepackers.com

    packers and movers pune
    packers and movers gurgaon
    packers and movers bangalore
    packers and movers noida

    ReplyDelete
  11. Thanks the information your best article provides list verified free quotes compare to save money & select the best # www.fooglepackers.com


    packers and movers delhi
    packers and movers mumbai
    packers and movers hyderabad
    packers and movers chennai

    ReplyDelete
  12. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    suchmaschinenmarketing lüdenscheid

    ReplyDelete
  13. We are top 5 mover and packer in Pune. Any problems are biggest definitely easier determine process by possible will flexion any workers are hard working with in your any time of the work him for her to the shifting relocation Foogle Packers Pune. List of packers movers in Pune with are process all over about tell me know as one by one services all over benefit from India is a very competition for the market in your competitor in all time location in Pune.

    http://www.fooglepackers.com/packersmovershyderabad.html

    http://www.fooglepackers.com/packersmoverspune.html

    http://www.fooglepackers.com/packersmoversmumbai.html

    ReplyDelete
  14. making a method parameter a ref when it is already a reference type
    ---------------------------------------------------------------------
    شركة تركيب طارد حمام بالرياض

    ReplyDelete
  15. Thanks for sharing this marvelous post. I m very pleased to read this article.Sign in pogo

    ReplyDelete
  16. Play Online Flash games What we call Free flash Games
    happy wheels is an extremely popular obstacle game developed by Fancy Force, Jim Bonacci and Jack Zankowski. This is a great game

    for you to experience.happy wheels is one of the best game all over internet In this web-based version, you will play Demo

    mode. There will be maps built by other players.Enjoy happy wheels game At the start of the game, you will have to take

    control of your character through dangerous paths, obstacles and deadly traps to reach the finish safely.Enjoy at the same time fireboy and watergirl game online as well

    ReplyDelete

  17. Before you plan to install antivirus software on your device, you are required to take
    few important steps to avoid software conflicts with the previously installed versions. please visit
    norton activation
    or directly visit
    norton.com/setup .
    you can find more topics there.We love to help you.

    ReplyDelete
  18. great informative post and A ref parameter is passed as a reference, not a value. This means you can assign the parameter in the called method and have it also be assigned at the calling site. Parameters. Difference. There is a difference between ref and out.coursework Writing Service

    ReplyDelete
  19. norton.com/setup - Online Help – Step by Step guide for Norton Setup, Download & complete installation online. We are providing independent support service if in case you face problem to activate or Setup Norton product.

    ReplyDelete
  20. office.com/setup - Let's Get Started with your Microsoft Office Setup by visiting www.office.com/setup and Enter 25-digit Activation Code to Setup Office Today. Feel free to contact us for any help.Office.com/setup

    ReplyDelete
  21. office.com/setup - Let's Get Started with your Microsoft Office Setup by visiting www.office.com/setup and Enter 25-digit Activation Code to Setup Office Today. Feel free to contact us for any help.

    ReplyDelete
  22. office.com/setup, office.com/verify, www office com setup, office.com/myaccount, Office Com Setup, office.com/productkeycard, Office Setup, setup.office.com, Office Com Set Up, office 2017 download, Office Setup Enter Product Key, office 2016 download, Office Setup Enter Key, office for mac download, Office Com Setup Enter Key

    ReplyDelete
  23. Thanks for writing such a great post. I really enjoyed reading that content.
    hooda games

    ReplyDelete
  24. Our technical experts will offer you the help to download, install and reinstall the McAfee product on your system. McAfee also provides the support to resolve the key related errors as well as the issues related to system performance.
    mcafee.com/activate

    Office.com/setup – Let’s Get Started with Office Setup Installation by simply following the steps discussed below. You may easily activate Office with the help of Office Com Setup link.
    Office.com/setup

    ReplyDelete