question on using ref keyword
-
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
-
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
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
-
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
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
-
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
-
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 ?
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; }