C# Lesson 1

Learn how to declare and use variables in the very first lesson from our series on learning C# for beginners.

The first lesson introduces 3 of the main c# variables. String, int, and double. Below you'll find the lesson video, notes, and the lesson's task.
Afterwards you can continue to the next lesson!
We also have the source code on GitHub.


using System;

namespace Lesson1
{
    class Variables
    {
        static void Main(string[] args)
        {

            string myString;
            myString = "This is a string value.";

            string mySecondString;
            mySecondString = "1000";

            int myInt;
            myInt = 1000;

            int mySecondInt = 2;

            Console.WriteLine(myString);
            Console.WriteLine("My second string: " + mySecondString);
            Console.WriteLine("My int: " + myInt);
            Console.WriteLine("My second int: " + mySecondInt);

            double myDouble = 3.14;
            Console.WriteLine("My Double: " + myDouble);

            Console.WriteLine("Goodbye");

        }
    }
}

/*
* TASK:
* 
* In this program you will need to make the following variables with the given data types:
*   myName      -   Holds a string value
*   myAge       -   Holds an int value
*   someDecimal -   Holds a decimal value between 0 and 1 ex: .223
*   
* Use string concatination and Console.WriteLine to print:
*   Hello, My name is 
*   I am 
*   A number between 0 and 1 is 
*   
* Where the variable contents are displayed between the <>
*/


// This lets us use Console.WriteLine in our program.
using System;

// We will learn about namespaces in a different lesson.
namespace Lesson1
{
    // We will learn about classes in a different lesson.
    class VariablesTask
    {

        // For now, just know that this is the entry point to our program.
        // The code between the {} is the first section of code that will run.
        static void Main(string[] args)
        {
            // This is a single line comment. It has no effect on the program.
            // It is used to write notes about what the code is doing.
            // ↓↓ Write a comment with your name and today's date under this line ↓↓


            /*
               This is a block comment.
               It can be used for multi lined comments.

               Make a NEW Block comment under this line 
               with your name and today's date on seperate lines

               ↓    ↓   ↓   ↓   ↓   ↓   ↓   ↓   ↓   ↓   ↓   ↓   ↓
             */


            // I'll start out with an example.
            // This variable is named introText and holds a string data type.
            // Use the equalsign(=) to set the variable's value.  = Is called the assignment operator
            string introText = "Variables!"; //  <-- end lines of code with a ;
            Console.WriteLine("Today I'm going to learn " + introText);


            //Make a variable named myName with the string data type.
            //Set the variable equal to your name.
            //Display the text 'Hello, My name is '


            //Make a variable named myAge with the int data type.
            //Set the variable equal to your age.
            //Display the text 'I am '


            //Make a variable named someDecimal with the decimal data type.
            //Set the variable equal to a number between 0 and 1. ex: .223
            //Display the text 'A number btween 0 and 1 is '


        } // This curly brace ends Main 
    } // This curly brace ends VariablesTask
} // This curly brace ends Lesson1Task

Recommended posts

We have similar articles. Keep reading!

C# Lesson 2

Learn how to read input from the user in the second lesson from our series on learning C# for beginners.

C# Project 2

Test out your knowledge on basic C# programming with a challenging project making a hangman game. Covers lessons 7-12 from the beginner series.

C# Lesson 7

You're familiar with "while" loops in C#, but but it's time to learn about "for" loops. Let's get loopy in the 7th lesson in our series about learning C#.

C# Project 1

Test out your knowledge on basic C# programming with a challenging project doing conversions. Covers lessons 1-6 from the beginner series.

Comments

Log in or sign up to leave a comment.