Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Initialization can be simplified :IDE0017

Tags:

c#

.net

With my code I get messages which shows messages as:

Object initialization can be simplified.

They are not shown as errors? How does simplification help? How do I simplify this code

        _passwordEntry = new Entry();
        _passwordEntry.Keyboard = Keyboard.Text;
        _passwordEntry.Placeholder = "Password";
        stackLayout.Children.Add(_passwordEntry);
like image 699
user Avatar asked Sep 01 '19 12:09

user


People also ask

What is object initialisation?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.

How do you initialize an object in C sharp?

In object initializer, you can initialize the value to the fields or properties of a class at the time of creating an object without calling a constructor. In this syntax, you can create an object and then this syntax initializes the freshly created object with its properties, to the variable in the assignment.

What is object initializer in C#?

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.


1 Answers

Place your mouse over the green highlighted code in Visual Studio and press ctrl+; or right click -> quick actions and refactoring.

Visual Studio will certainly offer you the possibility to change your code to this :

stackLayout.Children.Add(new Entry
{
    Keyboard = Keyboard.Text,
    Placeholder = "Password"
});

This is not an error, but an eventual simplification in code

like image 144
Cid Avatar answered Nov 11 '22 14:11

Cid