Last active
January 1, 2016 01:28
-
-
Save controlflow/8072635 to your computer and use it in GitHub Desktop.
Primary ctors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReverseForLookupItem : ForLookupItemBase | |
{ | |
public ReverseForLookupItem([NotNull] PrefixExpressionContext context, | |
[NotNull] LiveTemplatesManager templatesManager, | |
[CanBeNull] string lengthPropertyName) | |
: base("forR", context, templatesManager, lengthPropertyName) { } | |
protected override IForStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression) | |
{ | |
... | |
} | |
} | |
// vs. | |
class ReverseForLookupItem([NotNull] PrefixExpressionContext context, | |
[NotNull] LiveTemplatesManager templatesManager, | |
[CanBeNull] string lengthPropertyName) | |
: ForLookupItemBase("forR", context, templatesManager, lengthPropertyName) | |
{ | |
protected override IForStatement CreateStatement(CSharpElementFactory factory, ICSharpExpression expression) | |
{ | |
... | |
} | |
} |
// и да, тут два readonly поля (которые явно написаны):
class Point(int x, int y) {
public readonly int X = x;
public readonly int Y = y;
}
// тут тоже два поля (readonly backing-поле автосвойства):
class Point(int x, int y) {
public int X { get; } = x;
public int Y { get; } = y;
}
// тут два backing-поля автосвойства (мутабельные):
class Point(int x, int y) {
public int X { get; set; } = x;
public int Y { get; set; } = y;
}
// тут два поля, получившиеся из параметров класса (скорее всего мутабельные):
class Point(int x, int y) {
public int X { get { return x; } }
public int Y { get { return y; } }
}
// абсолютно то же самое, только короче (показывали на NDC):
class Point(int x, int y) {
public int X => x;
public int Y => y;
}
// скорее всего можно будет так (два поля из параметров классов):
class Point(int x, int y) {
public int X {
get { return x; }
set { x = value; }
}
public int Y {
get { return y; }
set { y = value; }
}
}
Кажется, слишком наворочено. Поживём-увидим :о)
Так не работает
class Point(int x, int y) {
public int X { get { return x; } }
public int Y { get { return y; } }
}
(7,31): error CS9007: Parameters of a primary constructor can only be accessed in instance variable initializers and arguments to the base constructor.
(8,31): error CS9007: Parameters of a primary constructor can only be accessed in instance variable initializers and arguments to the base constructor.
Последние 3 примера не работают
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Я не знаю точно, но скорее всего список полей будет определяться как в F# - если параметр класса использован только в вызове базового класса или инициализаторе поля/свойства (если такие будут в C# 6.0), то поле для него создаваться не будет.