Learn with me C# and .Net Lesson 2
Published: 2023-03-14 13:48:23

**Learn with me C# and .Net Lesson 2** 

If you missed the last review post from day 1 studies check it out here ➡️https://dev.to/melissapw/learn-with-me-c-and-net-day-1-lesson-review-20k

**Lesson 2 Objectives:**
* Data Types - strings
* Variables 
* String Interpolation
* .Length property
* Methods - Print - Trim - Replace -Contain


**Data types** represent the different types of information that we can use in our programs and how they should be used. 

**Data Types**
Data types tell us a few things about a piece of data, like:
* How it can be stored
* What operations we can perform with it
* Different methods it can be used with

Take a look at the following code below

The first line declares a variable, myName, and assigns it a value, “Melissa”. The second line prints out the name.

string myName = “Melissa”;
Console.WriteLine(myName);


Let’s break it down a little more.

The first word of the code string  is a data type used in C# and other coding languages. 

“When we use data in our programs, it’s good practice to save them in a variable.”

**What is a Variable?**
 A **variable** is basically like a box in our computer memory where we can store values used in our code which can be used later in the program.”

“Declaring the types of variables allows the compiler to stop the program from being run when variables are used incorrectly, i.e, an int being used when a string is needed or vice versa.”

string myName = “Melissa”;

myName is being used as a variable.

The equal sign is an **assignment operator**. The variable myName is being assigned the value of “Melissa”.


Like we previously learned in the last review post ➡️https://dev.to/melissapw/learn-with-me-c-and-net-day-1-lesson-review-20k

string myName = “Melissa”;

The ; semicolon is the **end of statement operator**; and tells the complier you’ve finished the command. 

string myName = “Melissa”;
Console.WriteLine(myName);

Console.WriteLine() is a _method_. A **method** is a block of code that implements some action. It has a name, so you can access it.

The Console.WriteLine() method prints myName to the console which would show Melissa. 

Let’s add hello to the code. Objective is to say Hello Melissa.

So we can add Hello which is still a string (datatype) to myName by putting Hello in quotes with a space then a plus sign myName. 

Console.WriteLine("Hello " + myName);

This should print Hello Melissa to the console.

This solution works but the best way to do this by _String interpolation_.

**String interpolation** makes your code more readable and convenient syntax to create formatted strings. This allows you to insert variables and expressions in a string so we don’t have to worry about punctuation or spaces.

You can add a $ before the opening quote of the string and place a variable between { } characters to tell C# to replace that text with the value of the variable.

Console.WriteLine($"Hello {myName}");

This would give you the same output as before. 
Hello Melissa

**Strings** are more than a collection of letters. The string class has a **Length property**, which returns the number of characters in the string. 

Here is an example using _string interpolation_ and the _.length property_ to find how many letters my favorite food Kimbap has.

string favFood = "Kimbap"; 
Console.WriteLine($"The word {favFood} has {favFood.Length} letters.");

The following would be printed to the console.

The word Kimbap has 6 letters.

**Methods** 
Previously we learned the Console.WriteLine() method and what a method is.

A **method** is a block of code that implements some action. It has a name, so you can access it.

You will use tons of methods throughout coding but another one we will discuss is the _Trim method_. 

The **Trim method** lets you remove space from a string if you had space around it. You can use **TrimStart** to remove space from the beginning of a string or **TrimEnd** to remove space from the end. 

**Example:** 

string breakfast = "       For breakfast I will be having a protein shake.       ";

First we created a variable called breakfast and assigned it a value of what I will be having for breakfast. Next we will print it to the console to see the output.

Console.WriteLine($"[{breakfast}]");

[       For breakfast I will be having a protein shake.       ]

The _square brackets_ [] are there to help visualize where whitespace starts and ends.

**TrimStart** 
**Example:** 
string trimmedBreakfast = breakfast.TrimStart();
Console.WriteLine($"[{trimmedBreakfast}]");

[For breakfast I will be having a protein shake.       ]

**TrimEnd**
**Example:**
trimmedBreakfast = breakfast.TrimEnd();
Console.WriteLine($"[{trimmedBreakfast}]");

[       For breakfast I will be having a protein shake.]

**Trim** 
**Example:**
trimmedBreakfast = breakfast.Trim();
Console.WriteLine($"[{trimmedBreakfast}]");

[For breakfast I will be having a protein shake.]

**Replace Method**
Searches for a substring and replaces it.
“The _Replace method_ takes two parameters. These are the strings between the parentheses. The first string is the text to search for. The second string is the text to replace it with.”

Lets use our code from above as an example:⬇️

[For breakfast I will be having a protein shake.]

trimmedBreakfast = trimmedBreakfast.Replace("a protein shake", "toast and eggs");
Console.WriteLine(trimmedBreakfast);

The output would be
               ⬇️
For breakfast I will be having toast and eggs.

**Contain Method**
“The other part of a search and replace operation is to find text in a string. You can use the _Contains method_ for searching. It tells you if a string contains a substring inside it.”

string movieTitles = "Ant-Man, Avengers, 
MoonKnight, Shang-Chi, Iron Man";
Console.WriteLine(movieTitles);

Lets test the contains method and see if our list includes the Avengers

Console.WriteLine(movieTitles.Contains("Avengers"));

True

We got back a boolean which states true that Avengers is included in our list. 

This checks to see if our list includes Thor
Console.WriteLine(movieTitles.Contains("Thor"));

False 

**Stay tuned for the review post**

Learning Resources:
Learn C#
https://www.codecademy.com/learn/learn-c-sharp

Introduction to C# 
https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/

.NET for Beginners https://youtube.com/playlist?list=PLdo4fOcmZ0oWoazjhXQzBKMrFuArxpW80

Here is a in-browser tutorial of .NET https://dotnet.microsoft.com/en-us/learn/dotnet/in-browser-tutorial/1