Join Bruce Van Horn for an in-depth discussion in this video Alternate if-else formats and the ternary operator, part of C# Essential Training: 2 Flow Control, Arrays, and Exception Handling.
- So we've seen a basic If statement at work.…I thought I'd show you a couple of…different formats for this.…Let's add another method to our flow control class.…This one is going to be very similar…to what we have on line ten.…This is going to be public bool IsYourFavoriteColorRed.…And this time I wanted to show you that…if you have only one thing in your If statement,…as we actually do in the blue version of this,…we don't really need to put the curly braces in there.…
So it looks like this.…We can say if(color.ToLower() == "red")…then we'll return true.…Otherwise, there's no need for an else here,…"return false;".…The reason this works is because…if it is actually red, then the statement returns here,…it exits the method and line 12 never actually runs.…But if it's false then it falls through to line 12,…and line 12 handles the return.…
It's a slightly more terse way of writing…what we have down here in the blue version.…And if you like terse, let me show you…an even more terse method,…and that's the ternery operator.…
AuthorBruce Van Horn
- Writing unit tests in C#
- Working with simple and multidimensional arrays
- Managing ordered and unordered data with lists
- Evaluating conditions with if-else statements
- Using OR, AND, and NOT operators
- Building loops
- Debugging and handling exceptions
- Creating the final build of your C# project
Skill Level Beginner
C# Object-Oriented Programming Tips and Trickswith Jesse Freeman58m 39s Intermediate
1. Unit Testing and Test-Driven Development
2. Arrays and Collections
3. Flow Control
4. Exception Handling
5. Getting to Production
Next steps1m 57s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.