Learn
Alternate Expressions
Introduction to Alternate Expressions

Good developers don’t work too hard. We all push ourselves to get better at coding, but when something seems difficult or repetitive, there’s usually a better way to do it!

Methods are a good example:

bool IsEven(int num) { return num % 2 == 0; }

Why bother with these curly braces ({ }) and return keywords for such a short method?

In C# there are other ways to define a method, which can save us effort in typing and make our code easier to read. They’re called expression-bodied definitions and lambda expressions.

From now on, when you see these phrases just think: “These are shortcuts for defining methods!” This lesson will show you how to use them.

Instructions

Check out these basic method definitions in the text editor. The method signature tells us the types of input to and output from the method. The method body lives inside the curly braces { } and sometimes contains the return keyword.

Take Welcome() for example:

  • The signature tells us that the method takes in a string and outputs nothing.
  • The body prints some text to the console.
Folder Icon

Take this course for free

Already have an account?