Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# Polymorphism Virtual Properties

Fredrik Rönnehag
Fredrik Rönnehag
2,342 Points

Difference between => and =

In the code he writes a propery like this, public string Name => "";

Whats the difference between writing a property like that compared to, public string Name = "";

2 Answers

Robert Stefanic
Robert Stefanic
35,170 Points

In C#, properties can have getters and setters.

=> is an expression that can be used to write getter-only properties.

When they use public string name => "";, it's saying that name is a property, which only has a getter, and that will return "" when called.

It'd be the same as writing:

public string name {
    get { return ""; }
}

But public string name => ""; is just a short hand version.

You can also use an expression in the more verbose property definition

public string name {
    get => "";
}

All of these ways of writing the getter are valid.

Robert Stefanic
Robert Stefanic
35,170 Points

"Equal to" and "Bigger than" are mutually exclusive. A number can't be both "bigger than" and "equal" to something.

The Greater than or Equal To operation is >=, which differs from the lambda operator =>.

Using => where you're trying to do a comparison will most likely give you compiler errors.