本文介绍了C#中的ref和out关键字有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好!任何人都可以告诉我ref和out关键字的基本区别吗?hi everyone!Can any one tell me the basic difference the ref and out keywords?推荐答案class OutExample{ static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 }}class RefExample { static void Method(ref int i) { // Rest the mouse pointer over i to verify that it is an int. // The following statement would cause a compiler error if i // were boxed as an object. i = i + 44; } static void Main() { int val = 1; Method(ref val); Console.WriteLine(val); // Output: 45 } }Although variables passed as an out arguments need not be initialized prior to being passed, the calling method is required to assign a value before the method returns.The ref and out keywords are treated differently at run-time, but they are treated the same at compile time. Therefore methods cannot be overloaded if one method takes a ref argument and the other takes an out argument. These two methods, for example, are identical in terms of compilation, so this code will not compile.class CS0663_Example{ // compiler error CS0663: "cannot define overloaded // methods that differ only on ref and out" public void SampleMethod(out int i) { } public void SampleMethod(ref int i) { }}Overloading can be done, however, if one method takes a ref or out argument and the other uses neither, like this:class RefOutOverloadExample{ public void SampleMethod(int i) { } public void SampleMethod(out int i) { }}void funcA(out int a){ a = 0; // at-least one statement should be there that // defines the value for the out parameter // otherwise compiler will report an error ...}void funcB() // calling function{ ... int a; funcA(out a); // note: value of a is not initialized. ...} 关键字 ref 是从函数传递并获取修改后的值。 1.您需要先修改变量你调用函数。 2.函数可能会也可能不会改变ref参数的值(如果它没有改变任何东西,那么目的是什么?)The keyword ref is to pass and get modified value from a function.1. You need to initialize the variable before you call the function.2. Function may or may not change the value of the ref parameter (if it doesn't change any thing then whats the purpose?)void funcA(ref int a){ ... a = a + 1; //modifying the value ...}void funcB() // calling function{ ... int a = 1; funcA(ref a); // note: value of a is not initialized. // now a = 2 ...} 覆盖只有差异关键字的方法,如下所示,将导致编译时错误无法定义仅在ref和out上有所不同的重载方法Overriding of methods with only difference keyword, as shown below, will lead to compile time error "cannot define overloaded methods that differ only on ref and out"static public void funcA(out int a){ a = 1;}static public void funcA(ref int a){ a = a + 1;} 我们可以为out参数函数赋值在调用者函数中。但是,这将导致编译时异常使用未分配的参数aWe can assign a value to the out parameter function in the caller function. However, this will lead to compile time exception "Use of unassigned out parameter a"namespace ConsoleApplication1{ class Program { static public void funcA(out int a) { if (a == 1) a = 2; else a = 1; } static void Main(string[] args) { int a = 1; funcA(out a); } }} HTHHTH 这篇关于C#中的ref和out关键字有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 09:00