在C#中vnext主要建设者 [英] Primary constructors in C# vnext

查看:135
本文介绍了在C#中vnext主要建设者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助语言功能实现状态是通过<一个提醒我注意HREF =htt​​p://chat.stackoverflow.com/transcript/message/15681824#15681824> C#聊天。我有一对夫妇的一些不包括在这个存在的问题。



一些新的功能是显而易见的,如消气仅自动性能

 公众诠释Ÿ{搞定; } = Y; 



...真棒。



不过,别人我不是...



清楚


主要构造



 类Point(INT X,int y)对{...} 

这可能是我在知识的差距,但我还没有跨越主构造一词来之前,但的一些研究似乎表明,这基本上是定义以下

 <$ C $快捷方式C>类Point 
{
私人INT X;
私人诠释Ÿ;

公共点(INT X,int y)对
{
this.x = X;
this.y = Y;
}
}




  • 这是正确的解释?

  • 这是否意味着你仍然可以做构造函数重载(术语主指的话)?

  • 您可以参考这些暗中支持字段其他构造函数/方法?


解决方案


  • 这是正确的解释?




差不多,但不完全是。默认情况下,主构造函数的参数没有捕获到领域;他们,然而,在实例成员初始化访问(只有那里),这样你就可以明确地初始化字段或属性与他们:

 类点(INT X,int y)对
{
公众诠释X {搞定; } = X;
公众诠释Ÿ{搞定; } = Y;
}



(注意,在这种情况下,背衬字段创建的,但对于属性,而不是构造参数)



但是也可以声明与访问修饰符的参数,在这种情况下,他们将被捕获为字段,和从所有实例成员访问:

 类点(公共只读INT X,公共只读int y)对
{
}

(正如你所看到的,你也可以指定其他修饰符的领域里,如只读





  • 这是否意味着你仍然可以做构造函数重载(术语主指的话)?




是的,你可以有其他,非主构造函数。但是,他们都需要调用主构造,与通常的:这个(...)语法





  • 您可以参考其他构造函数/方法,这些暗中支持字段?




正如上面提到的,有没有隐式后备字段;如果你明确地指定在主构造函数的参数访问修饰符的支持字段仅创建






编辑:此功能被撤回,并不会在C#6(可能在C#7,以稍微不同的形式)


The Language feature implementation status was brought to my attention via C# Chat. I have a couple of questions about some of the features not covered in this existing question.

Some of the new features are obvious such as Getter-only auto-properties

public int Y { get; } = y;

... awesome.

But others I am not clear on...


Primary constructors

class Point(int x, int y) { … }

It may be a gap in my knowledge, but I haven't come across the term "primary constructor" before, but some research seems to suggest that basically this is a shortcut for defining the following

class Point
{
    private int x;
    private int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

  • Is this the correct interpretation?
  • Does this mean you can still do constructor overloads (the term "primary" implies so)?
  • Can you refer to these implicit backing fields in other constructors/methods?

解决方案

  • Is this the correct interpretation?

Almost, but not exactly. By default, primary constructor parameters are not captured into fields; they are, however, accessible in instance member initializers (and only there), so you can explicitly initialize fields or properties with them:

class Point(int x, int y)
{
    public int X { get; } = x;
    public int Y { get; } = y;
}

(note that in this case, backing fields are created, but for the properties, not the constructor parameters)

But you can also declare the parameters with access modifiers, in which case they will be captured as fields, and be accessible from all instance members:

class Point(public readonly int x, public readonly int y)
{
}

(as you can see, you can also specify other modifiers for the fields, like readonly)

  • Does this mean you can still do constructor overloads (the term "primary" implies so)?

Yes, you can have other, non-primary constructors. However, they're all required to call the primary constructor, with the usual : this(...) syntax.

  • Can you refer to these implicit backing fields in other constructors/methods?

As mentioned above, there are no implicit backing fields; the backing field is only created if you explicitly specify an access modifier on the primary constructor parameter.


EDIT: this feature has been withdrawn and won't be in C# 6 (probably in C# 7, in a slightly different form)

这篇关于在C#中vnext主要建设者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆