C#.net 提供的4个关键字,in,out,ref,paras开发中会经常用到,那么它们如何使用呢? 又有什么区别? 1 in in只用在委托和接口中: 例子: 1 2 3 4 5 6 7 8 9 10 11 12 //测试模型 class Model { public int a { get; set; } public Model(int a) {
ref 的功能,在 react 当中.我们写了一个组件,在这个时候,我们的 render function 里面我们会渲染一系列的子组件或者 dom 节点,有时候我们会希望有这样的需求,就是我们要获取某个 dom 节点,或者是某个子组件的实例.去对他进行一些手动的操作,而不仅仅是 props 更新这种方式去更新这个节点.比如我们要获取一个 dom 节点,自己去绑定某一些事件,然后去做一些操作.通过 ref 这个特殊的 attr 非常方便的在组件内部去获取到子节点的具体的一个实例.这就是 ref
引例: 先看这个源码,函数传递后由于传递的是副本所以真正的值并没有改变. 源码如下: using System; using System.Collections.Generic; using System.Text; namespace refout参数学习 { class Program { static void Main(string[] args) { ; IncAge(age); Console.WriteLine(age);//打印结果还是20 Console.ReadKey();
首先,如果不使用这两个关键字,那是什么样 呢? 看下面的例子: using System; class Test { static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } static void Swap(int x,int y) { int temp = x; x = y; y = temp; } static void Main() {