<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Carl-Hugo Marcotte</title>
    <description>The latest articles on Forem by Carl-Hugo Marcotte (@carlhugom).</description>
    <link>https://forem.com/carlhugom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F194058%2Fd3720aa5-4fe4-48a8-be14-f17b98219e86.jpg</url>
      <title>Forem: Carl-Hugo Marcotte</title>
      <link>https://forem.com/carlhugom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/carlhugom"/>
    <language>en</language>
    <item>
      <title>Architecting ASP.NET Core Applications—Unveiling the Third Edition</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Mon, 25 Mar 2024 10:59:00 +0000</pubDate>
      <link>https://forem.com/carlhugom/architecting-aspnet-core-applications-unveiling-the-third-edition-4365</link>
      <guid>https://forem.com/carlhugom/architecting-aspnet-core-applications-unveiling-the-third-edition-4365</guid>
      <description>&lt;p&gt;After hundreds of hours of work, a new team, and two new tech reviewers, I'm delighted to announce the release of the third edition of &lt;strong&gt;Architecting ASP.NET Core Applications&lt;/strong&gt;, a unique guide for constructing resilient ASP.NET Core web applications.&lt;/p&gt;

&lt;p&gt;But that was not the title of the first two editions?!? That's correct. After thoughtful consideration, the book has a new title!&lt;br&gt;
Why? All editions were never only about design patterns, which is even more true for the 3rd edition, which expands even more than before into architectural styles and application organization, offering diverse strategies for structuring ASP.NET Core applications.&lt;br&gt;
Of course, I wanted to keep the essence of the first two editions, so here's the subtitle that brings that continuity: &lt;strong&gt;An Atypical Design Patterns Guide for .NET 8, C# 12, and Beyond&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Have you noticed the &lt;em&gt;and Beyond&lt;/em&gt; suffix? Well, that's because the book is good not only for .NET 8 and C# 12, but you'll also be able to leverage its content for future versions, and we wanted to clarify this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/carl-hugo-marcotte_architecting-aspnet-core-applications-activity-7177041549170507776-eCe2" class="ltag_cta ltag_cta--branded"&gt;Read the official LinkedIn Launch Post!&lt;/a&gt;
&lt;a href="https://adpg.link/buy8" class="ltag_cta ltag_cta--branded"&gt;Buy the book on Amazon!&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;Let's start with the updated Table of Content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Section 1: Principles and Methodologies
1. Introduction
2. Automated Testing
3. Architectural Principles
4. REST APIs

Section 2: Designing with ASP.NET Core
5. Minimal API
6. Model-View-Controller
7. Strategy, Abstract Factory, and Singleton Design Patterns
8. Dependency Injection
9. Application Configuration and the Options Pattern
10. Logging patterns

Section 3: Components Patterns
11. Structural Patterns
12. Behavioral Patterns
13. Operation Result Pattern

Section 4: Application Patterns
14. Layering and Clean Architecture
15. Object Mappers
16. Mediator and CQS Patterns
17. Vertical Slice Architecture
18. Request-EndPoint-Response (REPR)
19. Introduction to Microservices Architecture
20. Modular Monolith
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Have you noticed? We removed the UI chapters and replaced them with more REST APIs and backend content!&lt;br&gt;
Yes! This third edition is a testament to our commitment to relevancy and depth, which is now exclusively focused on developers striving for robust REST API and backend design knowledge.&lt;/p&gt;

&lt;p&gt;
  Definition of terms and acronyms
  &lt;p&gt;If there are terms, acronyms, or concepts you are unsure about, I left a list at the end of the article under Definition of terms and acronyms.&lt;/p&gt;

&lt;/p&gt;

&lt;h2&gt;
  
  
  Backend Design Like You've Never Seen Before
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Architecting ASP.NET Core Applications&lt;/strong&gt; is your gateway to mastering REST API and backend designs. It gives you the know-how for building robust and maintainable apps grounded in Gang of Four (GoF) design patterns and well-known architectural principles like SOLID, DRY, and YAGNI. The book focuses on the technical architecture mindset. It is written as a journey where we improve code over time, rework and refactor examples, and more to ensure you understand the logic behind the techniques we are covering. At the end of the book, I want you to understand the choices that we made so you can apply a similar way of thinking to your real-world problems, which are not covered in any books because each challenge is unique in the real world!&lt;/p&gt;

&lt;p&gt;This book is a deep dive into the architectural essence of building enduring ASP.NET Core applications. The third edition is here to quench your thirst for knowledge with an expanded section on Minimal APIs, more automated testing content, more architectural building blocks, more ways to organize your applications, and a closing chapter about building a modular monolith. We explore many application-building techniques, from layering to microservices.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Modular Monolith?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Modular Monolith&lt;/strong&gt;: A modular monolith organizes code into modules within a single application, combining the simplicity of a monolith with modular flexibility, easing maintenance and deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2024/2024-02-Book3-release-modular-monolith-diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2024/2024-02-Book3-release-modular-monolith-diagram.png" alt="Figure 20.2: A Modular Monolith, an aggregator, and three modules, each owning its own database schema"&gt;&lt;/a&gt;&lt;/p&gt;
Figure 20.2: A Modular Monolith, an aggregator, and three modules, each owning its own database schema



&lt;p&gt;You can also read the following articles to learn more about &lt;a href="https://www.forevolve.com/en/articles/2017/06/29/microservices-aggregation/"&gt;Microservices Aggregation&lt;/a&gt; (Modular Monolith).&lt;/p&gt;

&lt;h2&gt;
  
  
  What's new in the third edition?
&lt;/h2&gt;

&lt;p&gt;The sections are reimagined for a smoother learning journey, and the content has been revised to improve the clarity of each chapter. Chapters now prioritize REST API design and patterns, shedding extraneous UI code to concentrate on what truly matters in backend development.&lt;/p&gt;

&lt;p&gt;Chapter 2 has been overhauled to cover testing approaches like black-box, white-box, and grey-box testing. The foundational architectural principles are rearranged, and the chapter is improved to establish the groundwork for modern application design even better than before. Two new chapters now focus on REST APIs and Minimal APIs, while a third chapter about building Web APIs using MVC was updated.&lt;/p&gt;

&lt;p&gt;I improved and increased the number of real-world-like examples where numerous code projects have been updated or rewritten completely. The Dependency Injection chapter benefited from significant updates as well. I split the &lt;em&gt;options and logging&lt;/em&gt; chapter in two, and improved the content.&lt;/p&gt;

&lt;p&gt;Many other changes were applied, like improving the heading of chapters for easier navigation, and all chapters benefitted from content tweaks, diagram updates, code sample revamps, and more. On top of that, I added new content around open-source tools like Mapperly, MassTransit, and Refit.&lt;/p&gt;

&lt;p&gt;Additionally, Chapter 18 is a new chapter dedicated to the Request-EndPoint-Response (REPR) pattern using Minimal APIs. I also listened to your feedback and added some code to the Microservices chapter. The new microservices project extends the REPR code sample to microservices architecture and introduces API layering with a Backend For Frontend (BFF) example. Finally, Chapter 20 is also new, discusses modular monolith architecture, and builds on top of Chapters 18 and 19's new e-commerce examples. That last project, rebuilt in three flavors, is a larger implementation that combines more building blocks like a real app would while keeping it small enough to fit in a book.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Microservices Architecture?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Microservices Architecture&lt;/strong&gt; breaks down a large application into smaller, independent pieces, each performing a specific function. These microservices communicate with each other to form a scalable system, often deployed in the cloud as containerized or serverless applications, enhancing flexibility and scalability.&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2024/2024-02-Book3-release-microservices-diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2024/2024-02-Book3-release-microservices-diagram.png"&gt;&lt;/a&gt;&lt;/p&gt;

Figure 19.25: A diagram that represents the deployment topology and relationship between the different services




&lt;p&gt;You can also read the following articles to learn more about &lt;a href="https://www.forevolve.com/en/articles/2022/05/29/microservices-architecture-exerpt/"&gt;Implementing Microservices Architectures&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your ASP.NET Core Development Companion
&lt;/h2&gt;

&lt;p&gt;Once again, this release is crafted for intermediate ASP.NET Core developers eager to refine their knowledge of design patterns and application development. Software architects keen on revitalizing their theoretical and hands-on expertise will also find this third edition an invaluable ally. With comprehensive coverage of updated architectural patterns, RESTful design, SOLID principles, and a touch of microservices, this edition stands as a pillar of modern backend application design.&lt;/p&gt;

&lt;p&gt;For the best experience, I recommend you read the book cover to cover first to ensure you understand the decisions behind the refactoring and improvements we make throughout. Of course, afterward, you can use it as a reference and browse it however you please.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embrace The Journey of Backend Mastery
&lt;/h2&gt;

&lt;p&gt;Join me on an exceptional learning path that will revolutionize your ASP.NET Core application architecture perspective. The third edition awaits you, promising a transformative encounter with REST API and backend design unlike anything you’ve experienced before.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition of terms and acronyms
&lt;/h2&gt;

&lt;p&gt;Here is a reference to certain terms and acronyms from the article.&lt;br&gt;
Hopefully, these definitions will help you out.&lt;/p&gt;

&lt;p&gt;
  Terms and acronyms
  &lt;p&gt;&lt;strong&gt;REST API&lt;/strong&gt;: REST (Representational State Transfer) API is a design style that uses HTTP requests to access and use data, allowing applications to communicate and exchange data in a standardized format, enhancing interoperability and simplicity in web services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RESTful&lt;/strong&gt;: RESTful refers to web services that adhere to REST principles, enabling seamless and efficient interaction between clients and servers through standardized HTTP operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gang of Four (GoF) design patterns&lt;/strong&gt;: These foundational patterns, identified by four authors, offer solutions to common design challenges, improving code reusability and maintainability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GoF Design Patterns (Strategy, Abstract Factory, Singleton)&lt;/strong&gt;: Strategy enables selecting algorithms at runtime; Abstract Factory offers an interface for creating families of related objects; Singleton ensures a class has only one instance and provides a global point of access to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SOLID, DRY, and YAGNI principles&lt;/strong&gt;: SOLID represents five principles of object-oriented design that increase software maintainability; DRY ("Don't Repeat Yourself") emphasizes avoiding code duplication; YAGNI ("You Aren't Gonna Need It") advises against adding unnecessary functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Minimal API&lt;/strong&gt;: ASP.NET Core Minimal APIs enable fast and efficient REST endpoint creation with minimal code, dependencies, and configuration, focusing on simplicity and performance while streamlining route and action declaration without needing traditional scaffolding or controllers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Model-View-Controller (MVC)&lt;/strong&gt;: MVC is a design pattern that separates an application into three main components—Model, View, and Controller—to isolate business logic, user interface, and user input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dependency Injection&lt;/strong&gt;: This technique allows the creation of dependent objects outside of a class and provides those objects to the class, improving modularity and testability and breaking tight coupling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REPR (Request-EndPoint-Response) Pattern&lt;/strong&gt;: This pattern promotes the simple routing and handling of HTTP requests by directly associating requests with their handling functions and responses, promoting clean and readable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Microservices Architecture&lt;/strong&gt;: This architecture style structures an application as a collection of small, autonomous services, improving modularity and scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modular Monolith&lt;/strong&gt;: A modular monolith organizes code into modules within a single application, combining the simplicity of a monolith with modular flexibility, easing maintenance and deployment.&lt;/p&gt;



&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/posts/carl-hugo-marcotte_architecting-aspnet-core-applications-activity-7177041549170507776-eCe2" class="ltag_cta ltag_cta--branded"&gt;Read the official LinkedIn Launch Post!&lt;/a&gt;
&lt;a href="https://adpg.link/buy8" class="ltag_cta ltag_cta--branded"&gt;Buy the book on Amazon!&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>csharp</category>
      <category>programming</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Implementing Microservices Architectures</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sat, 25 Jun 2022 14:50:55 +0000</pubDate>
      <link>https://forem.com/carlhugom/implementing-microservices-architectures-3h0n</link>
      <guid>https://forem.com/carlhugom/implementing-microservices-architectures-3h0n</guid>
      <description>&lt;p&gt;This article aims to give you an overview of the concepts surrounding microservices and event-driven architecture, which should help you make informed decisions about whether you should go for a microservices architecture or not.&lt;/p&gt;

&lt;p&gt;The following topics will be covered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  What are microservices?&lt;/li&gt;
&lt;li&gt;  An introduction to event-driven architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;This article is an excerpt from my book, &lt;strong&gt;An Atypical ASP.NET Core 6 Design Patterns Guide&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What are microservices?
&lt;/h2&gt;

&lt;p&gt;Besides being a buzzword, microservices represent an application that is divided into multiple smaller applications. Each application, or microservice, interacts with the others to create a scalable system. Usually, microservices are deployed to the cloud as containerized or serverless applications.&lt;/p&gt;

&lt;p&gt;Before getting into too many details, here are a few principles to keep in mind when building microservices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Each microservice should be a cohesive unit of business.&lt;/li&gt;
&lt;li&gt;  Each microservice should own its data.&lt;/li&gt;
&lt;li&gt;  Each microservice should be independent of the others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Furthermore, everything we have studied so far—that is, the other principles of designing software—applies to microservices but on another scale. For example, you don't want tight coupling between microservices (solved by microservices independence), but the coupling is inevitable (as with any code). There are numerous ways to solve this problem, such as the Publish-Subscribe pattern.&lt;/p&gt;

&lt;p&gt;There are no hard rules about how to design microservices, how to divide them, how big they should be, and what to put where. That being said, I'll lay down a few foundations to help you get started and orient your journey into microservices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cohesive unit of business
&lt;/h3&gt;

&lt;p&gt;A microservice should have a single business responsibility. Always design the system with the domain in mind, which should help you divide the application into multiple pieces. If you know &lt;strong&gt;Domain-Driven Design&lt;/strong&gt; (&lt;strong&gt;DDD&lt;/strong&gt;), a microservice will most likely represent a &lt;strong&gt;Bounded Context&lt;/strong&gt;, which in turn is what I call a &lt;em&gt;cohesive unit of business&lt;/em&gt;. Basically, a cohesive unit of business (or bounded context) is a self-contained part of the domain that has limited interactions with other parts of the domain.&lt;/p&gt;

&lt;p&gt;Even if a &lt;strong&gt;microservice&lt;/strong&gt; has &lt;em&gt;micro&lt;/em&gt; in its name, it is more important to group logical operations under it than to aim at a micro-size. Don't get me wrong here; if your unit is tiny, that's even better. However, suppose you split a unit of business into multiple smaller parts instead of keeping it together (breaking cohesion).&lt;/p&gt;

&lt;p&gt;In that case, you are likely to introduce useless chattiness within your system (coupling between microservices). This could lead to performance degradation and to a system that is harder to debug, test, maintain, monitor, and deploy.&lt;/p&gt;

&lt;p&gt;Moreover, it is easier to split a big microservice into smaller pieces than assemble multiple microservices back together.&lt;/p&gt;

&lt;p&gt;Try to apply the Single Responsibility Principle (SRP) to your microservices: a microservice should have only one reason to change unless you have a good reason to do otherwise.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ownership of data
&lt;/h3&gt;

&lt;p&gt;Each microservice is the source of truth of its cohesive unit of business. A microservice should share its data through an API (a web API/HTTP, for example) or another mechanism (integration events, for example). It should own that data and not share it with other microservices directly at the database level.&lt;/p&gt;

&lt;p&gt;For instance, two different microservices should never access the same relational database table. If a second microservice needs some of the same data, it can create its own cache, duplicate the data, or query the owner of that data but not access the database directly; &lt;strong&gt;never&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This data-ownership concept is probably the most critical part of the microservices architecture and leads to microservices independence. Failing at this will most likely lead to a tremendous number of problems. For example, if multiple microservices can read or write data in the same database table, each time something changes in that table, all of them must be updated to reflect the changes. If different teams manage the microservices, that means cross-team coordination. If that happens, each microservice is not independent anymore, which opens the floor to our next topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Microservice independence
&lt;/h3&gt;

&lt;p&gt;At this point, we have microservices that are cohesive units of business and own their data. That defines &lt;strong&gt;independence&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This independence offers the systems the ability to scale while having minimal to no impact on the other microservices. Each microservice can also scale independently, without the need for the whole system to be scaled. Additionally, when the business requirements grow, each part of that domain can evolve independently.&lt;/p&gt;

&lt;p&gt;Furthermore, you could update one microservice without impacting the others or even have a microservice go offline without the whole system stopping.&lt;/p&gt;

&lt;p&gt;Of course, microservices have to interact with one another, but the way they do should define how well your system runs. A little like Vertical Slice architecture, you are not limited to using one set of architectural patterns; you can independently make specific decisions for each microservice. For example, you could choose a different way for how two microservices communicate with each other versus two others. You could even use different programming languages for each microservice.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I recommend sticking to one or a few programming languages for smaller businesses and organizations as you most likely have fewer developers, and each has more to do. Based on my experience, you want to ensure business continuity when people leave and make sure you can replace them and not sink the ship due to some obscure technologies used here and there (or too many technologies).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we've defined the basics, let's jump into the different ways microservices can communicate using event-driven architecture.&lt;/p&gt;

&lt;h2&gt;
  
  
  An introduction to event-driven architecture
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Event-driven architecture&lt;/strong&gt; (&lt;strong&gt;EDA&lt;/strong&gt;) is a paradigm that revolves around consuming streams of events, or data in motion, instead of consuming static states.&lt;/p&gt;

&lt;p&gt;What I define by a static state is the data stored in a relational database table or other types of data stores, like a NoSQL documents store. That data is dormant in a central location and waiting for actors to consume and mutate it. It is stale between every mutation and the data (a record, for example) represents a finite state.&lt;/p&gt;

&lt;p&gt;On the other hand, data in motion is the opposite: you consume the ordered events and determine the change in state that each event brings.&lt;/p&gt;

&lt;p&gt;What is an event? People often interchange the words event, message, and command. Let’s try to clarify this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  A message is a piece of data that represents something.&lt;/li&gt;
&lt;li&gt;  A message can be an object, a JSON string, bytes, or anything else your system can interpret.&lt;/li&gt;
&lt;li&gt;  An event is a message that represents something that happened in the past.&lt;/li&gt;
&lt;li&gt;  A command is a message sent to tell one or more recipients to do something.&lt;/li&gt;
&lt;li&gt;  A command is sent (past tense), so we can also consider it an event.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A message usually has a payload (or body), headers (metadata), and a way to identify it (this can be through the body or headers).&lt;/p&gt;

&lt;p&gt;We can use events to divide a complex system into smaller pieces or have multiple systems talk to each other without creating tight couplings. Those systems could be subsystems or external applications, such as microservices.&lt;/p&gt;

&lt;p&gt;Like &lt;strong&gt;Data Transfer Objects&lt;/strong&gt; (&lt;strong&gt;DTO&lt;/strong&gt;) of web APIs, events become the data contracts that tie the multiple systems together (coupling). It is essential to think about that carefully when designing events. Of course, we cannot foresee the future, so we can only do so much to get it perfect the first time. There are ways to version events, but this is out of the scope of this article.&lt;/p&gt;

&lt;p&gt;EDA is a fantastic way of breaking tight coupling between microservices but requires rewiring your brain to learn this newer paradigm. Tooling is less mature, and expertise is scarcer than more linear ways of thinking (like using point-to-point communication and relational databases), but this is slowly changing and well worth learning (in my opinion).&lt;/p&gt;

&lt;p&gt;We can categorize events into the following overlapping buckets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Domain events&lt;/li&gt;
&lt;li&gt;  Integration events&lt;/li&gt;
&lt;li&gt;  Application events&lt;/li&gt;
&lt;li&gt;  Enterprise events&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we’ll explore next, all types of events play a similar role with different intents and scopes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain events
&lt;/h3&gt;

&lt;p&gt;A domain event is a term based on DDD representing an event in the domain. This event could then trigger other pieces of logic to be executed subsequently. It allows a complex process to be divided into multiple smaller processes. Domain events work well with domain-centric designs, like Clean Architecture, as we can use them to split complex domain objects into multiple smaller pieces. Domain events are usually application events. We can use MediatR to publish domain events inside an application.&lt;/p&gt;

&lt;p&gt;To summarize, &lt;strong&gt;domain events integrate pieces of domain logic together while keeping the domain logic segregated&lt;/strong&gt;, leading to loosely coupled components that hold one domain responsibility each (single responsibility principle).&lt;/p&gt;

&lt;h3&gt;
  
  
  Integration events
&lt;/h3&gt;

&lt;p&gt;Integration events are like domain events but are used to propagate messages to external systems, to integrate multiple systems together while keeping them independent. For example, a microservice could send the &lt;code&gt;new user registered&lt;/code&gt; event message that other microservices react to, like saving the &lt;code&gt;user id&lt;/code&gt; to enable additional capabilities or sending a greeting email to that new user.&lt;/p&gt;

&lt;p&gt;We use a message broker or message queue to publish such events. We’ll cover those next, after covering application and enterprise events.&lt;/p&gt;

&lt;p&gt;To summarize, &lt;strong&gt;integration events integrate multiple systems together while keeping them independent&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Application events
&lt;/h3&gt;

&lt;p&gt;An application event is an event that is internal to an application; it is just a matter of scope. If the event is internal to a single process, that event is also a domain event (most likely). If the event crosses microservices boundaries that your team owns (the same application), it is also an integration event. The event itself won't be different; it is the reason why it exists and its scope that describes it as an application event or not.&lt;/p&gt;

&lt;p&gt;To summarize, &lt;strong&gt;application events are internal to an application&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enterprise events
&lt;/h3&gt;

&lt;p&gt;An enterprise event describes an event that crosses internal enterprise boundaries. These are tightly coupled with your organizational structure. For example, a microservice sends an event that other teams, part of other divisions or departments, consume.&lt;/p&gt;

&lt;p&gt;The governance model around those events should be different from application events that only your team consumes. Someone must think about who can consume that data, under what circumstances, the impact of changing the event schema (data contract), schema ownership, naming conventions, data-structure conventions, and more, or risk building an unstable data highway.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I like to see EDA as a central &lt;strong&gt;data highway&lt;/strong&gt; in the middle of applications, systems, integrations, and organizational boundaries, where the events (data) flow between systems in a loosely coupled manner.&lt;/p&gt;

&lt;p&gt;It’s like a highway where cars flow between cities (without traffic jams). The cities are not controlling what car goes where but are open to visitors.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To summarize, &lt;strong&gt;enterprise events are integration events that cross organizational boundaries&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We defined events, messages, and commands in this quick overview of event-driven architecture. An event is a snapshot of the past, a message is data, and a command is an event that suggests other systems to take action. Since all messages are from the past, calling them events is accurate. We then organized events into a few overlapping buckets to help identify the intents. We can send events for different objectives, but whether it is about designing independent components or reaching out to different parts of the business, an event remains a payload that respects a certain format (schema). That schema is the data contract (coupling) between the consumers of those events. That data contract is probably the most important piece of it all; break the contract, break the system.&lt;/p&gt;

&lt;p&gt;Now, let's see how event-driven architecture can help us follow the &lt;strong&gt;SOLID&lt;/strong&gt; principles at cloud-scale:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;S&lt;/strong&gt;: Systems are independent of each other by raising and responding to events. The events themselves are the glue that ties those systems together. Each piece has a single responsibility.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;O&lt;/strong&gt;: We can modify the system’s behaviors by adding new consumers to a particular event without impacting the other applications. We can also raise new events to start building a new process without affecting existing applications.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;L&lt;/strong&gt;: N/A.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;I&lt;/strong&gt;: Instead of building a single process, EDA allows us to create multiple smaller systems that integrate through data contracts (events) where those contracts become the messaging interfaces of the system.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;D&lt;/strong&gt;: EDA enables systems to break tight coupling by depending on the events (interfaces/abstractions) instead of communicating directly with one another, inverting the dependency flow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;EDA does not only come with advantages; it also has a few drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Microservices come at a cost and building a monolith is still a good idea for many projects.&lt;/li&gt;
&lt;li&gt;  It is easier to add new features to a monolith than it can be to add them to a microservice application.&lt;/li&gt;
&lt;li&gt;  Most of the time, mistakes cost less in a monolith than in a microservices application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will explore any intricacies of microservices in more detail in &lt;strong&gt;An Atypical ASP.NET Core 6 Design Patterns Guide&lt;/strong&gt;, along with analysis of each of the following patterns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Mediating communication between microservices using message queues and the Publish-Subscribe pattern.&lt;/li&gt;
&lt;li&gt;  Shielding and hiding the complexity of the microservices cluster using Gateway patterns.&lt;/li&gt;
&lt;li&gt;  Using one model to read the data and one model to mutate the data with CQRS pattern.&lt;/li&gt;
&lt;li&gt;  Adding missing features, adapting one system to another, or migrating an existing application to an event-driven architecture model, to name a few possibilities, with the Microservices Adapter pattern.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The microservices architecture is different to building monoliths. Instead of one big application, we split it into multiple smaller ones that we call microservices. Microservices must be independent of one another; otherwise, we will face the same problems associated with tightly coupled classes, but at the cloud scale.&lt;/p&gt;

&lt;p&gt;Microservices are great when you need scaling, want to go serverless, or split responsibilities between multiple teams, but keep the operational costs in mind. Starting with a monolith and migrating it to microservices when scaling is another solution. You can also plan your future migration toward microservices, which leads to the best of both worlds while keeping operational complexity low.&lt;/p&gt;

&lt;p&gt;I don't want you to discard the microservices architecture, but I just want to make sure that you weigh up the pros and cons of such a system before blindly jumping in. Your team's skill level and ability to learn new technologies may also impact the cost of jumping into the microservices boat.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>csharp</category>
      <category>eventdriven</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Boolean algebra laws with C#</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 29 Aug 2021 14:31:22 +0000</pubDate>
      <link>https://forem.com/carlhugom/boolean-algebra-laws-with-c-2hgb</link>
      <guid>https://forem.com/carlhugom/boolean-algebra-laws-with-c-2hgb</guid>
      <description>&lt;p&gt;This article explores multiple Boolean algebra laws in a programmer-oriented way, leaving the mathematic notation aside. Those laws can be beneficial when working with boolean logic to simplify complex conditions. This article is very light in explanation and exposes the laws using C#. Don't worry, I'm not recycling myself as a math teacher.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a sub-series, starting with &lt;a href="https://www.forevolve.com/en/articles/2021/05/09/learn-coding-with-dot-net-core-part-9/"&gt;Introduction to Boolean algebra and logical operators&lt;/a&gt;.&lt;br&gt;
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.&lt;br&gt;
If you are already reading the whole series in order, please discard this word of advice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's explore Boolean algebra laws in alphabetical order.&lt;br&gt;
Some are very simple, while some may seem more complex, but all are very useful tools.&lt;/p&gt;

&lt;p&gt;In the examples, the variables (&lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;) are all booleans, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Absorption Law
&lt;/h2&gt;

&lt;p&gt;The absorption law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; (A || B)&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || A &amp;amp;&amp;amp; B&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Annulment Law
&lt;/h2&gt;

&lt;p&gt;The annulment law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; false&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || true&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Associative Law
&lt;/h2&gt;

&lt;p&gt;The associative law says that no matter the order or priority of the OR comparisons, the result will always be the same.&lt;br&gt;
The following conditions yield the same result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A || (B || C)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;(A || B) || C&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;(A || C) || B&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || B || C&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Complement Law
&lt;/h2&gt;

&lt;p&gt;The complement law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; !A&lt;/code&gt; is always &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || !A&lt;/code&gt; is always &lt;code&gt;true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Commutative Law
&lt;/h2&gt;

&lt;p&gt;The commutative law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; B&lt;/code&gt; is the same as &lt;code&gt;B &amp;amp;&amp;amp; A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || B&lt;/code&gt; is the same as &lt;code&gt;B || A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Consensus Law
&lt;/h2&gt;

&lt;p&gt;The consensus law goes as follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;(A || B) &amp;amp;&amp;amp; (!A || C) &amp;amp;&amp;amp; (B || C)&lt;/code&gt; is equivalent to &lt;code&gt;(A || B) &amp;amp;&amp;amp; (!A || C)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; &lt;code&gt;A &amp;amp;&amp;amp; B || A &amp;amp;&amp;amp; C || B &amp;amp;&amp;amp; C&lt;/code&gt; is equivalent to &lt;code&gt;A &amp;amp;&amp;amp; B || A &amp;amp;&amp;amp; C&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These two might be harder to grasp, so here's my take at a quick explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the first two comparisons of &lt;code&gt;(A || B) &amp;amp;&amp;amp; (!A || C) &amp;amp;&amp;amp; (B || C)&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;!A&lt;/code&gt; are almost cancelling themselves, leaving the outcome of the comparison to &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;. Based on that fact, no matter the values, the last comparison &lt;code&gt;&amp;amp;&amp;amp; (B || C)&lt;/code&gt; becomes useless (already evaluated).&lt;/li&gt;
&lt;li&gt;The second one is similar. In the first two comparisons of &lt;code&gt;A &amp;amp;&amp;amp; B || A &amp;amp;&amp;amp; C || B &amp;amp;&amp;amp; C&lt;/code&gt;, we test &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;, making the last comparison void &lt;code&gt;|| B &amp;amp;&amp;amp; C&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Word of advice:&lt;/strong&gt; it is not mandatory to remember the consensus law, so feel free to skip this one if you think it's too complicated for today. You can always come back to it later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  De Morgan's laws
&lt;/h2&gt;

&lt;p&gt;This one is very interesting and can be handy from time to time.&lt;br&gt;
In plain English, De Morgan's laws are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The &lt;code&gt;negation&lt;/code&gt; of a &lt;code&gt;disjunction&lt;/code&gt; is the &lt;code&gt;conjunction&lt;/code&gt; of the &lt;code&gt;negations&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;negation&lt;/code&gt; of a &lt;code&gt;conjunction&lt;/code&gt; is the &lt;code&gt;disjunction&lt;/code&gt; of the &lt;code&gt;negations&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Logical &lt;code&gt;conjunction&lt;/code&gt; means AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;), and the logical &lt;code&gt;disjunction&lt;/code&gt; means OR (&lt;code&gt;||&lt;/code&gt;).&lt;br&gt;
Unless you are into mathematics, you don't have to remember that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now to the part that interests us, in C#, De Morgan's laws are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;!(A || B)&lt;/code&gt; is equivalent to &lt;code&gt;!A &amp;amp;&amp;amp; !B&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;!(A &amp;amp;&amp;amp; B)&lt;/code&gt; is equivalent to &lt;code&gt;!A || !B&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Distributive Law
&lt;/h2&gt;

&lt;p&gt;The distributive law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; B || A &amp;amp;&amp;amp; C&lt;/code&gt; is equivalent to &lt;code&gt;A &amp;amp;&amp;amp; (B || C)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;(A || B) &amp;amp;&amp;amp; (A || C)&lt;/code&gt; is equivalent to &lt;code&gt;A || B &amp;amp;&amp;amp; C&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Double negation law
&lt;/h2&gt;

&lt;p&gt;The double negation law says that two negations negate themselves.&lt;br&gt;
In C# this looks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;!!A&lt;/code&gt; is equivalent to &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Identity Law
&lt;/h2&gt;

&lt;p&gt;The identity law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; true&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || false&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Idempotent Law
&lt;/h2&gt;

&lt;p&gt;The idempotent law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; A&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A || A&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Redundancy Law
&lt;/h2&gt;

&lt;p&gt;The redundancy law goes as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;(A || B) &amp;amp;&amp;amp; (A || !B)&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; B || A &amp;amp;&amp;amp; !B&lt;/code&gt; always equals &lt;code&gt;A&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;(A || !B) &amp;amp;&amp;amp; B&lt;/code&gt; is equivalent to &lt;code&gt;A &amp;amp;&amp;amp; B&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;A &amp;amp;&amp;amp; !B || B&lt;/code&gt; is equivalent to &lt;code&gt;A || B&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored a bunch of Boolean algebra laws from a C# programmer perspective. You can find information on them, including mathematic proofs, online or in books if you are into maths. Personally, I prefer the plain C# version, so that's why I translated them to this here. Even if some of those laws might seem a bit too complicated to remember, don't be discouraged; even the simplest ones are helpful: start there.&lt;/p&gt;

&lt;p&gt;Programming is like playing LEGO&lt;sup&gt;®&lt;/sup&gt; blocks: we can combine all of those laws, which are logic patterns. For example, &lt;code&gt;!(!A || !B)&lt;/code&gt; looks complicated, but after applying De Morgan's law, it becomes equivalent to &lt;code&gt;!(!(A &amp;amp;&amp;amp; B))&lt;/code&gt;. By removing the useless parenthesis we end up having &lt;code&gt;!!(A &amp;amp;&amp;amp; B)&lt;/code&gt;, which exposes a double negation. Applying the double negation law leads to &lt;code&gt;A &amp;amp;&amp;amp; B&lt;/code&gt;. That simplified version of the original condition looks way simpler, doesn't it?&lt;/p&gt;

&lt;p&gt;Learning the basics is helpful in the long run. If you have a good memory, feel free to memorize all of this as a starting point. If you don't, don't worry, learn them one by one. The idea is to simplify your code, so it reaches a more maintainable state. With a bit of time, you will most likely know and apply many (if not all) of them without thinking about it. Just start with the simplest ones, bookmark the article, and learn the others later. Understanding rules, laws, and concepts should get you further than just remembering them; but that takes more time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun fact: I've run many interviews in 2021, and one of my favorite technical questions is based on complicated conditions that can be simplified using some of those laws. And no, I'm not looking for candidates that know the name of the laws, just if they can resolve a complex if-statement and how.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please leave your questions or comments below or drop me a Tweet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/"&gt;my blog&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using the switch selection statement to simplify conditional statements blocks</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 25 Jul 2021 15:54:34 +0000</pubDate>
      <link>https://forem.com/carlhugom/using-the-switch-selection-statement-to-simplify-conditional-statements-blocks-2lll</link>
      <guid>https://forem.com/carlhugom/using-the-switch-selection-statement-to-simplify-conditional-statements-blocks-2lll</guid>
      <description>&lt;p&gt;This article explores how to simplify certain complex conditional blocks by introducing the &lt;code&gt;switch&lt;/code&gt; statement.&lt;br&gt;
The &lt;code&gt;switch&lt;/code&gt; keyword is very standard in programming languages.&lt;br&gt;
We use it to compare a variable with many values.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Please note that we are not covering switch expressions in this article.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a sub-series, starting with &lt;a href="https://www.forevolve.com/en/articles/2021/05/09/learn-coding-with-dot-net-core-part-9/"&gt;Introduction to Boolean algebra and logical operators&lt;/a&gt;.&lt;br&gt;
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.&lt;br&gt;
If you are already reading the whole series in order, please discard this word of advice.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The Switch
&lt;/h2&gt;

&lt;p&gt;In the previous article, &lt;a href="https://www.forevolve.com/en/articles/2021/06/13/learn-coding-with-dot-net-core-part-10/"&gt;Using if-else selection statements to write conditional code blocks&lt;/a&gt;, we covered the basics behind contextual code.&lt;br&gt;
This article explores the &lt;code&gt;switch&lt;/code&gt; statement by converting a complex &lt;code&gt;if&lt;/code&gt; block to a &lt;code&gt;switch&lt;/code&gt;.&lt;br&gt;
We go through the syntax afterward.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial code (if):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"world"&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"goodbye"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Au revoir!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Goodbye in French&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"name?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your name?"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Your name is &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid input"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Converted code (switch):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"world"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"goodbye"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Au revoir!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Goodbye in French&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"name?"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your name?"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Your name is &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid input"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's now analyze the previous code, starting with the new keywords:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;switch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;case&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;break&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;default&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;switch&lt;/code&gt; keyword starts a code block that must be followed by a variable in parenthesis, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Code block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;case&lt;/code&gt; keyword, followed by a &lt;em&gt;value&lt;/em&gt; and &lt;code&gt;:&lt;/code&gt;, is an equality comparison against the original &lt;code&gt;variable&lt;/code&gt; passed to the &lt;code&gt;switch&lt;/code&gt;.&lt;br&gt;
You can see &lt;code&gt;case "goodbye":&lt;/code&gt; as the equivalent of &lt;code&gt;if (variable == "goodbye")&lt;/code&gt;.&lt;br&gt;
A &lt;code&gt;case&lt;/code&gt; block can be empty or end with a &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt;.&lt;br&gt;
We will not cover the &lt;code&gt;return&lt;/code&gt; keyword in this article because it is related to other concepts that we have not explored yet.&lt;br&gt;
When the &lt;code&gt;case&lt;/code&gt; is empty, it continues to the next &lt;code&gt;case&lt;/code&gt;.&lt;br&gt;
For example, &lt;code&gt;case "hello":&lt;/code&gt; falls back to &lt;code&gt;case "world":&lt;/code&gt; that falls back to &lt;code&gt;case "hello world":&lt;/code&gt; that gets executed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Interesting fact:&lt;/strong&gt; C# does not support falling from one non-empty &lt;code&gt;case&lt;/code&gt; to another as some other languages do.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;break&lt;/code&gt; keyword is a &lt;em&gt;jump statement&lt;/em&gt; that allows controlling the flow of the program by exiting the current block, a.k.a. jumping out of the &lt;code&gt;switch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;Finally, in a &lt;code&gt;switch&lt;/code&gt; block, the &lt;code&gt;default&lt;/code&gt; keyword is the equivalent of the &lt;code&gt;else&lt;/code&gt;; it is hit when no other &lt;code&gt;case&lt;/code&gt; was hit.&lt;/p&gt;

&lt;p&gt;Before the exercise, let's peek at the program flow created by a switch statement.&lt;/p&gt;
&lt;h2&gt;
  
  
  Flow of the program
&lt;/h2&gt;

&lt;p&gt;In a nutshell, a &lt;code&gt;switch&lt;/code&gt; statement allows comparing if a variable is equal to a value from a list of cases.&lt;br&gt;
Here is a visual representation of the program flow created by a &lt;code&gt;switch&lt;/code&gt; block:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/switch-flow.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/switch-flow.png" alt="switch program's flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, it's your turn to try it out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;Convert the following code to use a &lt;code&gt;switch&lt;/code&gt; statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"E"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid input"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you are done, you can compare with &lt;strong&gt;My Solution&lt;/strong&gt; below.&lt;/p&gt;

&lt;p&gt;
  My Solution
  &lt;br&gt;
&lt;strong&gt;Program.cs&lt;/strong&gt;&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"D"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"4"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"E"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid input"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;/p&gt;

&lt;p&gt;Good job! You completed another small chapter of your programming journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This article explored the &lt;code&gt;switch&lt;/code&gt; statement, which allows comparing if a variable is equal to a value from a list of cases.&lt;br&gt;
The &lt;code&gt;switch&lt;/code&gt; statement is another way to create conditional code and control our programs' flow.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;switch&lt;/code&gt; statement is handy for variables with a finite number of values like &lt;code&gt;enum&lt;/code&gt;s.&lt;br&gt;
More recent versions of C# also introduce switch expressions and pattern matching that open many other possibilities. However, many of those possibilities require knowledge of object-oriented programming that we are not exploring in this article series.&lt;/p&gt;

&lt;p&gt;Please leave your questions or comments below or drop me a Tweet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Common Boolean algebra laws&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/"&gt;my blog&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using if-else selection statements to write conditional code blocks</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sat, 19 Jun 2021 16:11:54 +0000</pubDate>
      <link>https://forem.com/carlhugom/using-if-else-selection-statements-to-write-conditional-code-blocks-18l2</link>
      <guid>https://forem.com/carlhugom/using-if-else-selection-statements-to-write-conditional-code-blocks-18l2</guid>
      <description>&lt;p&gt;In this article, we are exploring conditional execution flows. What is a code path? What is a conditional? What's an &lt;code&gt;if&lt;/code&gt; statement? These are the subject that we cover here.&lt;br&gt;
As part of the beginner journey, we focus on the if-else selection statements LEGO® block, laying down the foundation for more advanced use-cases.&lt;/p&gt;

&lt;p&gt;In this article, we are exploring conditional execution flows. What is a code path? How will we do that? These are the subject that we cover here.&lt;br&gt;
As part of the beginner journey, we focus on the &lt;em&gt;if-else selection statements&lt;/em&gt; LEGO® block, laying down the foundation for more advanced use-cases.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a sub-series, starting with &lt;a href="https://www.forevolve.com/en/articles/2021/05/09/learn-coding-with-dot-net-core-part-9/"&gt;Introduction to Boolean algebra and logical operators&lt;/a&gt;.&lt;br&gt;
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.&lt;br&gt;
If you are already reading the whole series in order, please discard this word of advice.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Conditional execution flow
&lt;/h2&gt;

&lt;p&gt;In the article &lt;a href="https://dev.to/carlhugom/how-to-read-user-inputs-from-a-console-5fce"&gt;How to read user inputs from a console&lt;/a&gt;, we talked about the flow of a program, moving from the first instruction to the next.&lt;br&gt;
Let's call that &lt;em&gt;linear execution flow&lt;/em&gt;.&lt;br&gt;
Here, you will learn how to run only part of the code based on different values, leading to a more complex execution flow model.&lt;/p&gt;

&lt;p&gt;By writing conditional code blocks, we can create a program that actually has more complex logic than what we programmed so far.&lt;br&gt;
Our program will be able to do different things based on different runtime values, like user inputs.&lt;/p&gt;

&lt;p&gt;Let's now explore how to program that using C#.&lt;/p&gt;
&lt;h2&gt;
  
  
  Equality operators
&lt;/h2&gt;

&lt;p&gt;Before we dig into the conditional blocks, we will visit two new operators to help us compare if two values are equal or not.&lt;br&gt;
Those works with all primitive types like &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;, for example.&lt;/p&gt;
&lt;h3&gt;
  
  
  Equality operator ==
&lt;/h3&gt;

&lt;p&gt;The equality operator &lt;code&gt;==&lt;/code&gt; allows comparing if two values are equal.&lt;br&gt;
The syntax is &lt;code&gt;left_operands == right_operand&lt;/code&gt; and returns a Boolean value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; you can read this as &lt;strong&gt;left_operand is equal to right_operand&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, since A is not equal to B, the result of the &lt;code&gt;left == right&lt;/code&gt; comparison is &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
In the case of &lt;code&gt;"A" == "A"&lt;/code&gt;, the result would have been &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We have one last operator to look into before exploring the if-else selection statements, the inequality operator.&lt;/p&gt;
&lt;h3&gt;
  
  
  Inequality operator !=
&lt;/h3&gt;

&lt;p&gt;The inequality operator &lt;code&gt;!=&lt;/code&gt; is the opposite of the equality operator and allows comparing if two values are different (not equal).&lt;br&gt;
The syntax is &lt;code&gt;left_operands != right_operand&lt;/code&gt; and returns a Boolean value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; you can read this as &lt;strong&gt;left_operand is not equal to right_operand&lt;/strong&gt; or &lt;strong&gt;left_operand is different than right_operand&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, since A is not equal to B, the result of the &lt;code&gt;left != right&lt;/code&gt; comparison is &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; the inequality operator is syntactic sugar, equivalent to &lt;code&gt;!(left == right)&lt;/code&gt;.&lt;br&gt;
This simplifies writing C# code a lot.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Other comparison operators exist, but let's keep our scope narrow here and jump into the main subject instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  if-else selection statements
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;if-else selection statements&lt;/em&gt; are blocks of C# code that are executed conditionally based on a Boolean expression—a condition that evaluates to &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
In this section, we are exploring the following concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Statements block&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;if&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;else&lt;/code&gt; statement&lt;/li&gt;
&lt;li&gt;  The &lt;code&gt;else if&lt;/code&gt; statement&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Statements block
&lt;/h3&gt;

&lt;p&gt;In C#, a statements block is delimited by &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Code before the statement block&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Statement block start delimiter&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="c1"&gt;// Code inside the block&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Statement block end delimiter&lt;/span&gt;
&lt;span class="c1"&gt;// Code after the statement block&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A block creates a sub-context that can access its parent context.&lt;br&gt;
However, the parent context can't access that sub-context.&lt;br&gt;
Here is an example that illustrates this concept:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Inside block; a = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Inside block; b = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Outside block; a = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Outside block; b = &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Error: The name 'b' does not exist in the current context&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, the program can access the &lt;code&gt;a&lt;/code&gt; variable from inside the statements block but cannot access the &lt;code&gt;b&lt;/code&gt; variable from outside of it.&lt;br&gt;
Because of that, if we execute the code, .NET will report an error telling us &lt;code&gt;The name 'b' does not exist in the current context&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We use &lt;em&gt;statements blocks&lt;/em&gt; extensively throughout the article, so don't worry about it if you are unsure why you would write one.&lt;br&gt;
Let's explore why I indented the code inside the block before writing our first conditional code block using the &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;
&lt;h4&gt;
  
  
  Indentation
&lt;/h4&gt;

&lt;p&gt;Have you noticed the indentation added inside the statements block?&lt;br&gt;
By convention, we add that indentation for readability.&lt;br&gt;
That makes the different contexts (or nested-levels) line up vertically, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/indentation-of-a-statements-block.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/indentation-of-a-statements-block.png" alt="indentation of a statements block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In C#, we usually use 4 spaces to indent the code.&lt;br&gt;
People may also use 2 spaces (not frequent in the .NET/C# world).&lt;br&gt;
Some people also prefer to use tabs instead of spaces.&lt;br&gt;
By default, Visual Studio and Visual Studio Code will translate a tab to &lt;em&gt;N&lt;/em&gt; spaces automatically (default: 4), so you don't have to type 4 spaces every time. One tab will do.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Fun" fact:&lt;/strong&gt; A tabs versus spaces war also exists where people prefer tabs over spaces or vice versa and argue that their way is the best over the other.&lt;br&gt;
I personally use spaces, tried tabs, tried many techniques during the years, and realized that it does not matter much in the end.&lt;br&gt;
If you are working in a team or an enterprise, there may well be existing guidelines around this.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we explore the &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;if&lt;/code&gt; statement
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;if&lt;/code&gt; statement does literally what its English definition is: &lt;strong&gt;if the &lt;em&gt;condition&lt;/em&gt; is true, then enter the statements block; otherwise, don't&lt;/strong&gt;.&lt;br&gt;
This is where we begin to put that Boolean algebra to good use.&lt;br&gt;
The syntax goes like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something when condition is true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, &lt;code&gt;input == "GO"&lt;/code&gt; represents the condition that evaluates to a boolean result.&lt;br&gt;
When running the program, if the user enters &lt;code&gt;GO&lt;/code&gt; (uppercase), the program will print &lt;code&gt;The user entered GO!&lt;/code&gt;. Otherwise, it will skip that code block.&lt;br&gt;
Here is a running example:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-06-05-if-statement-program-execution.gif" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-06-05-if-statement-program-execution.gif" alt="if statement program execution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see from that recording, if we enter something other than &lt;code&gt;GO&lt;/code&gt;, the program skips the statements block.&lt;br&gt;
Here is the visual representation of this program flow when the user enters &lt;code&gt;GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/GO-condition-true.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/GO-condition-true.png" alt="Visual representation of the program flow when the user enters GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;NOT GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/GO-condition-false.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/GO-condition-false.png" alt="Visual representation of the program flow when the user enters NOT GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; in the preceding diagrams, the parts that are not executed are grayed out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But what happens if we want something different to happen if the input is not &lt;code&gt;GO&lt;/code&gt; while keeping this logic?&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;else&lt;/code&gt; statement
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;else&lt;/code&gt; statement must follow an &lt;code&gt;if&lt;/code&gt; statement block (or an &lt;code&gt;else if&lt;/code&gt; block; see below).&lt;br&gt;
We can't write an &lt;code&gt;else&lt;/code&gt; block alone.&lt;br&gt;
The &lt;code&gt;else&lt;/code&gt; statements block is a &lt;strong&gt;&lt;em&gt;fallback statements block&lt;/em&gt;&lt;/strong&gt; that is executed when the &lt;code&gt;if&lt;/code&gt; condition is evaluated to &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The syntax goes like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something when condition is true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something when condition is false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the following example, we put that to good use and display &lt;code&gt;Console.WriteLine("The user did not enter GO!");&lt;/code&gt; when the input is different than &lt;code&gt;"GO"&lt;/code&gt;.&lt;br&gt;
We could write this with two &lt;code&gt;if&lt;/code&gt; statements or an &lt;code&gt;if&lt;/code&gt; followed by an &lt;code&gt;else&lt;/code&gt; statement.&lt;br&gt;
Let's start by the first option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user did not enter GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the preceding code would do the trick.&lt;br&gt;
However, we can remove that second comparison &lt;code&gt;input != "GO"&lt;/code&gt; by leveraging the &lt;code&gt;else&lt;/code&gt; statement instead.&lt;br&gt;
This will a) remove that comparison (slightly improve performance) and b) make our program more maintainable by removing the duplicated logic.&lt;br&gt;
The alternative looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="c1"&gt;// Only this line changed&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user did not enter GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running any of those two programs results in the following execution flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-06-05-if-else-statement-program-execution.gif" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-06-05-if-else-statement-program-execution.gif" alt="if statement program execution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there we go; we can now use the &lt;code&gt;if&lt;/code&gt; and the &lt;code&gt;if-else&lt;/code&gt; statements to control the program's execution flow.&lt;br&gt;
With them, we can execute only certain statement blocks based on runtime values, like values entered by the user.&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/GO-condition-true-if-else.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/GO-condition-true-if-else.png" alt="Visual representation of the program flow when the user enters GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;NOT GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/GO-condition-false-if-else.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/GO-condition-false-if-else.png" alt="Visual representation of the program flow when the user enters NOT GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; in the preceding diagrams, the parts that are not executed are grayed out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ok, but what happens when we want to write a different message if the user enters &lt;code&gt;SHOW ME&lt;/code&gt;?&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;else if&lt;/code&gt; statement
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;else if&lt;/code&gt; statement is a follow-up &lt;code&gt;if&lt;/code&gt; statement if you wish.&lt;br&gt;
As we saw in the preceding section, we can have two &lt;code&gt;if&lt;/code&gt; statements back to back, but they are independent of each other.&lt;br&gt;
On the other hand, the &lt;code&gt;else if&lt;/code&gt; statement allows to add another conditional block after the &lt;code&gt;if&lt;/code&gt;, but the condition is only be evaluated when the previous condition was evaluated to &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
We can chain as many &lt;code&gt;else if&lt;/code&gt; statements as we need.&lt;br&gt;
An &lt;code&gt;else&lt;/code&gt; statement can optionally go last; after all &lt;code&gt;else if&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;Here are a few examples of the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if + else if&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if + else if + else if&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if + else if + else&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if + else if + else if + else&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned, we can add as many &lt;code&gt;else if&lt;/code&gt; blocks as needed.&lt;br&gt;
Let's now apply that to our problem: we want to write a different message if the user enters &lt;code&gt;SHOW ME&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could do this only with &lt;code&gt;if&lt;/code&gt; statements, but as you can see below, it can get complicated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;--&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user did not enter GO nor SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By looking at the preceding code, we can see that the more logic we add, the more complex the &lt;code&gt;else&lt;/code&gt;-like block becomes (the &lt;code&gt;if&lt;/code&gt; line marked by a &lt;code&gt;&amp;lt;--&lt;/code&gt; comment).&lt;br&gt;
Here we have two conditions (&lt;code&gt;input == "GO"&lt;/code&gt; and &lt;code&gt;input == "SHOW ME"&lt;/code&gt;) so we must make sure that both are &lt;code&gt;false&lt;/code&gt; before executing the default block (&lt;code&gt;input != "GO" &amp;amp;&amp;amp; input != "SHOW ME"&lt;/code&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tips:&lt;/strong&gt; I strongly advise against writing that type of code as it can get out of hand very quickly.&lt;br&gt;
In this case, seeing it firsthand will allow you to identify such code.&lt;br&gt;
Always aim at simplicity and readability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's simplify that code using &lt;code&gt;if—else if—else&lt;/code&gt; blocks instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user did not enter GO nor SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can notice two sets of changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Replacing the second &lt;code&gt;if&lt;/code&gt; by an &lt;code&gt;else if&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Replacing the last &lt;code&gt;if&lt;/code&gt; by an &lt;code&gt;else&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a &lt;em&gt;diff&lt;/em&gt; of those two listings, where the &lt;code&gt;-&lt;/code&gt; lines are replaced by the &lt;code&gt;+&lt;/code&gt; lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enter something: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered GO!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user entered SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"GO"&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"SHOW ME"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The user did not enter GO nor SHOW ME!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"End of the program."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the preceding code block highlights, using &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else if&lt;/code&gt;, then &lt;code&gt;else&lt;/code&gt; allowed us to get rid of the complex condition that negates the other two conditions.&lt;br&gt;
This code is still simple, but if you think about adding more and more conditions, the last &lt;code&gt;if&lt;/code&gt; would become very hard to maintain, error-prone, and hard to read.&lt;br&gt;
Moreover, all conditions would be duplicated. Once for its own &lt;code&gt;if&lt;/code&gt; block and negated for that last &lt;code&gt;if&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using an &lt;code&gt;else&lt;/code&gt; statement just makes our life easier, so why not, right?&lt;br&gt;
Anyway, running any of those two programs results in the following execution flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-06-05-else-if-statement-program-execution.gif" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-06-05-else-if-statement-program-execution.gif" alt="else if statement program execution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's explore how the code is evaluated.&lt;br&gt;
Those two small sets of differences change many things in the execution flow, as demonstrated in the following image:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-else-if-diff-order-all-annotations.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-else-if-diff-order-all-annotations.png" alt="else if diff order execution flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the preceding image, we can notice that each condition is evaluated (on the left). In contrast, the conditions are evaluated only when the previous one was false (on the right).&lt;br&gt;
This is the big difference between using &lt;code&gt;if—else if—else&lt;/code&gt; or not.&lt;/p&gt;

&lt;p&gt;To make it easier to understand, let's compare the steps depicting the scenario of a user typing &lt;code&gt;GO&lt;/code&gt; (&lt;code&gt;input == "GO"&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-else-if-diff-order-input-equals-go.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-else-if-diff-order-input-equals-go.png" alt="else if diff order execution flow when input equals GO"&gt;&lt;/a&gt;&lt;/p&gt;




The steps of the left listing goes like this:

1. The first few lines are executed.
2. The first `if` is evaluated to `true`.
3. The program writes a line to the console.
4. The second `if` is evaluated as `false`.
5. The third `if` is evaluated as `false`.
6. The program writes a line to the console.





The steps of the right listing goes like this:

1. The first few lines are executed.
2. The first `if` is evaluated to `true`.
3. The program writes a line to the console.
4. The program writes a line to the console.





&lt;p&gt;In the right execution flow, only the first &lt;code&gt;if&lt;/code&gt; is evaluated.&lt;br&gt;
The program skips the evaluation of the &lt;code&gt;else if&lt;/code&gt; statement and &lt;em&gt;jumps over&lt;/em&gt; both the &lt;code&gt;else if&lt;/code&gt; and &lt;code&gt;else&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/GO-condition-if-elseif-else.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/GO-condition-if-elseif-else.png" alt="Visual representation of the program flow when the user enters GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;SHOW ME&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/SHOW-ME-condition-if-elseif-else.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/SHOW-ME-condition-if-elseif-else.png" alt="Visual representation of the program flow when the user enters SHOW ME"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the visual representation of this program flow when the user enters &lt;code&gt;NOT GO&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/NOT-GO-condition-if-elseif-else.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/NOT-GO-condition-if-elseif-else.png" alt="Visual representation of the program flow when the user enters NOT GO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; in the preceding diagrams, the parts that are not executed are grayed out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you may begin to realise, &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else if&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; blocks are different ways to control the flow of execution of your programs.&lt;br&gt;
Now that we explored that, it is time for you to practice.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;You must write a program that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Asks the user to input his first name.&lt;/li&gt;
&lt;li&gt;  Asks the user to input his last name.&lt;/li&gt;
&lt;li&gt;  If the user enters your first name and last name, write &lt;code&gt;Hey! that's me!&lt;/code&gt; to the console.&lt;/li&gt;
&lt;li&gt;  If the user enters any other name combination, write &lt;code&gt;Hello, FIRST_NAME, LAST_NAME&lt;/code&gt; where &lt;code&gt;FIRST_NAME&lt;/code&gt; and &lt;code&gt;LAST_NAME&lt;/code&gt; are the names the user entered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are a few optional hints in case you feel stuck:&lt;/p&gt;

&lt;p&gt;
  Hint 1
  &lt;br&gt;
If you don't remember how to ask a user for his input, have a look at &lt;a href="https://dev.to/carlhugom/how-to-read-user-inputs-from-a-console-5fce"&gt;How to read user inputs from a console&lt;/a&gt;.&lt;br&gt;


&lt;/p&gt;

&lt;p&gt;
  Hint 2
  &lt;br&gt;
There are multiple ways of implementing this solution.&lt;br&gt;
However, an &lt;code&gt;if&lt;/code&gt; followed by an &lt;code&gt;else&lt;/code&gt; block should be a good start.&lt;br&gt;


&lt;/p&gt;

&lt;p&gt;
  Hint 3
  &lt;br&gt;
The AND operator (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) and the equality operator (&lt;code&gt;==&lt;/code&gt;) will allow you to combine both conditions (first name and last name) into one.&lt;br&gt;
Feel free to have a look at &lt;a href="https://dev.to/carlhugom/introduction-to-boolean-algebra-and-logical-operators-46am"&gt;Introduction to Boolean algebra and logical operators&lt;/a&gt; if you need a reminder on logical operators.&lt;br&gt;


&lt;/p&gt;

&lt;p&gt;Once you are done, you can compare with &lt;strong&gt;My Solution&lt;/strong&gt; below.&lt;/p&gt;

&lt;p&gt;
  My Solution
  &lt;br&gt;
&lt;strong&gt;Program.cs&lt;/strong&gt;&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;MyFirstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Carl-Hugo"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;MyLastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Marcotte"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your first name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your last name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MyFirstName&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MyLastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey! that's me!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Hello, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the preceding code, I:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Leveraged constants to make those values more obvious and easier to change (located at the top of the file). We explored constants in &lt;a href="https://dev.to/carlhugom/introduction-to-c-constants-4gid"&gt;Introduction to C# constants&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;  Used &lt;code&gt;if—else&lt;/code&gt; blocks, introduced in this article.&lt;/li&gt;
&lt;li&gt;  Composed the conditional expression using the equality operator (&lt;code&gt;==&lt;/code&gt;) and the AND logical operator (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;), translating the English requirements to code: « &lt;em&gt;If the user enters your first name and last name [...]&lt;/em&gt; ».&lt;/li&gt;
&lt;li&gt;  Employed string interpolation to format the output of the &lt;code&gt;else&lt;/code&gt; block. We explored string interpolation in &lt;a href="https://dev.to/carlhugom/introduction-to-string-interpolation-g4g"&gt;Introduction to string interpolation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you may start to notice, the more we move forward, the more LEGO® blocks we can piece together, and the more complex the application we can build.&lt;br&gt;
All the pieces are simple. The craft is about assembling them correctly.&lt;br&gt;
The hardest part of programming is probably to teach our brain to &lt;em&gt;think computer&lt;/em&gt;, allowing it to translate human-described requirements to code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; don't think about the code itself; understand the human version of the problem instead, then try to fix it. This should help you.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;p&gt;Here is an alternative way you could have implemented the condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;MyFirstName&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;MyLastName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Hello, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hey! that's me!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, the logic is inverted.&lt;br&gt;
That condition could also have been simplified to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MyFirstName&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;MyLastName&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Hello, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are not sure how I was able to play with those conditions, we will explore that in a future article about common Boolean algebra laws.&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Good job! You completed another small chapter of your programming journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned how to write code that gets executed only when certain conditions are met.&lt;br&gt;
We learnt about the &lt;strong&gt;equality&lt;/strong&gt; (&lt;code&gt;==&lt;/code&gt;) and &lt;strong&gt;inequality&lt;/strong&gt; (&lt;code&gt;!=&lt;/code&gt;) operators.&lt;br&gt;
Then we explored how to write &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;else if&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; statements blocks to alter the linear flow of a program.&lt;br&gt;
We also briefly covered code indentation as a standard way to improve the readability of your code.&lt;/p&gt;

&lt;p&gt;Please leave your questions or comments below or drop me a Tweet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Using the switch selection statement to simplify conditional statements blocks&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/"&gt;my blog&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to Boolean algebra and logical operators</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Thu, 13 May 2021 16:01:18 +0000</pubDate>
      <link>https://forem.com/carlhugom/introduction-to-boolean-algebra-and-logical-operators-46am</link>
      <guid>https://forem.com/carlhugom/introduction-to-boolean-algebra-and-logical-operators-46am</guid>
      <description>&lt;p&gt;In this article, I introduce you to &lt;strong&gt;Boolean algebra&lt;/strong&gt;, a branch of algebra that evaluates the value of a condition to &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. This is a &lt;strong&gt;fundamental part of programming&lt;/strong&gt; that you can't escape, and you will use this until the end of your programmer career and maybe even beyond that point.&lt;/p&gt;

&lt;p&gt;The article is not focusing on mathematical applications and representations but on programming. The objective is to give you the knowledge you need for the next article of the series.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;. If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place. I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
This article is the first part of a sub-series showcasing the following articles:
    
&lt;ul&gt;
  &lt;li&gt;
&lt;a href="https://www.forevolve.com/en/articles/2021/05/09/learn-coding-with-dot-net-core-part-9/"&gt;Introduction to Boolean algebra and logical operators&lt;/a&gt; (you are here)&lt;/li&gt;
  &lt;li&gt;Writing conditional code blocks with if-else selection statements&lt;/li&gt;
  &lt;li&gt;Advanced logical operators and common Boolean algebra laws&lt;/li&gt;
&lt;/ul&gt;

&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Boolean type
&lt;/h2&gt;

&lt;p&gt;In C#, &lt;code&gt;bool&lt;/code&gt; is the type that represents a boolean value. A &lt;code&gt;bool&lt;/code&gt; can have a value of &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. A &lt;code&gt;bool&lt;/code&gt; is the memory representation of a bit. A bit is a base 2 digit that is either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;. The value &lt;code&gt;0&lt;/code&gt; means &lt;code&gt;false&lt;/code&gt;, and the value &lt;code&gt;1&lt;/code&gt; means &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Want to know more?&lt;/strong&gt; The mathematic that we learn at school is base-10; a.k.a., there are 10 numbers: 0 to 9.&lt;br&gt;
On the other hand, computers use base-2, or a binary numeral system, that includes only two numbers: 0 and 1.&lt;br&gt;
Chances are, this is not something that you need to know right away, but I recommend learning this concept one day.&lt;br&gt;
Knowing base-2 (binary), base-8 (octal), and base-16 (hexadecimal) can only help you (yes, there are more than just base-2 and base-10).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following code shows the two possibilities, written in C#:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Using var&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;thisIsTrue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;thisIsFalse&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Using the type name&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;thisIsAlsoTrue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;thisIsAlsoFalse&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we covered how to declare a variable of type &lt;code&gt;bool&lt;/code&gt;, let's look at the basic operations of Boolean algebra.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic operations
&lt;/h2&gt;

&lt;p&gt;There are three basic operations in boolean algebra:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Known-as&lt;/th&gt;
&lt;th&gt;C#&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Conjunction&lt;/td&gt;
&lt;td&gt;AND&lt;/td&gt;
&lt;td&gt;&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Disjunction&lt;/td&gt;
&lt;td&gt;OR&lt;/td&gt;
&lt;td&gt;||&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Negation&lt;/td&gt;
&lt;td&gt;NOT&lt;/td&gt;
&lt;td&gt;&lt;code&gt;!&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With &lt;code&gt;AND&lt;/code&gt;, &lt;code&gt;OR&lt;/code&gt;, and &lt;code&gt;NOT&lt;/code&gt;, we can create most logical conditions that a program requires to run. Let's start by exploring the &lt;code&gt;NOT&lt;/code&gt; logical operator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logical operator NOT
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;NOT&lt;/code&gt; operator is a unary prefix operator and is different from &lt;code&gt;AND&lt;/code&gt; and &lt;code&gt;OR&lt;/code&gt;, which are binary operators. It prefixes a boolean value and inverts it. In C# (and many other languages), the &lt;code&gt;NOT&lt;/code&gt; symbol is &lt;code&gt;!&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; C# 8.0 introduced the &lt;code&gt;!&lt;/code&gt; as a suffix operator, a.k.a. the null-forgiving operator, which is a totally different thing.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following table lists the two possible use of the negation operator and their outcome:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Expression (C#)&lt;/th&gt;
&lt;th&gt;English&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;NOT &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;NOT &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following code uses the preceding grid to explore the possibilities using C#, outputting the values in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;value4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"value1: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"value2: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"value3: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"value4: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When running the program, we obtain the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;value1: True
value2: False
value3: False
value4: True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can observe here, the value of the &lt;code&gt;value3&lt;/code&gt; and &lt;code&gt;value4&lt;/code&gt; variables are the opposite of their negated source. This is the main takeaway here: the NOT operator, in &lt;code&gt;!variable&lt;/code&gt;, flips the original value of &lt;code&gt;variable&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;One last bit&lt;/strong&gt;: as an analogy, you could see a boolean as a light switch and the negation as the action of flipping the switch on/off.&lt;br&gt;
For example, when you flip the light-switch from &lt;em&gt;off&lt;/em&gt; (&lt;code&gt;false&lt;/code&gt;) to &lt;em&gt;on&lt;/em&gt; (&lt;code&gt;true&lt;/code&gt;); &lt;strong&gt;on&lt;/strong&gt; is the equivalent of &lt;strong&gt;not off&lt;/strong&gt; (&lt;code&gt;!false&lt;/code&gt;) while &lt;strong&gt;off&lt;/strong&gt; is the equivalent of &lt;strong&gt;not on&lt;/strong&gt; (&lt;code&gt;!true&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, let's jump into the AND logical operator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional logical operator AND
&lt;/h3&gt;

&lt;p&gt;In C#, the conditional logical &lt;code&gt;AND&lt;/code&gt; operator is represented by &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; It is essential to double the symbol, otherwise &lt;code&gt;&amp;amp;&lt;/code&gt; (single) is a binary operator (acting on bits), and it is different.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator is a binary operator that acts on two operands, like &lt;code&gt;result = operand1 &amp;amp;&amp;amp; operand2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is an example of using the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Result: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding code outputs &lt;code&gt;Result: True&lt;/code&gt; to the console.&lt;br&gt;
Now that you may be wondering why &lt;code&gt;true &amp;amp;&amp;amp; true&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;, let's have a look at the logical table of the &lt;code&gt;AND&lt;/code&gt; operator:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Left&lt;/th&gt;
&lt;th&gt;Right&lt;/th&gt;
&lt;th&gt;C#&lt;/th&gt;
&lt;th&gt;English&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true &amp;amp;&amp;amp; true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; AND &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true &amp;amp;&amp;amp; false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; AND &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false &amp;amp;&amp;amp; true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; AND &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false &amp;amp;&amp;amp; false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; AND &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you may have noticed from the preceding table, all combinations result in &lt;code&gt;false&lt;/code&gt; except when both operands are &lt;code&gt;true&lt;/code&gt;; that's how the AND operator works. Let's update the preceding code to cover the &lt;code&gt;true &amp;amp;&amp;amp; false&lt;/code&gt; scenario:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Result: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updated code outputs &lt;code&gt;Result: False&lt;/code&gt; to the console, precisely like the table predicted.&lt;/p&gt;

&lt;p&gt;Ok, we are not done yet. Next, we look at the &lt;code&gt;OR&lt;/code&gt; operator, which has a similar syntax but a different logical outcome.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional logical operator OR
&lt;/h3&gt;

&lt;p&gt;In C#, the conditional logical &lt;code&gt;OR&lt;/code&gt; operator is represented by &lt;code&gt;||&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; It is essential to double the symbol, otherwise &lt;code&gt;|&lt;/code&gt; (single) is a binary operator (acting on bits), and it is different.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;||&lt;/code&gt; operator, same as the &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; operator, is a binary operator that acts on two operands, like &lt;code&gt;result = operand1 || operand2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a C# example of using the &lt;code&gt;||&lt;/code&gt; operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Result: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding code outputs &lt;code&gt;Result: True&lt;/code&gt; to the console. Like the AND operator, the OR operator also has a logical table that comes with it. Let's have a look:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Left&lt;/th&gt;
&lt;th&gt;Right&lt;/th&gt;
&lt;th&gt;C#&lt;/th&gt;
&lt;th&gt;English&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; || &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; OR &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; || &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; OR &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; || &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; OR &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; || &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; OR &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An interesting observation is how the &lt;code&gt;||&lt;/code&gt; operator returns &lt;code&gt;true&lt;/code&gt; whenever there is at least one operand that is equal to &lt;code&gt;true&lt;/code&gt;. In other words, the &lt;code&gt;||&lt;/code&gt; operator returns &lt;code&gt;false&lt;/code&gt; only when there is no &lt;code&gt;true&lt;/code&gt; (when both operands are &lt;code&gt;false&lt;/code&gt;). In code, the only way to have a result of &lt;code&gt;false&lt;/code&gt; would be the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Result: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding code outputs &lt;code&gt;Result: False&lt;/code&gt; to the console.&lt;/p&gt;

&lt;p&gt;Now that we covered the basic operators, it is time to look at the &lt;em&gt;logical exclusive &lt;code&gt;OR&lt;/code&gt; operator&lt;/em&gt;, which is closer to the spoken &lt;code&gt;OR&lt;/code&gt; than the logical &lt;code&gt;OR&lt;/code&gt; that we just learned about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logical exclusive OR operator (XOR)
&lt;/h2&gt;

&lt;p&gt;In spoken languages, we usually use &lt;code&gt;OR&lt;/code&gt; as an &lt;em&gt;exclusive OR&lt;/em&gt;. For example, when we say, « do you prefer blue or green? » we expect a response about one of the two but not both. That type of OR is called the exclusive &lt;code&gt;OR&lt;/code&gt;, also known as &lt;code&gt;XOR&lt;/code&gt;. In C#, the XOR operator is &lt;code&gt;^&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Advanced information:&lt;/strong&gt; the &lt;code&gt;XOR&lt;/code&gt; operator is a compound operator, or shortcut if you which.&lt;br&gt;
We can compose the equivalent of the &lt;code&gt;XOR&lt;/code&gt; operator using basic operators like &lt;code&gt;NOT&lt;/code&gt;, &lt;code&gt;AND&lt;/code&gt;, and &lt;code&gt;OR&lt;/code&gt;.&lt;br&gt;
In C#, the &lt;code&gt;XOR&lt;/code&gt; operator can be expressed as one of the following expressions: &lt;code&gt;(left || right) &amp;amp;&amp;amp; (!left &amp;amp;&amp;amp; !right)&lt;/code&gt; or &lt;code&gt;(left &amp;amp;&amp;amp; !right) || (!left &amp;amp;&amp;amp; right)&lt;/code&gt;.&lt;br&gt;
In the preceding two code snippets, the parenthesis change the priority of the operations.&lt;br&gt;
The parenthesis play the same concept than in the following elementary mathematic equations &lt;code&gt;(1 + 2) * 3 = 3 * 3 = 9&lt;/code&gt; but &lt;code&gt;1 + 2 * 3 = 1 + 6 = 7&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's start by exploring the &lt;code&gt;XOR&lt;/code&gt; logic table:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Left&lt;/th&gt;
&lt;th&gt;Right&lt;/th&gt;
&lt;th&gt;C#&lt;/th&gt;
&lt;th&gt;English&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true ^ true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; OR &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true ^ false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;true&lt;/code&gt; OR &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false ^ true&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; OR &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false ^ false&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;false&lt;/code&gt; OR &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As you may have noticed, the &lt;code&gt;XOR&lt;/code&gt; logic table is the same as the &lt;code&gt;OR&lt;/code&gt; table, but the result is &lt;code&gt;false&lt;/code&gt; when both operands are &lt;code&gt;true&lt;/code&gt; (first row). This is what differentiates OR and XOR: the result is &lt;code&gt;true&lt;/code&gt; if one operand is &lt;code&gt;true&lt;/code&gt; but not both.&lt;/p&gt;

&lt;p&gt;In code, XOR looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;leftOperand&lt;/span&gt; &lt;span class="p"&gt;^&lt;/span&gt; &lt;span class="n"&gt;rightOperand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Result: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executing the preceding code, we get &lt;code&gt;Result: True&lt;/code&gt; as the console output because &lt;strong&gt;one of the operands is true but not both&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; based on my personal experiences, this operator is not used very often.&lt;br&gt;
Nevertheless, I think it is worth knowing of its existence, for those few times.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, that's your turn to practice what we just covered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;The exercise will focus on the logic part and not on the code part. We will use this knowledge in the next installment, where we will learn to write conditional code based on boolean logic. For now, try to answer the following questions without consulting the logic tables.&lt;/p&gt;

&lt;p&gt;What is the result of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;true &amp;amp;&amp;amp; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;true || true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;!true &amp;amp;&amp;amp; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;true || !true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;true ^ true&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you are done, compare your results with the answers below:&lt;/p&gt;

&lt;p&gt;
  Answers
  &lt;ol&gt;
&lt;li&gt;&lt;code&gt;true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;/p&gt;
&lt;p&gt;Good job! You completed another small chapter of your programming journey.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored the three basic operations of Boolean algebra: &lt;code&gt;AND&lt;/code&gt;, &lt;code&gt;OR&lt;/code&gt;, and &lt;code&gt;NOT&lt;/code&gt;. Each of them has a &lt;em&gt;logical table&lt;/em&gt; that lists the expected output based on the inputs. For example, AND returns &lt;code&gt;true&lt;/code&gt; only when both operands are &lt;code&gt;true&lt;/code&gt;, while OR returns &lt;code&gt;false&lt;/code&gt; only when both operands are &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then we looked at the compound operator XOR (exclusive OR). That operator helps simplify certain scenarios where you want a result of &lt;code&gt;true&lt;/code&gt; when only one of the two operands is &lt;code&gt;true&lt;/code&gt; but not both.&lt;/p&gt;

&lt;p&gt;This is very important and will be used in subsequent articles to learn to use boolean algebra to write conditional logic. We will also explore more complex scenarios and some laws to help you simplify your conditional logic. This was a theoretical article that we will practice in the next one of the series.&lt;/p&gt;

&lt;p&gt;All in all, Boolean algebra is one of the bases of programming that you can't escape. Please leave a comment below if you have questions or comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Writing conditional code blocks with if-else selection statements&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/"&gt;my blog&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Escaping characters in C# strings</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 18 Apr 2021 18:44:18 +0000</pubDate>
      <link>https://forem.com/carlhugom/escaping-characters-in-c-strings-3p2d</link>
      <guid>https://forem.com/carlhugom/escaping-characters-in-c-strings-3p2d</guid>
      <description>&lt;p&gt;In this article, we look at escaping characters in C# strings.&lt;br&gt;
But what is escaping, you may wonder?&lt;br&gt;
That's how we write special characters or characters that would otherwise be impossible to include in a string, like &lt;code&gt;"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a sub-series, starting with &lt;a href="https://www.forevolve.com/en/articles/2021/03/21/learn-coding-with-dot-net-core-part-6/"&gt;Introduction to string concatenation&lt;/a&gt;.&lt;br&gt;
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.&lt;br&gt;
If you are already reading the whole series in order, please discard this word of advice.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Escaping characters
&lt;/h2&gt;

&lt;p&gt;As mentioned in the introduction, the action of escaping characters means you write an "escape sequence" to represent specific characters.&lt;br&gt;
An escape sequence starts with a &lt;code&gt;\&lt;/code&gt;. It is followed by a character or a series of numbers representing the Unicode character code.&lt;/p&gt;

&lt;p&gt;Here are a few examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;quotes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Wrap \"a few words\" with quotes."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;newLine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Add a\nnew line."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tab&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"\tAdd a tab at the begining of the string."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"This is the letter e: \u0065"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quotes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tab&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run the previous code, we obtain the following result:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-04-output-escaped-characters.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-04-output-escaped-characters.png" alt="Code sample's console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we take a closer look:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  We can write &lt;code&gt;\"&lt;/code&gt; to insert a &lt;code&gt;"&lt;/code&gt; character inside a string, which would be impossible otherwise.&lt;/li&gt;
&lt;li&gt;  We can use the special &lt;code&gt;\n&lt;/code&gt; to insert a new line (see below for more special characters).&lt;/li&gt;
&lt;li&gt;  We can use the special &lt;code&gt;\t&lt;/code&gt; to insert a horizontal tab (see below for more special characters).&lt;/li&gt;
&lt;li&gt;  We can use the Unicode representation of a character, prefixed by &lt;code&gt;\u&lt;/code&gt;, to insert that character into a string (UTF-16 or UTF-32).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my opinion, the most helpful special characters are:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Escape sequence&lt;/th&gt;
&lt;th&gt;Character name&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Single quote&lt;/td&gt;
&lt;td&gt;Not related to &lt;code&gt;string&lt;/code&gt; but to &lt;code&gt;char&lt;/code&gt;, like &lt;br&gt;&lt;code&gt;var q = '\'';&lt;/code&gt; (not covered yet).&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Double quote&lt;/td&gt;
&lt;td&gt;See preceding example.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\\&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Backslash&lt;/td&gt;
&lt;td&gt;&lt;code&gt;var s = "c:\\folder\\some-file.ext";&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;New line&lt;/td&gt;
&lt;td&gt;See preceding example.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Carriage return&lt;/td&gt;
&lt;td&gt;Read the OS specific line break section&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Horizontal tab&lt;/td&gt;
&lt;td&gt;See preceding example.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\u0000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unicode escape sequence (UTF-16)&lt;/td&gt;
&lt;td&gt;To write the character &lt;code&gt;e&lt;/code&gt;: &lt;code&gt;\u0065&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\U00000000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unicode escape sequence (UTF-32)&lt;/td&gt;
&lt;td&gt;To write the character &lt;code&gt;e&lt;/code&gt;: &lt;code&gt;\U00000065&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\x0[0][0][0]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unicode escape sequence (UTF-16) with variable length&lt;/td&gt;
&lt;td&gt;To write the character &lt;code&gt;e&lt;/code&gt;: &lt;code&gt;\x65&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;See &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#string-escape-sequences"&gt;String Escape Sequences&lt;/a&gt; for some more characters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, we explore line break.&lt;/p&gt;

&lt;h2&gt;
  
  
  Platforms-specific line break
&lt;/h2&gt;

&lt;p&gt;In case you did not know, Windows line breaks are different from Unix-based platforms.&lt;br&gt;
Windows uses a sequence of both &lt;em&gt;carriage return&lt;/em&gt; and &lt;em&gt;new line&lt;/em&gt; (&lt;code&gt;\r\n&lt;/code&gt;) while Unix uses only a &lt;em&gt;new line&lt;/em&gt; (&lt;code&gt;\n&lt;/code&gt;).&lt;br&gt;
Windows is pretty forgiving and will most likely understand &lt;code&gt;\n&lt;/code&gt;.&lt;br&gt;
Nevertheless, in a cross-platform app targeting both Windows and Unix (e.g., Linux and Mac), do you really want to leave line breaks to chance?&lt;br&gt;
To save the day, we can use the &lt;code&gt;Environment.NewLine&lt;/code&gt; property instead, which adapts to the current OS.&lt;/p&gt;

&lt;p&gt;Here is an example (same output as the &lt;code&gt;newLine&lt;/code&gt; variable of the preceding example):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Add a&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewLine&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;new line."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// We leveraged interpolation here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we have many line breaks, this can become tedious.&lt;br&gt;
Hopefully, we can simplify this by leveraging the &lt;code&gt;string.Format&lt;/code&gt; method we cover next.&lt;/p&gt;
&lt;h2&gt;
  
  
  string.Format
&lt;/h2&gt;

&lt;p&gt;We can use the &lt;code&gt;string.Format&lt;/code&gt; method to format a string.&lt;br&gt;
It is similar to interpolation, but instead of inserting the variables directly, we need to define numerical tokens, like &lt;code&gt;{0}&lt;/code&gt;, &lt;code&gt;{1}&lt;/code&gt;, &lt;code&gt;{2}&lt;/code&gt;, and so forth.&lt;br&gt;
Then, we pass arguments that match those tokens in the correct order to get the final result.&lt;/p&gt;

&lt;p&gt;Many other .NET methods allow such a construct, including &lt;code&gt;Console.WriteLine&lt;/code&gt;.&lt;br&gt;
Here is an example of both &lt;code&gt;string.Format&lt;/code&gt; and &lt;code&gt;Console.WriteLine&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;newLine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Add a{0}new line."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Add a{0}new line."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When running the code, both &lt;code&gt;Console.WriteLine&lt;/code&gt; output the same result.&lt;br&gt;
The advantage here is writing &lt;code&gt;{0}&lt;/code&gt; every time we need a line break.&lt;br&gt;
Of course, we could use interpolation with &lt;code&gt;{Environment.NewLine}&lt;/code&gt; directly, but it makes the string longer.&lt;br&gt;
Even worst, we could use concatenation like &lt;code&gt;"Add a" + Environment.NewLine + "new line."&lt;/code&gt;; I find this to be the hardest code to read, making the string even longer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how does that work?&lt;/strong&gt;&lt;br&gt;
In the background, the framework replaces the numerical tokens with the specified arguments.&lt;/p&gt;

&lt;p&gt;There are more to &lt;code&gt;string.Format&lt;/code&gt;, including the possibility to format your values, but that's getting more and more out of the scope of our main subject.&lt;br&gt;
Next, we look at escaping characters in interpolated strings.&lt;/p&gt;
&lt;h2&gt;
  
  
  Interpolation-specific escaping
&lt;/h2&gt;

&lt;p&gt;In an interpolated string, we use the characters &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt; to wrap the element to insert, like a variable.&lt;br&gt;
What if we want to write those characters?&lt;br&gt;
It is as easy as doubling them.&lt;br&gt;
For &lt;code&gt;{&lt;/code&gt;, we write &lt;code&gt;{{&lt;/code&gt;.&lt;br&gt;
For &lt;code&gt;}&lt;/code&gt;, we write &lt;code&gt;}}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Darth Vader"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="n"&gt;Hello&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}}}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As simple as that, the preceding code will output &lt;code&gt;{Hello Darth Vader}&lt;/code&gt;.&lt;br&gt;
Next, let's have a look at the verbatim identifier (a.k.a. multiline strings).&lt;/p&gt;

&lt;h2&gt;
  
  
  The verbatim identifier
&lt;/h2&gt;

&lt;p&gt;The verbatim identifier (&lt;code&gt;@&lt;/code&gt;) allows us to write multiline strings, but it's not all.&lt;br&gt;
It also disables the escaping of characters, which can be very handy when writing file paths (amongst other things).&lt;br&gt;
In other words, a backslash is just a backslash, not a special character that starts an escape sequence.&lt;br&gt;
For example, it is more convenient to write &lt;code&gt;var path = @"c:\folder\some-file.ext";&lt;/code&gt; than &lt;code&gt;var path = "c:\\folder\\some-file.ext";&lt;/code&gt; (simple vs double backslash).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Since &lt;code&gt;\[character]&lt;/code&gt; does not work, how can we escape &lt;code&gt;"&lt;/code&gt;?&lt;/strong&gt;&lt;br&gt;
Like with interpolation, we only have to double it, like &lt;code&gt;var v = @"Some ""quoted words"" here.";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The verbatim identifier can also be used to escape reserved keywords, like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;public&lt;/code&gt;, etc.&lt;br&gt;
For example, if you'd like to create a variable named &lt;code&gt;public&lt;/code&gt;, you can't write &lt;code&gt;var public = "some value";&lt;/code&gt; because this won't compile.&lt;br&gt;
However, you could prefix your variable name with &lt;code&gt;@&lt;/code&gt; to make it work, like this: &lt;code&gt;var @public = "some value";&lt;/code&gt;.&lt;br&gt;
Using this makes your code a little harder to read, but that's a trick that can come in very handy sometimes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; one place where it was handy was to create &lt;code&gt;class&lt;/code&gt; attributes in an old ASP.NET MVC version because &lt;code&gt;class&lt;/code&gt; is a reserved keyword: we had to escape it using the verbatim identifier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference:&lt;/strong&gt; if you want to know more about reserved keywords, please visit the &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/"&gt;C# Keywords&lt;/a&gt; page in the official documentation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, it's your turn to try it out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;In this exercise, you will write a program that writes code.&lt;/p&gt;

&lt;p&gt;Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post &lt;a href="https://www.forevolve.com/en/articles/2021/04/18/learn-coding-with-dot-net-core-part-8/#exercise"&gt;on my blog&lt;/a&gt;. I'm sorry for the inconvenience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored the string type a little more.&lt;br&gt;
We discovered that the backslash character (&lt;code&gt;\&lt;/code&gt;) is used to escape characters or to create special characters like tabs (&lt;code&gt;\t&lt;/code&gt;) and line breaks (&lt;code&gt;\r&lt;/code&gt; and &lt;code&gt;\n&lt;/code&gt;).&lt;br&gt;
We then learned about the &lt;code&gt;Environment.NewLine&lt;/code&gt; property that gives us the correct platforms-specific line break.&lt;br&gt;
We also peaked at &lt;code&gt;string.Format&lt;/code&gt; to format strings using numerical tokens instead of interpolation.&lt;/p&gt;

&lt;p&gt;Afterward, we explored some edge cases to escape &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt; in interpolated strings and &lt;code&gt;"&lt;/code&gt; in multiline strings.&lt;br&gt;
The solution was to double the characters; a.k.a. write &lt;code&gt;""&lt;/code&gt; to obtain &lt;code&gt;"&lt;/code&gt;.&lt;br&gt;
We saw that in &lt;code&gt;@"..."&lt;/code&gt; strings, the escape character (&lt;code&gt;\&lt;/code&gt;) does not work, which is an advantage that can become a disadvantage.&lt;br&gt;
We finally learned to use the verbatim identifier (&lt;code&gt;@&lt;/code&gt;) to bypass the reserved keywords limitation and use it as an escape character for identifiers.&lt;/p&gt;

&lt;p&gt;All in all, that's a lot of new content that concludes our introduction to strings mini-sub-series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Introduction to boolean algebra&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/contact/"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to string interpolation</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 28 Mar 2021 14:57:20 +0000</pubDate>
      <link>https://forem.com/carlhugom/introduction-to-string-interpolation-g4g</link>
      <guid>https://forem.com/carlhugom/introduction-to-string-interpolation-g4g</guid>
      <description>&lt;p&gt;In this article, we continue to explore string manipulations by focusing on interpolation.&lt;br&gt;
Instead of concatenating many pieces together, interpolation allows us to insert special tokens inside a string.&lt;br&gt;
A value then replaces those tokens.&lt;br&gt;
Interpolation and concatenation play the same role, but often one ends up being more elegant than the other and makes the code easier to maintain.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is part of a sub-series, starting with &lt;a href="https://www.forevolve.com/en/articles/2021/03/21/learn-coding-with-dot-net-core-part-6/"&gt;Introduction to string concatenation&lt;/a&gt;.&lt;br&gt;
It is not mandatory to read all articles in order, but I strongly recommend it, especially if you are a beginner.&lt;br&gt;
If you are already reading the whole series in order, please discard this word of advice.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Interpolation
&lt;/h2&gt;

&lt;p&gt;The definition of &lt;em&gt;interpolation&lt;/em&gt;, from Oxford Languages (&lt;a href="https://www.google.com/search?q=interpolation&amp;amp;oq=interpolation"&gt;Google search&lt;/a&gt;), is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The insertion of something of a different nature into something else.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In C#, we must prefix the string with the &lt;code&gt;$&lt;/code&gt; character if we want to use interpolation.&lt;br&gt;
Then, we can insert the value of an expression in that string by wrapping it with &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Joe"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"Hello &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing the preceding code should write &lt;code&gt;Hello Joe!&lt;/code&gt; in the terminal.&lt;br&gt;
I find &lt;code&gt;$"Hello {name}!"&lt;/code&gt; to be more elegant than &lt;code&gt;"Hello " + name + "!"&lt;/code&gt; and way easier to read, especially for a neophyte.&lt;/p&gt;

&lt;p&gt;Next, we explore how to use interpolation in a multiline string.&lt;/p&gt;
&lt;h3&gt;
  
  
  Multiline string interpolation
&lt;/h3&gt;

&lt;p&gt;If you wondered why I talked about multiline strings in the previous article, I planned this section.&lt;br&gt;
We can use both &lt;code&gt;$&lt;/code&gt; and &lt;code&gt;@&lt;/code&gt; to mix interpolation and multiline string, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Joe"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$@"Hello &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;!&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s"&gt;What's up?"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, we used interpolation in a multiline string by chaining both &lt;code&gt;$&lt;/code&gt; and &lt;code&gt;@&lt;/code&gt; (in this order).&lt;br&gt;
As easy as that!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; the order is important. If you inverse the symbols order (&lt;code&gt;@$"..."&lt;/code&gt; instead of &lt;code&gt;$@"..."&lt;/code&gt;), the code will not compile.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, it is your turn to try it out!&lt;/p&gt;
&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;To practice interpolation, we will replace concatenation with interpolation in the code from the previous article's exercise.&lt;/p&gt;

&lt;p&gt;Here is the previous solution as a reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"IntroToDotNet"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your first name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your last name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Greetings "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post &lt;a href="https://www.forevolve.com/en/articles/2021/03/28/learn-coding-with-dot-net-core-part-7/#exercise"&gt;on my blog&lt;/a&gt;. I'm sorry for the inconvenience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored interpolation as a way to replace concatenation in certain scenarios.&lt;br&gt;
At this point, using one or the other is only a matter of taste.&lt;/p&gt;

&lt;p&gt;To use interpolation, we need to prefix a string with &lt;code&gt;$&lt;/code&gt;.&lt;br&gt;
Inside that string, we can then wrap an expression, like a variable, with &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;.&lt;br&gt;
The program will replace that token at runtime with the expression's value.&lt;/p&gt;

&lt;p&gt;We also saw that we could use interpolation with multiline strings by prefixing the string with both special characters, like this: &lt;code&gt;$@"..."&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Escaping characters in C# strings&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/contact/"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to string concatenation</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Fri, 26 Mar 2021 02:07:41 +0000</pubDate>
      <link>https://forem.com/carlhugom/introduction-to-string-concatenation-2he1</link>
      <guid>https://forem.com/carlhugom/introduction-to-string-concatenation-2he1</guid>
      <description>&lt;p&gt;In this article, we dig a little more into the &lt;code&gt;string&lt;/code&gt; type.&lt;br&gt;
We also explore how to concatenate (combine) strings.&lt;br&gt;
As a programmer, you will often need to manipulate strings, concatenation and interpolation being two recurring themes.&lt;br&gt;
We will cover interpolation in the next installment.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;. If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place. I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;
&lt;h2&gt;
  
  
  Strings
&lt;/h2&gt;

&lt;p&gt;In C#, a string is a sequence of characters delimited by quotes (&lt;code&gt;"&lt;/code&gt;).&lt;br&gt;
We used strings in previous articles to write text to the console.&lt;/p&gt;

&lt;p&gt;As a reminder, here is how to create a string variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Superman"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We already used strings, so no need to linger too long here.&lt;br&gt;
Next, we explore some new content: multiline strings.&lt;/p&gt;
&lt;h3&gt;
  
  
  Multiline strings
&lt;/h3&gt;

&lt;p&gt;In C#, we can create multiline strings by prefixing the string with the &lt;code&gt;@&lt;/code&gt; character.&lt;br&gt;
As the name implies, the only difference is the possibility to set the content of the string over multiple lines.&lt;/p&gt;

&lt;p&gt;Here is an example of a multiline string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"This
is
a
multiline
string!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// More code here...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But keep an eye open for details.&lt;br&gt;
For example, in the following code, the first and last characters are a &lt;em&gt;new line&lt;/em&gt;, which may not be what you expected to write.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"
This
is
a
multiline
string!
"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// More code here...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiline strings are handy in different scenarios where you don't want to use concatenation.&lt;br&gt;
As an introduction to strings, I will not get into too many more details.&lt;br&gt;
However, there are many methods to manipulate strings, compare them, or optimize their usage.&lt;/p&gt;

&lt;p&gt;Next, we look into the main subject: concatenation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Concatenation
&lt;/h2&gt;

&lt;p&gt;Concatenation is the action of piecing multiple strings together to make a new one; combining strings if you wish.&lt;br&gt;
In C#, the concatenation operator is the &lt;code&gt;+&lt;/code&gt; symbol.&lt;br&gt;
The &lt;code&gt;+&lt;/code&gt; operator combines both string operands (the values on the left and right sides) into a new string.&lt;br&gt;
Let's look into an example to make learning easier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Greetings "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"from .NET!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;concatenatedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding code combines the strings &lt;code&gt;"Greetings "&lt;/code&gt; and &lt;code&gt;"from .NET!"&lt;/code&gt; into &lt;code&gt;"Greetings from .NET!"&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; the space after the word &lt;code&gt;Greetings&lt;/code&gt;. Without it, the combined string would have been &lt;code&gt;"Greetingsfrom .NET!"&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another way to do it, which is more likely to happen than what we just did, is assigning a value to a variable and then updating that value.&lt;br&gt;
Here is an example of that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Greetings "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"from .NET!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;concatenatedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preceding code does the same as &lt;code&gt;"Greetings " + "from .NET!"&lt;/code&gt;, but in two steps (bullets 1 and 2):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The value &lt;code&gt;"Greetings "&lt;/code&gt; is assigned to the &lt;code&gt;concatenatedString&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;The value &lt;code&gt;"from .NET!"&lt;/code&gt; is added at the end of the &lt;code&gt;concatenatedString&lt;/code&gt; variable, then reassigned to it.&lt;/li&gt;
&lt;li&gt;The program writes &lt;code&gt;"Greetings from .NET!"&lt;/code&gt; to the console.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look more into the line &lt;code&gt;concatenatedString = concatenatedString + "from .NET!";&lt;/code&gt; as it may be harder to understand at first.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Reminder:&lt;/strong&gt; the assignation operator (&lt;code&gt;=&lt;/code&gt;) has the lowest priority, so the code on the right (the right-hand operand) is processed first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is an image to help analyze the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.forevolve.com%2Fblog%2Fimages%2F2021%2F2021-learn-to-code-part-6.1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.forevolve.com%2Fblog%2Fimages%2F2021%2F2021-learn-to-code-part-6.1.png" alt="Code execution order"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program ready the literal string &lt;code&gt;"Greetings "&lt;/code&gt; for step 2.&lt;/li&gt;
&lt;li&gt;The program assigns the value of the right-hand operand (&lt;code&gt;"Greetings "&lt;/code&gt;) to the &lt;code&gt;concatenatedString&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;The program ready the current value of the &lt;code&gt;concatenatedString&lt;/code&gt; variable for step 5 (&lt;code&gt;"Greetings "&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The program ready the literal string &lt;code&gt;"from .NET!"&lt;/code&gt; for step 5.&lt;/li&gt;
&lt;li&gt;The program concatenates (combines) the two strings into a new one.&lt;/li&gt;
&lt;li&gt;The program assigns the value of the right-hand operand (&lt;code&gt;"Greetings from .NET!"&lt;/code&gt;) to the &lt;code&gt;concatenatedString&lt;/code&gt; variable, replacing its value.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can see steps 3 to 5 as evaluated first, like one big step, then the program continues at step 6.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we covered that, there is a shortcut to this process: the &lt;code&gt;+=&lt;/code&gt; operator.&lt;br&gt;
You can see the &lt;code&gt;+=&lt;/code&gt; operator as a combination of both concatenation (&lt;code&gt;+&lt;/code&gt;) and assignation (&lt;code&gt;=&lt;/code&gt;).&lt;br&gt;
The previous example, using the &lt;code&gt;+=&lt;/code&gt; operator, would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Greetings "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;concatenatedString&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"from .NET!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;concatenatedString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second line of code is simplified compared to the previous bloc.&lt;br&gt;
It does the same, without the need to specify that &lt;code&gt;concatenatedString = concatenatedString + [something else]&lt;/code&gt;.&lt;br&gt;
This new syntax removes unneeded pieces, reducing the length of the line of code, most likely even making it easier to read.&lt;/p&gt;

&lt;p&gt;Enough theory; next, it's your turn to try it out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;To practice concatenation, we will update the code from the previous article's exercise, but with a twist. &lt;/p&gt;

&lt;p&gt;Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post &lt;a href="https://www.forevolve.com/en/articles/2021/03/21/learn-coding-with-dot-net-core-part-6/#exercise" rel="noopener noreferrer"&gt;on my blog&lt;/a&gt;. I'm sorry for the inconvenience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned that we could write multiline strings if we need to.&lt;br&gt;
We also learned about concatenation, which combines multiple strings into a new one.&lt;br&gt;
The concatenation operator is the symbol &lt;code&gt;+&lt;/code&gt;.&lt;br&gt;
We also look at the &lt;code&gt;+=&lt;/code&gt; operator. &lt;code&gt;+=&lt;/code&gt; allows us to combine both the concatenation and assignation operators. Using it can simplify our code and remove a step.&lt;br&gt;
In programs, concatenation is often used, which makes it an essential piece of knowledge to have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Introduction to string interpolation&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, &lt;a href="https://twitter.com/CarlHugoM" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/contact/" rel="noopener noreferrer"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to read user inputs from a console</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 14 Mar 2021 15:40:22 +0000</pubDate>
      <link>https://forem.com/carlhugom/how-to-read-user-inputs-from-a-console-5fce</link>
      <guid>https://forem.com/carlhugom/how-to-read-user-inputs-from-a-console-5fce</guid>
      <description>&lt;p&gt;In this article, we explore how to read user inputs from the console.&lt;br&gt;
This article is the foundation of more dynamic notions enabling our programs to change based on user interactions and react to them.&lt;br&gt;
We also learn how to change the title of the console and how to delete its content.&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;
&lt;h2&gt;
  
  
  User input
&lt;/h2&gt;

&lt;p&gt;So far, we used the &lt;code&gt;Console.Write&lt;/code&gt; and &lt;code&gt;Console.WriteLine&lt;/code&gt; methods to write to the console.&lt;br&gt;
We also created variables and constants to hold and reuse some text.&lt;br&gt;
However, we don't know how to interact with the user yet.&lt;/p&gt;

&lt;p&gt;There are multiple types of applications, like web apps, mobile apps, and Windows apps.&lt;br&gt;
In our case, we will continue to use console applications because they are the simplest.&lt;br&gt;
We don't need to bother with complex user interfaces, animation, or interaction, and we can focus on learning to program.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Console apps may feel less exciting, but that allows us to focus on only one subject at a time.&lt;br&gt;
Remember that every piece of knowledge that you are acquiring is like a new LEGO&lt;sup&gt;®&lt;/sup&gt; block that you'll be able to piece with the others later.&lt;br&gt;
Moreover, what you are learning in this series is reusable in most, if not all, other types of apps.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;Console&lt;/code&gt; class offers three methods to read user inputs, &lt;code&gt;Read&lt;/code&gt;, &lt;code&gt;ReadKey&lt;/code&gt;, and &lt;code&gt;ReadLine&lt;/code&gt;.&lt;br&gt;
We can use the first two for more complex scenarios.&lt;br&gt;
The third one is very straightforward and is the method we are focusing on in this article.&lt;/p&gt;

&lt;p&gt;As its name implies, &lt;code&gt;Console.ReadLine&lt;/code&gt; reads the line entered by the user.&lt;br&gt;
It is simple and gives us the power to accomplish what we need, to learn the basic programming concepts.&lt;/p&gt;
&lt;h2&gt;
  
  
  Reading a line entered by a user
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Console.ReadLine&lt;/code&gt; method returns a &lt;code&gt;string&lt;/code&gt;, representing the line written in the console by the user.&lt;br&gt;
Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter a greeting message, then press ENTER: "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the preceding code, we write a message to the user, then wait for an input.&lt;br&gt;
The program will block its execution there until the user hits the &lt;code&gt;&amp;lt;ENTER&amp;gt;&lt;/code&gt; key.&lt;br&gt;
At this point, it will resume and continue, then write the read line back to the console.&lt;/p&gt;

&lt;p&gt;Here is the console content when running the program and entering &lt;code&gt;Hello Amigo!&amp;lt;ENTER&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Please enter a greeting message, then press ENTER: Hello Amigo!
Hello Amigo!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; it is important to note that the &lt;em&gt;new line character&lt;/em&gt; (the &lt;code&gt;&amp;lt;ENTER&amp;gt;&lt;/code&gt;) is not part of the line (not saved in the &lt;code&gt;hello&lt;/code&gt; variable).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we saw an example, let's explore the flow of execution of the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flow of execution
&lt;/h2&gt;

&lt;p&gt;The program flow, or flow of execution, represents the order in which the program executes the instructions (lines of code).&lt;br&gt;
In our case, the code is linear; it starts at the top and ends at the bottom of the &lt;code&gt;Program.cs&lt;/code&gt; file.&lt;br&gt;
Nevertheless, the &lt;code&gt;Console.ReadLine()&lt;/code&gt; method blocks the program, waiting for a user input, which disturbs the flow.&lt;/p&gt;

&lt;p&gt;Here is what happens:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-2.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-2.png" alt="Code sequence"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program writes the question to the console.&lt;/li&gt;
&lt;li&gt;The program executes the right-end of the assignation operator (&lt;code&gt;=&lt;/code&gt;), the &lt;code&gt;Console.ReadLine()&lt;/code&gt; method, which waits for the user to hit the &lt;code&gt;&amp;lt;ENTER&amp;gt;&lt;/code&gt; key.
&amp;gt; &lt;strong&gt;More info:&lt;/strong&gt; The assignation operator &lt;code&gt;=&lt;/code&gt; is always the last to be executed; it has the lowest priority.&lt;/li&gt;
&lt;li&gt;The user types &lt;code&gt;Hello Amigo!&lt;/code&gt; then hit &lt;code&gt;&amp;lt;ENTER&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The program then resumes and assigns the user-entered value to the &lt;code&gt;hello&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;The program writes that input back to the console.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a second way to visualize this flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-graph-5.png" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-graph-5.png" alt="Code sequence graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the &lt;code&gt;Console.ReadLine()&lt;/code&gt; method manages the bifurcation even if code-wise, the flow is linear.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We will learn ways to control a program's flow in future articles.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, it is your turn to try this out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;To practice all that we explored so far, including user-inputs, you must write a program that asks the following two questions to the user:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;What is your first name?&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;What is your last name?&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then, the program must greet that user using the following format: &lt;code&gt;Greetings {first name} {last name}!&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Assuming the user entered &lt;code&gt;Carl-Hugo&lt;/code&gt; as the first name and &lt;code&gt;Marcotte&lt;/code&gt; as the last name, the greeting message would read &lt;code&gt;Greetings Carl-Hugo Marcotte!&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately, I was not able to recreate the whole exercise on this platform, so please look at the exercise on the original post &lt;a href="https://www.forevolve.com/en/articles/2021/03/14/learn-coding-with-dot-net-core-part-5/#exercise"&gt;on my blog&lt;/a&gt;. I'm sorry for the inconvenience.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bonus information
&lt;/h2&gt;

&lt;p&gt;In this short section, we explore two more manipulations of the console:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  How to set its title.&lt;/li&gt;
&lt;li&gt;  How to clear what is written in it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The block of code at the end of this section contains the preceding exercise's solution; please be advised.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting a custom console title
&lt;/h3&gt;

&lt;p&gt;If you want to change the console title, you can set the &lt;code&gt;Console.Title&lt;/code&gt; property to the string of your choice, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"IntroToDotNet"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty straightforward, isn't it?&lt;br&gt;
Next, let's see how to clear the text from the console.&lt;/p&gt;
&lt;h3&gt;
  
  
  Clearing the console
&lt;/h3&gt;

&lt;p&gt;One last bit of knowledge here: we can clear the console using the &lt;code&gt;Console.Clear()&lt;/code&gt; method.&lt;br&gt;
So instead of the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What is your first name? Carl-Hugo
What is your last name? Marcotte
Greetings Carl-Hugo Marcotte!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could clear the console between each question to obtain the following flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-ConsoleClear-2.gif" class="article-body-image-wrapper"&gt;&lt;img src="//cdn.forevolve.com/blog/images/2021/2021-03-00-learn-to-code-input-ConsoleClear-2.gif" alt="Adding Console.Clear calls"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the code to achieve that result:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Program.cs&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"IntroToDotNet"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Custom title&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your first name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Clear after the first question&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What is your last name? "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Clear after the second question&lt;/span&gt;

&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Greetings "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it for this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we explored how to read user inputs from the console.&lt;br&gt;
We used the &lt;code&gt;ReadLine&lt;/code&gt; method to acquire the value a user wrote before hitting the &lt;code&gt;&amp;lt;ENTER&amp;gt;&lt;/code&gt; key.&lt;br&gt;
Interacting with the user is the foundation of the next many articles where we will use this to acquire and manipulate data typed by the user.&lt;/p&gt;

&lt;p&gt;We also looked at how to change the title of the terminal Window because why not, right?&lt;br&gt;
Finally, we explored how to clear the text to reset the console to an empty state.&lt;br&gt;
This second interlude can be very handy at crafting a better user experience (UX).&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Introduction to string concatenation and interpolation&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, Twitter, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/contact/"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to C# comments</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 07 Mar 2021 16:19:31 +0000</pubDate>
      <link>https://forem.com/carlhugom/introduction-to-c-comments-3pkd</link>
      <guid>https://forem.com/carlhugom/introduction-to-c-comments-3pkd</guid>
      <description>&lt;p&gt;In this article, we explore how to write comments.&lt;br&gt;
Comments are simply human-readable pieces of text that are not compiled nor interpreted.&lt;br&gt;
A comment is usually a note that we can leave in the code for the next programmer to get into that code or for us.&lt;br&gt;
I left a few comments in preceding installments; did you noticed them?&lt;/p&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;
&lt;h2&gt;
  
  
  Comments
&lt;/h2&gt;

&lt;p&gt;There are three types of comments in C#:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Single-line&lt;/li&gt;
&lt;li&gt;  Multiline&lt;/li&gt;
&lt;li&gt;  XML documentation comments, also known as &lt;em&gt;triple-slash comments&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will only explore the first two.&lt;br&gt;
We can use the third style to document our code, but we don't know how to create such code; yet.&lt;/p&gt;

&lt;p&gt;We can use comments to leave notes in the source code.&lt;br&gt;
As a beginner, comments can be handy.&lt;br&gt;
The more you progress, the clearer your code will become and the less needed the comments are gonna be.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; I usually recommend to intermediate developers to write as close to zero comments as possible in their code.&lt;br&gt;
Comments too often become outdated and forgotten.&lt;/p&gt;

&lt;p&gt;For example, the code change, but the comment remain the same.&lt;br&gt;
In that case, the comment describes the first version of the code, but the second version does something else.&lt;br&gt;
This is the kind of situation that can lead a third developer in the wrong direction.&lt;br&gt;
Both the code and the comment can contradict themselves.&lt;/p&gt;

&lt;p&gt;On the other hand, comments can be useful to lay out ideas and algorithms, especially as a beginner.&lt;br&gt;
They can also be an excellent tool to reorganize logic into cohesive units.&lt;br&gt;
We can also use comments to diagnose and debug a feature.&lt;br&gt;
Enough of that; I'll most likely talk about that in future articles, in a more exciting context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, let's have a look at the first style that we are covering in this article.&lt;/p&gt;
&lt;h3&gt;
  
  
  Single-line comments
&lt;/h3&gt;

&lt;p&gt;We can write a single-line comment by writing &lt;code&gt;//&lt;/code&gt;.&lt;br&gt;
Everything that is written afterward, on the same line, becomes a comment that does nothing.&lt;/p&gt;

&lt;p&gt;Here are some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;tmp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"SOME TEXT"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is a comment that could describe this statement&lt;/span&gt;

&lt;span class="c1"&gt;// This is a comment that could describe the following code block&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"====================="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"===== "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" ====="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"====================="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the code, we get the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=====================
===== SOME TEXT =====
=====================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the comments are not executed or outputted. They are there only to inform the programmer about the code.&lt;/p&gt;

&lt;p&gt;Next, let's look at multiline comments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiline comments
&lt;/h3&gt;

&lt;p&gt;Multiline comments are like their name implies: they can cover multiple lines.&lt;br&gt;
Unlike single-line comments, multiline comments have an opening and a closing sequence of characters.&lt;br&gt;
Everything in between those two tokens is a comment.&lt;br&gt;
We can open a multiline comment with &lt;code&gt;/*&lt;/code&gt; and close it with &lt;code&gt;*/&lt;/code&gt; (same characters in the inverted order).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt; The numpad of a &lt;em&gt;standard keyboard&lt;/em&gt; makes it very easy to open and close a multiline comment in a smooth sequence of keystrokes: &lt;code&gt;/&lt;/code&gt; -&amp;gt; &lt;code&gt;*&lt;/code&gt; -&amp;gt; &lt;code&gt;*&lt;/code&gt; -&amp;gt; &lt;code&gt;/&lt;/code&gt; -&amp;gt; move your arrow between the two &lt;code&gt;*&lt;/code&gt;, hit &lt;code&gt;enter&lt;/code&gt; or write your comment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here are a few examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* This is a multiline comment, but only on one line; please don't do what I did with the following line in your code, it is just an example. */&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="cm"&gt;/* This */&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="cm"&gt;/* can */&lt;/span&gt;&lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="cm"&gt;/* be */&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="cm"&gt;/* almost */&lt;/span&gt;&lt;span class="s"&gt;"SOME TEXT"&lt;/span&gt;&lt;span class="cm"&gt;/* anywhere, */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="cm"&gt;/* as long as you don't break any token */&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;// We can also use single-line comments; one does not block the usage of the other.&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"====================="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cm"&gt;/*
We can also
use multiline
comments to
write longer
sequence of
text divided
onto multiple
lines.
*/&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"===== "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tmp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" ====="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"====================="&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the code, we get the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=====================
===== SOME TEXT =====
=====================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once again, the comments are not executed or outputted. They are there only to inform the programmer about the code.&lt;br&gt;
Moreover, as you may have noticed, multiline comments don't have to cover multiple lines.&lt;br&gt;
Unlike the single-line comments that start from &lt;code&gt;//&lt;/code&gt;, commenting the text until the end of that line, we can use multiline comments to comment out only a part of a line.&lt;/p&gt;

&lt;p&gt;And voilà! You have made another step toward learning to program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Comments are a tool to annotate and leave notes inside the code.&lt;br&gt;
We can write single-line comments beginning with &lt;code&gt;//&lt;/code&gt;, and we can write multiline comments between &lt;code&gt;/*&lt;/code&gt; and &lt;code&gt;*/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One essential thing to understand about comments is to use them to describe the functionally, not the code itself.&lt;br&gt;
For example, explaining to a C# programmer using a comment that &lt;code&gt;var text = "whatever";&lt;/code&gt; creates a variable named &lt;code&gt;text&lt;/code&gt; is not helpful.&lt;br&gt;
But explaining that the following block of code is an implementation of the XYZ algorithm could be.&lt;br&gt;
As I mentioned, at some point, you want to aim at writing clear enough code that you don't need to explain it.&lt;br&gt;
That said, as a beginner, please leverage comments to help you out.&lt;br&gt;
You can describe your code if that helps you remember or understand for example.&lt;br&gt;
Everyone learns differently.&lt;br&gt;
There is no wrong with how to learn, so if using comments helps you, please do!&lt;br&gt;
If you don't know, experiment with techniques and keep what works best for you, only you can find this part out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Introduction to user inputs&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, Twitter, or other places you can find me.&lt;br&gt;
You can look at &lt;a href="https://www.forevolve.com/contact/"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to C# constants</title>
      <dc:creator>Carl-Hugo Marcotte</dc:creator>
      <pubDate>Sun, 28 Feb 2021 14:01:21 +0000</pubDate>
      <link>https://forem.com/carlhugom/introduction-to-c-constants-4gid</link>
      <guid>https://forem.com/carlhugom/introduction-to-c-constants-4gid</guid>
      <description>&lt;p&gt;In this article, we explore constants.&lt;br&gt;
A constant is a special kind of variable.&lt;br&gt;
The kind that does not vary; they are &lt;strong&gt;immutable&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Immutable&lt;/strong&gt; means that it cannot change.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This article is part of a &lt;strong&gt;learn programming&lt;/strong&gt; series where you need &lt;strong&gt;no prior knowledge of programming&lt;/strong&gt;.&lt;br&gt;
If you want to &lt;strong&gt;learn how to program&lt;/strong&gt; and want to learn it &lt;strong&gt;using .NET/C#&lt;/strong&gt;, this is the right place.&lt;br&gt;
I suggest reading the whole series in order, starting with &lt;a href="https://dev.to/carlhugom/creating-your-first-net-c-program-1poj"&gt;Creating your first .NET/C# program&lt;/a&gt;, but that's not mandatory.&lt;/p&gt;
&lt;h2&gt;
  
  
  Definition of a constant
&lt;/h2&gt;

&lt;p&gt;Besides knowing that a constant's value cannot change, its &lt;strong&gt;value is replaced at compile-time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On the contrary, &lt;strong&gt;variables are evaluated at runtime&lt;/strong&gt;. Therefore, the program uses the value they hold at the time of their usage during the execution.&lt;/p&gt;

&lt;p&gt;To define a constant, we use the &lt;code&gt;const&lt;/code&gt; modifier in front of its type, followed by an identifier, an assignment operator, then its value; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello .NET Ninja!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;102&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; we can't use the &lt;code&gt;var&lt;/code&gt; keyword for constants.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at two use-cases, one using a variable and one using a constant.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable flow
&lt;/h3&gt;

&lt;p&gt;In the following code, the value of the &lt;code&gt;greetings&lt;/code&gt; variable is transferred to the &lt;code&gt;WriteLine&lt;/code&gt; method. It is then written to the console at runtime.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello .NET Ninja!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we change the value of the variable along the way, the output will be different; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello .NET Ninja!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"This is new!"&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executing the preceding code, the program will write two lines to the console, &lt;code&gt;Hello .NET Ninja!&lt;/code&gt; and &lt;code&gt;This is new!&lt;/code&gt;.&lt;br&gt;
That's because the evaluation is done at runtime.&lt;/p&gt;

&lt;p&gt;Next, we look at constants.&lt;/p&gt;
&lt;h3&gt;
  
  
  Constant flow
&lt;/h3&gt;

&lt;p&gt;In the case of a constant, its value is replaced at compile-time.&lt;br&gt;
Let's take the following code as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;greetings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello .NET Ninja!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During the compilation phase, the compiler replaces the identifier of the constant by its value.&lt;br&gt;
In our case, we end up with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello .NET Ninja!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executing the program, the result is the same as if we wrote the preceding code ourselves.&lt;br&gt;
The only difference is that the compiler did the job of replacing the value for us.&lt;/p&gt;

&lt;p&gt;Next, we look at what we can do with this.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to do with constants?
&lt;/h2&gt;

&lt;p&gt;As a neophyte, this may sound useless, but as your level of knowledge grows, it becomes more relevant.&lt;br&gt;
In general, constants are used to make your program easier to maintain.&lt;br&gt;
Here are a few examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1:&lt;/strong&gt; you do not want hard-coded values spread throughout your program.&lt;br&gt;
Doing so makes the program harder to maintain when you need to change one of those hard-coded values.&lt;br&gt;
You can also make a typo when typing the value manually.&lt;br&gt;
For example, you need to find the values, make sure you don't forget any, replace them, etc., leading to more risks of making a mistake in the process.&lt;br&gt;
To fix that issue, you can use constants to centralize all of those values in one place, making the program easier to maintain without impact on runtime performance because the constants are replaced at compile-time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2:&lt;/strong&gt; you want to reuse a specific value at multiple places in the code as a way to identify something (an identifier, a key, an index, a URL).&lt;br&gt;
Instead of duplicating that literal value, you can use a constant, which removes the duplication by depending only on the constant.&lt;br&gt;
If you need to change the value, you can update the constant, recompile, and voilà; all references to that constant now use the new updated value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;More info:&lt;/strong&gt; There is a downside to constants, which can happen in a more advanced scenario.&lt;br&gt;
When creating a project&lt;sup&gt;(1)&lt;/sup&gt; that depends on a constant from another project&lt;sup&gt;(2)&lt;/sup&gt;, if the author of project&lt;sup&gt;(2)&lt;/sup&gt; changes the value of a constant that project&lt;sup&gt;(1)&lt;/sup&gt; depends on, project&lt;sup&gt;(1)&lt;/sup&gt; needs to be recompiled for the change to be applied.&lt;br&gt;
This could, on rare occasions, have unintended consequences, where different values are used, leading to inconsistent states.&lt;/p&gt;

&lt;p&gt;Feel free to return to this note later as it is for more advanced scenarios.&lt;br&gt;
Maybe some or all of this makes no sense to you yet, which is normal.&lt;br&gt;
I decided to leave the note anyway because it is constant-related and important to know.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Exercise
&lt;/h2&gt;

&lt;p&gt;Unfortunately, I was not able to recreate the exercise on this platform, so please look at the exercise on the original post &lt;a href="https://www.forevolve.com/en/articles/2021/04/01/learn-coding-with-dot-net-core-part-4/#exercise"&gt;on my blog&lt;/a&gt;. I'm sorry for the inconvenience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned the constants' syntax.&lt;br&gt;
We covered how to create a constant and how to assign it a value.&lt;br&gt;
Like declaring a variable using the &lt;code&gt;var&lt;/code&gt; keyword, we must assign it a value when declaring it.&lt;br&gt;
We cannot use the &lt;code&gt;var&lt;/code&gt; keyword for constant and must specify its type instead.&lt;br&gt;
Unlike variables, once the constant is declared, we cannot change its value.&lt;/p&gt;

&lt;p&gt;The value of a constant is evaluated and replaced at compile-time, making it a great candidate to improve maintainability while keeping performance the same.&lt;br&gt;
As a reference, variables are evaluated at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;It is now time to move to the next article: &lt;strong&gt;Introduction to C# comments&lt;/strong&gt; which is coming soon. Stay tuned by following me on dev.to, Twitter, or other places you can find me. You can look at &lt;a href="https://www.forevolve.com/contact/"&gt;my blog contact page&lt;/a&gt; for more info.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
