编程语言
首页 > 编程语言> > C# 中的 ref 已经被放开,或许你已经不认识了

C# 中的 ref 已经被放开,或许你已经不认识了

作者:互联网

一:背景

1. 讲故事

最近在翻 netcore 源码看,发现框架中有不少的代码都被 ref 给修饰了,我去,这还是我认识的 ref 吗?就拿 Span 来说,代码如下:


    public readonly ref struct Span<T>
    {
        public ref T GetPinnableReference()
        {
            ref T result = ref Unsafe.AsRef<T>(null);
            if (_length != 0)
            {
                result = ref _pointer.Value;
            }
            return ref result;
        }

        public ref T this[int index]
        {
            get
            {
                return ref Unsafe.Add(ref _pointer.Value, index);
            }
        }             
    }

是不是到处都有 ref,在 struct 上有,在 local variable 也有,在 方法签名处 也有,在 方法调用处 也有,在 属性 上也有, 在 return处 也有,简直是应有尽有,太

标签:struct,nums,C#,int,IL,放开,ref,public
来源: https://www.cnblogs.com/huangxincheng/p/13939828.html