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. question on using ref keyword

question on using ref keyword

Scheduled Pinned Locked Moved C#
question
5 Posts 4 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.
  • P Offline
    P Offline
    ppp001
    wrote on last edited by
    #1

    Hi all, I have tested using ref in passing reference to method, e.g. Car car1 = new Car(); Change(ref car); ... void Change(ref Car car) { car.id = 1; //car = null; //car = new Car(); } it seem that by passing car by ref can null the reference of the car, or change the reference of the car, it can do more that just by Change(Car car) (i.e. without the ref keyword. Then why we not always add ref keywords to the method, so more capabilities are achieved ? What are the disadvantages of using ref ?? Thanks

    L G 2 Replies Last reply
    0
    • P ppp001

      Hi all, I have tested using ref in passing reference to method, e.g. Car car1 = new Car(); Change(ref car); ... void Change(ref Car car) { car.id = 1; //car = null; //car = new Car(); } it seem that by passing car by ref can null the reference of the car, or change the reference of the car, it can do more that just by Change(Car car) (i.e. without the ref keyword. Then why we not always add ref keywords to the method, so more capabilities are achieved ? What are the disadvantages of using ref ?? Thanks

      L Offline
      L Offline
      levgiang
      wrote on last edited by
      #2

      The disadvantages of ref keyword is it can change the reference of car.There is a situation we don't want change the reference of car,so we don't need use the ref keyword. When you want pass type value parameters and want change the variable pass to method ,you use the ref keyword.and if you dont't want change the variables pass to method,you shoudn't use the ref keyword. le van giang

      P 1 Reply Last reply
      0
      • P ppp001

        Hi all, I have tested using ref in passing reference to method, e.g. Car car1 = new Car(); Change(ref car); ... void Change(ref Car car) { car.id = 1; //car = null; //car = new Car(); } it seem that by passing car by ref can null the reference of the car, or change the reference of the car, it can do more that just by Change(Car car) (i.e. without the ref keyword. Then why we not always add ref keywords to the method, so more capabilities are achieved ? What are the disadvantages of using ref ?? Thanks

        G Offline
        G Offline
        Gavin Jeffrey
        wrote on last edited by
        #3

        http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfRef.asp[^] Good question - two things I found were 1)An argument passed to a ref parameter must first be initialized 2)A property is not a variable and cannot be passed as a ref parameter

        1 Reply Last reply
        0
        • L levgiang

          The disadvantages of ref keyword is it can change the reference of car.There is a situation we don't want change the reference of car,so we don't need use the ref keyword. When you want pass type value parameters and want change the variable pass to method ,you use the ref keyword.and if you dont't want change the variables pass to method,you shoudn't use the ref keyword. le van giang

          P Offline
          P Offline
          ppp001
          wrote on last edited by
          #4

          Thanks But it seems there are no harm to change the reference by ref. And this add more functionality by using ref (except we cant pass property and not initialized object reference to it ). So, in what other situation that it is harmful to use ref ?

          G 1 Reply Last reply
          0
          • P ppp001

            Thanks But it seems there are no harm to change the reference by ref. And this add more functionality by using ref (except we cant pass property and not initialized object reference to it ). So, in what other situation that it is harmful to use ref ?

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

            In any case when changing the reference will make you lose the reference to an object that you need. Example: You create a SqlParameter object and add it to a SqlCommand object. Now the command object also has a reference to the parameter. You send your reference to a function that replaces the parameter with a new parameter. You execute the command, which will place data in the parameter you added to it. You try to retrive the data using your reference to the parameter, but as it no longer references the correct parameter, you won't get the data. --- 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