Sử dụng ref và Tham số ra Khi bạn vượt qua một đối số cho một phương pháp, các tham số tương ứng được khởi tạo với một bản sao của các đối số. Điều này đúng cho dù tham số này là một loại giá trị (như int một), | Using ref and out Parameters When you pass an argument to a method the corresponding parameter is initialized with a copy of the argument. This is true regardless of whether the parameter is a value type such as an int or a reference type such as a WrappedInt . This arrangement means it s impossible for any change to the parameter to affect the value of the argument passed in. For example in the following code the value output to the console is 42 and not 43. The DoWork method increments a copy of the argument arg and not the original argument static void DoWork int param param static void Main int arg 42 DoWork arg arg writes 42 not 43 In the previous exercise you saw that if the parameter to a method is a reference type then any changes made by using that parameter change the data referenced by the argument passed in. The key point is that although the data that was referenced changed the parameter itself did not it still referenced the same object. In other words although it is possible to modify the object that the argument refers to through the parameter it s not possible to modify the argument itself for example to set it to refer to a completely new object . Most of the time this guarantee is very useful and can help to reduce the number of bugs in a program. Occasionally however you might want to write a method that actually needs to modify an argument. C provides the ref and out keywords to allow you to do this. Creating ref Parameters If you prefix a parameter with the ref keyword the parameter becomes an alias for or a reference to the actual argument rather than a copy of the argument. When using a ref parameter anything you do to the parameter you also do to the original argument because the parameter and the argument both reference the same object. When you pass an argument to a ref parameter you must also prefix the argument with the ref keyword. This syntax provides a useful visual indication that the argument might change. Here s