Welcome to your first introduction to programming in this course! We're excited you're here.
Throughout this course, we hope to challenge you to think outside the box, to learn quickly, and to collaborate well with others.
Programming is an amazing skill, and, by the end of this course, we hope to see you equipped with all the skills necessary to be a TrueCoder.
We're going to be teaching you to learn C# and .NET within this course. So, first things first. Let's get our Hello World app up and running... Because we might get struck by lightning if we did anything else.
Create the Hello World application using Visual Studio. Select "Create new project" and you'll see the New Project options:
In the created project, you'll see code that looks like this:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
}
}
}
We're going to add some code to our Main
method:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Add code here:
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
There are several ways to build and run your application. We're going to go over the different ways to do so:
Your app, at its core, will be built with the C# Compiler (csc
), and we'll show you how to access it directly.
cd
into the directory of your projectcsc HelloWorld/Program.cs
in the Command PromptProgram.exe
fileYou'll see Hello World
print out on your screen
The Microsoft Build Engine (MSBuild
) is the build platform for .NET and Visual Studio. MSBuild
is the build platform for .NET and Visual Studio:
msbuild HelloWorld.sln
.exe
file in the build resultsProgram.exe
fileYou'll see Hello World
print out on your screen
Our C# Editor and Integrated Development Environment (IDE) of Choice
We're glad that you now know how to run your app with just the Command Prompt. However, Visual Studio makes everything a whole lot easier:
You'll see Hello World
print out on your screen
Ok, we've got the basics. Now, let's kick it up a notch. Let's make the computer talk to us:
using System;
using System.Speech.Synthesis;
namespace HelloWorld
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome! What's your name?");
var input = Console.ReadLine();
var output = $"Awesome! Your name is: {input}";
Console.WriteLine(output);
Speak(output);
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
}
}
Ok, that's pretty cool. Now, let's tap into a random Chuck Norris joke and make the computer speak it to us:
using System;
using System.Net;
using System.Speech.Synthesis;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace HelloWorld
{
private const string Address = "https://api.chucknorris.io/jokes/random";
public class Program
{
static void Main(string[] args)
{
var joke = GetJoke();
Console.WriteLine(joke.Value);
Speak(joke.Value);
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
private static Joke GetJoke()
{
var client = new WebClient();
var str = client.DownloadString(Address);
return JsonConvert.DeserializeObject<Joke>(str);
}
private static void Speak(string textToSpeak)
{
var synth = new SpeechSynthesizer();
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
synth.Speak(textToSpeak);
}
}
public class Joke
{
public string Value { get; set; }
public string Url { get; set; }
[JsonProperty("icon_url")]
public string IconUrl { get; set; }
public string Id { get; set; }
}
}