Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between = and => for a variable? [duplicate]

Tags:

c#

What's the difference between these two ways to add something?

private string abc => "def";

And

private string abc = "def";
like image 801
kb_ Avatar asked Mar 01 '16 09:03

kb_


People also ask

What is the difference between i i 1 and i += 1?

The difference is that one modifies the data-structure itself (in-place operation) b += 1 while the other just reassigns the variable a = a + 1 .

Is there a difference between += and =+?

+ is an arithmetic operator while += is an assignment operator.. When += is used, the value on the RHS will be added to the variable on the LHS and the resultant value will be assigned as the new value of the LHS..

What does i += 1 mean?

The solution means to say that there is no difference, ++i has the same meaning as (i += 1) no matter what i happens to be and no matter the context of the expression.

Is it += or =+ in Python?

The Python += operator performs an addition operator and then assigns the result of the operation to a variable. The += operator is an example of a Python assignment operator.


1 Answers

This is the declaration of a classic field as it has always existed in C#:

private string abc = "def";

The field is immediately assigned an initial value.


This is a shorthand syntax for declaring a getter-only property (or expression-bodied property), introduced in C# 6:

private string abc => "def";

It's a short way to write the following:

private string abc
{
    get { return "def"; }
}
like image 73
Marius Schulz Avatar answered Sep 29 '22 22:09

Marius Schulz