Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in C# this is not allowed in member initializer, but in VB.Net Me is allowed

Tags:

c#

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.

like image 540
Matt_J Avatar asked Mar 13 '13 09:03

Matt_J


People also ask

Why #is used in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more. It actually adds code to the source file before the final compilation.

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.

Why is %d used in C?

%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value. ( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example.


2 Answers

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.

like image 152
LukeHennerley Avatar answered Oct 25 '22 06:10

LukeHennerley


VB existed before .NET and VB.NET, so some features existed that developers did not want to remove when .NET was introduced. Another such feature is "On Error Resume Next", which also does not exist in C#.

like image 37
Dialecticus Avatar answered Oct 25 '22 04:10

Dialecticus