Step 1: Intro
This is a .NET Console application written in C#. Select Run Code to try it out.
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
Console.WriteLine("Hello World!");
}
}
}
Step 2: Strings
Try modifying the code so that the console says hello to your name, instead of the world (for example, Hello Ana!
). Select Run Code to see if you got it right.
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
Console.WriteLine("Hello <name>!");
}
}
}
Step 3: Variables
Variables hold values that you can use elsewhere in your code.
Let's store your name in a variable, then read the value from that variable when creating the output message.
var name = "<name>";
Console.WriteLine("Hello " + name + "!");
Show me
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
var name = "<name>";
Console.WriteLine("Hello " + name + "!");
}
}
}
Step 4: String interpolation
String interpolation lets you piece together strings in a more concise and readable way.
If you add a $
before the opening quotes of the string, you can then include string values, like the name
variable, inside the string in curly brackets. Try it out and select Run Code.
Console.WriteLine($"Hello {name}!");
Show me
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
var name = "<name>";
Console.WriteLine($"Hello {name}!");
}
}
}
Step 5: Methods
Methods take inputs, do some work, and sometimes return a result.
ToUpper()
is a method you can invoke on a string, like the name
variable. It will return the same string, converted to uppercase.
Update the greeting to change the name of the person being greeted to uppercase, and select Run Code.
Console.WriteLine($"Hello {name.ToUpper()}!");
Show me
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
var name = "<name>";
Console.WriteLine($"Hello {name.ToUpper()}!");
}
}
}
Step 6: Collections
Collections hold multiple values of the same type.
Replace the name variable with a names variable that has a list of names. Then use a foreach loop to iterate over all the names and say hello to each person.
var names = new List<string> { "<name>", "Felipe", "Emillia" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
Show me
using System;
using System.Collections.Generic;
using System.Linq;
namespace myApp
{
class Program
{
static void Main()
{
var names = new List<string> { "<name>", "Felipe", "Emillia" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}
}
}
}