DotNet-100-PreWork-GitBook
  • DotNet PreWork
  • Part 0: Intro
    • 1.0: Installation
    • 2.0: Getting Started
    • 3.0: Configuration
  • Part 1: HTML Fundamentals
    • Basic Tags
    • H & P Tags
    • Lists
    • Links
    • Images
    • Tables
    • Divs
    • Inputs
    • Forms
    • Sections
    • Articles & Headers
    • Footers
    • Navs
    • iFrames
  • Part 2: CSS Fundamentals
    • CSS Set Up
    • Classes
    • Ids
    • Margins & Padding
    • Fonts
    • Backgrounds
    • Widths & Heights
    • Borders
    • Positions
  • Part 3: C# Fundamentals
    • 1.0: Hello World
    • 2.0: Variables
    • 3.0: Basic Types
    • 3.0a: Types Challenges
    • 3.0b: Types Challenges Answers
    • 4.0: Operators
    • 5.0: Strings
    • 6.0: Booleans
      • 6.0a: Boolean Challenge
      • 6.0b: Challenge Answers
    • 7.0: Conditionals
      • 7.1: Switch Case
      • 7.2: If Statements
      • 7.3: Ternary Expressions
    • 8.0: Numbers
    • 9.0: Loops
      • 9.0a: Loop Challenges
      • 9.0b: Challenge Answers
    • 10.0: Classes and Objects
    • 11.0: Properties
    • 12.0: Useful-Links
Powered by GitBook
On this page
  • Project Set up
  • Description
  • Sample Code
  • Analysis
  • Challenge
  1. Part 3: C# Fundamentals

1.0: Hello World

PreviousPart 3: C# FundamentalsNext2.0: Variables

Last updated 6 years ago

In this module we'll be writing our first C# program using the console window, and we'll learn the structure of a C# file.

Project Set up

  1. If you haven't already, create a folder where you would like to keep your work for this class. It's best to make this in your C drive.

  2. Open Visual Studio Community 2017.

  3. Go to File > New Project.

  4. Select Windows Classic Desktop on the left sidebar and choose Console App (.NET Framework).

  5. Browse to the folder you created in Step 1.

  6. Name the Project 01_HelloWorld and name the Solution CSharpPreWork.

  7. Check Create directory and Create new Git repository.

  8. This will create a folder named CSharpPreWork, a solution (.sln file) inside that folder named CSharpPreWork, and a project called 01_HelloWorld. Note that our image doesn't have the '01' prefix that we are asking you to add.

  9. As you add more projects to the solution, there will be more folders added.

    • Note: When you need to re-open this solution, or other solutions you create, you'll open Visual Studio, go to File > Open > Project/Solution and browse to the [SolutionName].sln file.

  10. The Program.cs file in HelloWorld is where you will be writing your first program.

Description

For our first program we will be printing Hello World to the console using the method Console.WriteLine()

Sample Code

  1. Open Program.cs and type this code in the Main method:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
        Console.ReadLine();
    }
}
  1. Press Start at top of the window to run the program.

  2. You should see a console window pop up with the following:

Analysis

We have made our first C# program. Here's what we did: 1. We called a WriteLine() method built into the .NET framework that prints the line to the console window. More on methods later. 2. We called a ReadLine() method that is also built into the framework that reads the current program line and also pauses the program until the user presses enter. The program doesn't necessarily need the ReadLine() method here. It's just easier for you, the developer, because it will pause the program and keep the printed line up for you to see.

  1. Try it without the ReadLine() method. You can also use CTRL F5 instead of Start to run the program, this removes the need for Console.ReadLine()

Challenge

To get a little bit of practice running the console and writing in C#, create a conversation between two people within your console application, and underneath the "Hello World" message. It should look something like this:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");

        Console.WriteLine("Hey there how are you doing?");
        Console.WriteLine("I'm good, how are you?");
        Console.WriteLine("I'm fantastic!  I'm learning how to code!");
        Console.WriteLine("Wow! That's great!");
        Console.ReadLine();
    }
}

Folder Structure
Hello World
Solution
New Project
New Folder