为什么在C#这成员的初始值是不允许的,但在VB.Net我被允许 [英] Why in C# this is not allowed in member initializer, but in VB.Net Me is allowed

查看:127
本文介绍了为什么在C#这成员的初始值是不允许的,但在VB.Net我被允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换一个VB.Net应用到C#,并已注意到,在VB.Net代码中,有一个私有成员变量,这是使用初始化是这样的:

 私人m_ClassA作为新MyCollection的(中ClassA的)(我)

当我将它转换为C#这样的代码:

 私人MyCollection的<&的ClassA GT; _classA =新MyCollection的<&的ClassA GT;(本); 



我有误差




参数,而参数的类型是 REF




如果我把 REF 中的参数前面这个,我得到




不能使用这个在成员初始化错误。




我在这里读了成员的基类初始化之前,等等这个不能在成员使用,因为它可能尚未被初始化。我的问题是,为什么它是合法的在VB.Net,而不是C#?



这是下到编译器的不同处理呢?这似乎不可思议,这两个具有不同的行为。



要避开它我想我会在初始化的构造器的成员。


< DIV CLASS =h2_lin>解决方案

MSDN




一个这种访问只允许在一个实例
构造函数,实例方法或实例访问的块。




这可以在这里阅读



您无法访问此随地真的比实例/构造等。所以,你不能做这样的事情之一:

 公共类Foo 
{
私人美孚_foo =这;
}



正如你所说,在 C#你将不得不使用的方法/构造函数。

 公共类Foo 
{
私人富_foo;
公共美孚()
{
_foo =这一点;
}
公共无效InitializeFoo()
{
_foo =这一点;
}
}



MSDN 还规定以下为




Me关键字提供方式来指代,其中所述代码是当前正在执行一个
类或结构的具体实例。我
的行为就像任何一个对象变量或一个结构变量
指的是当前实例。




这听起来像曾经类已经执行,您可以访问这个,但只在实例方法,而在 VB.NET 你得到的类执行,因此,你为什么不能把它作为你说的原因时访问。


I'm converting a VB.Net app into C#, and have noticed that in the VB.Net code, there is a private member variable, which is initialised using Me like this:

Private m_ClassA As New MyCollection(Of ClassA)(Me)

When I convert this to C# code like this:

private MyCollection<ClassA> _classA = new MyCollection<ClassA>(this);

I have the error

Argument is value while parameter type is ref.

If I put ref in front of the parameter this, I get the error

cannot use this in member initializer.

I've read here that members are initialized before the base class, and so this cannot be used in members as it may not yet be initialised. My question is why is it legal in VB.Net and not C#?

Is this down to the compiler handling it differently? It seems weird that the two have different behaviours.

To get around it I guess i'll initialize the member in the contructor.

解决方案

According to MSDN.

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor.

This can be read here.

You can't access this anywhere really other than instance/constructors. So you couldn't do something like this either:

public class Foo
{
  private Foo _foo = this;
}

As you say, in C# your going to have to use methods/constructors.

public class Foo
{
  private Foo _foo;
  public Foo()
  {
    _foo = this;
  }
  public void InitializeFoo()
  {
    _foo = this;
  }
}

MSDN also states the following for Me:

The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing. Me behaves like either an object variable or a structure variable referring to the current instance.

It sounds like once the class has executed you get access to this, but only within the instance methods whereas in VB.NET you get access at the time the class is executing, hence the reason why you can't use it as you have stated.

这篇关于为什么在C#这成员的初始值是不允许的,但在VB.Net我被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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