dotnet / dotnet/csharpstandard

Instance initializers performed in extern instance constructors

Open
#1,253 1 comment 0 reactions 1 assignee Claimed by @jskeet View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

Spec of *instance variable initializers* ([§15.11.3](https://github.com/dotnet/csharpstandard/blob/dcfbe0a482acd93ef41c15dbee431d4a0ad6d6c2/standard/classes.md#15113-instance-variable-initializers)) does not seem to exclude `extern` constructors, i.e., it sounds like `extern` constructors should perform variable initializers as well.

I think the spec should exclude `extern` constructors from the wording - the compiler cannot perform the initializers when an `extern` constructor is used since it doesn't control its code. (Indeed, the roslyn implementation doesn't perform the initializers.)

Note also that if you have a single constructor which is `extern`, then all variable initializers are not emitted anywhere, so they are useless. But there is no warning about that - should one be added to the language? For example:

```cs
class C
{
int X = 5; // the initializer has no effect on the produced assembly

extern C();
}
```

Related issue for static extern constructors: https://github.com/dotnet/csharpstandard/issues/158

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.