当前位置: 移动技术网 > IT编程>开发语言>c# > [译]C# 7系列,Part 6: Read-only structs 只读结构

[译]C# 7系列,Part 6: Read-only structs 只读结构

2019年12月12日  | 移动技术网IT编程  | 我要评论

原文:

背景

在.net世界中,有两种基本类型:引用类型和值类型。简单地说,引用类型是可以继承/扩展的类,当传递引用类型对象时,传递的是一个“指针”;值类型是不能继承/扩展的结构,当传递值类型对象时,传递的是一个“副本”。

c#中的struct是一个值类型,它“内部继承”自system.valuetype。(我说的是结构之间没有继承。)

当在参数中使用struct时,会生成struct的副本,使用struct可能是高效的,因为它减少了堆对象分配的时间。

在许多场景中,开发人员使用结构作为传递值的有效方法,例如方法的返回对象,或者跨应用程序使用的基本数据结构。

只读结构

只读结构是其公共成员为只读的结构,就好像“this”变量一样。

看一下下面的声明:

public struct s
{
    public int age { get; set; }
    public string name { get; set; }

    public s(int age, string name)
    {
        this.age = age;
        this.name = name;
    }

    public s(s other)
    {
        this = other;
    }

    public s replace(s other)
    {
        s value = this;
        this = other;
        return value;
    }
}

可以看到,我可以完全访问已声明的属性age和name,还可以访问this变量,这样就可以用另一个实例s来替换this实例。

如果我在声明中添加readonly修饰符,我的访问权限将受到限制:

  • 所有的成员(属性、字段)必须是自读;
  • 我需要在公共的有参构造函数中初始化成员;
  • 除了在构造函数中,“this”变量在其他地方都是只读的;
  • 你不能定义“类字段”事件;

下面的截图显示了上面的代码改成了readonly struct后需要修正的地方。

下面是修改后的代码:

public readonly struct s
{
    public int age { get; }
    public string name { get; }

    public s(int age, string name)
    {
        this.age = age;
        this.name = name;
    }

    public s(s other)
    {
        this = other;
    }
}

你可以像往常一样初始化s的新实例。但是你不能修改任何实例的任何成员。你应该总是调用有参(而不是无参)构造函数来正确初始化实例对象,否则您将获得实例的默认值(所有成员都被初始化为成员类型的默认值)。

private static void test()
{
    s s = new s(18, "anna");
    ref s other = ref s;
    other = new s(other);
    bool equal = s.equals(other); // true.
}

结论

只读结构是一个方便的特性,可以帮助保护你的值被意外修改的影响;与其他新特性相结合(例如,ref结构和in参数),它将使你的c#代码更容易地面向低级别的编程。在接下来的几篇文章中,我将解释所有这些新事物。请注意,你需要c# 7.2才能使用这个特性,它在visual studio 2017.5 preview 4或更高版本中可用。

 

系列文章:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网