A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/dotnet/csharplang/discussions/821 below:

[Proposal] infer backing variable from definition · dotnet/csharplang · Discussion #821 · GitHub

[Proposal] infer backing variable from definition #821

Aug 14, 2017 · 6 comments

-

Currently we have to write a backing variable for the property:

private bool _isEditing; public bool IsEditing { get => _isEditing; }

I think the _isEditing variable can be inferred by the compiler as it is being stated in the getter so the resulting code would be like:

public bool IsEditing { get => _isEditing; }

Sometimes the viewmodels have dozens of properties like this so it'll help make it much more readable, specially if we have long variable names.

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

-

Why would this be preferable to auto-implemented properties?

public bool IsEditing { get; private set; }

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

-

I think this feature is useless as we can write or use a tool to complete the property defination,and ofcourse,without the tools,it's also easy to do in VS2017.(With Ctrl+.)

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

-

Because sometimes we have thins like NotifyPropertyChanged inside the setters in the ViewModels:

private bool _isEditing; public bool IsEditing { get => _isEditing; set => RaisePropertyChanged(ref _isEditing, value); }

So you cannot relay on the auto-implemented properties:

public bool IsEditing { get => _isEditing; set => RaisePropertyChanged(ref _isEditing, value); }

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

-

C# is not the kind of language that will infer the existence of a field or local based on usage. Declaration is necessary. There are proposals to allow for either scoped backing fields:

public bool IsEditing {
    // field is scoped within the property only
    bool _isEditing;
    get => _isEditing;
    set => RaisePropertyChanged(ref _isEditing, value);
}

Or proposals to allow for an inferred backing field:

// field is a contextual keyword just like value
public bool IsEditing {
    get => field;
    set => RaisePropertyChanged(ref field, value);
}

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

-

That last option would be just fine in most of scenarios

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

-

Yes, see #140 which I want really bad.

Even further, we should be able to do:

// field is a contextual keyword just like value
public bool IsEditing { get; set => RaisePropertyChanged(ref field, value); }

Beta Was this translation helpful? Give feedback.

You must be logged in to vote

0 replies

Converted from issue

This discussion was converted from issue #821 on September 08, 2020 21:44.

Heading

Bold

Italic

Quote

Code

Link

Numbered list

Unordered list

Task list

Attach files

Mention

Reference

Menu reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4