C# Constants

Constants

If you don’t want others (or yourself) to overwrite existing values, you can add the const keyword in front of the variable type.

This will declare the variable as “constant”, which means unchangeable and read-only:

Example

const int myNum = 15;
myNum = 20; // error

Continue reading C# Constants

C# Variables

C# Variables

Variables are containers for storing data values.

In C#, there are different types of variables (defined with different keywords), for example:

  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • double – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
  • string – stores text, such as “Hello World”. String values are surrounded by double quotes
  • bool – stores values with two states: true or false

Continue reading C# Variables

C# Comments

C# Comments

Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line Comments

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by C# (will not be executed). Continue reading C# Comments

C# Output

C# Output

To output values or print text in C#, you can use the WriteLine() method:

Example

Console.WriteLine("Hello World!");

 

Continue reading C# Output

C# Syntax

C# Syntax

In the previous chapter, we created a C# file called Program.cs, and we used the following code to print “Hello World” to the screen:

Program.cs

using System;

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

Result:

Hello World!

 

Continue reading C# Syntax