KodeBLOG

Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

C# Basics

Introduction

C# Basics: This tutorial sets the foundation for learning C# programming. It covers the basic topics that every C# programmer should know.

Topics to be covered

  • C# Syntax
  • Keywords
  • Variables
  • Operators
    • Arithmetic
    • Comparison
    • Logical
  • Comments
  • Namespaces
  • C# is a compiled language
  • Errors, warnings and messages

C# Syntax

The C# syntax is inspired by C and C++. If you are familiar with languages such as JAVA and PHP, then it will be easy for you to learn C#. If you are not familiar with any of the languages mentioned above, this tutorial will get you up to speed.

C# Syntax Rules

  • All statements in C# end with semi colons.
  • All variables in C# must be defined before using them.
  • The class and method bodies must be enclosed in opening and closing braces

The code shown below is for a simple console application that prints “Hello World!” On the screen

using System;

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

HERE,

  • using System; imports the System namespace. The semi colon is used to end the C# statement
  • class Program{...} declares a class Program. { opens the body of the class and } closes the body of the class.
  • static void Main(string[] args) is the method that is executed when the program runs.

The above code produces the output

Prints Hello World! in the console window

Keywords

A keyword is a reserved word that has special meaning to the compiler. An example of a keyword C# is;

using

using is used to import a namespace.

Keywords cannot be used as identifiers. Other examples of C# keywords include

int
decimal
namespace
class

Variables

A variable is a memory location used to store values. The stored values can change at any time.

C# Variables

In C#, all variables must be declared before using them. Variable names are also case sensitive

The following code declares two variables

int i = 0;
string message = "Welcome";

HERE,

int i = 0; defines a variable i of data type int. The variable i is initialized to a value of 0 value.

string message = "Welcome";

defines a variable message of string data type. The variable message is initialized to a value of Welcome value.

Variable Naming Rules

  • Variable names cannot start with a number
  • Variable names should not contain spaces
  • A variable name should not be a keyword. If you want to use a key work as a variable name then you must append @ symbol at the beginning of the variable name
  • A data type must be specified for all declared variables

Operators

In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators are used to manipulate data. For example, the plus (+) operator is used to sum up numbers.

C# has three major categories of operators

  • Arithmetic - used to manipulate numeric data
    • Addition (+) - used to add numbers
    • Subtraction (-) - used to subtract numbers
    • Multiplication (*) - used to multiply numbers
    • Division (/) - used to divide numbers
  • Comparison - used to compare values
    • Equal to (==) - compares two values and returns true if they are equal. False is returned if they are not equal.
    • Not Equal To (!=) - compares two values and returns true if they are not equal. False is returned if they are equal
    • Greater Than (>) - used to compare two numbers. Returns true if the number on the left hand side is larger than the number on the right hand side.
    • Greater Than Or Equal To(>=) - used to compare two numbers. Returns true if the number on the left hand side is either greater than or equal to the number on the right hand side.
    • Less Than - used to compare two numbers. Returns true if the number on the left hand side is less than the number on the right hand side.
    • Less Than Or Equal To(<=) - used to compare two numbers. Returns true if the number on the left hand side is either less than or equal to the number on the right hand side.
  • Logical - used when working with values that evaluate to either true or false
    • AND - used when evaluating more than one condition. If both conditions evaluate to true, AND returns true. If any of the conditions is false, AND returns false
    • OR - used when evaluating more than one condition. If any of the conditions evaluates to true, OR returns true. If all of the conditions are false, OR returns false.

Comments

These are statements that will be ignored by the compiler. They help developers document the source codes. C# supports single and multiple line comments as shown below.

  • Single comments start with // two forward slashes
  • Multiple line comments start with /* a forward slash followed by an asterisk. The closing tag for multiple line comments is */ an asterisk and a forward slash

    //single line comment
    /* 
    this is a multi-line 
    comment  */
    

Namespaces

Namespaces are used to group similar classes together. Let’s suppose that you have developed a database library, you can create namespaces for MS SQL Server, MySQL, Oracle etc. this is very helpful in avoiding identifier name crashes. The same identifiers can be used in different classes without any conflicts.

How to Define a Namespace in C#

A namespace is defined using the keyword namespace

The following shows an example

namespace MySQL {
    class Connect(){

    }
} 

//importing a namespace 
using MySQL;

Summary

  • The syntax for C# is inspired by C and C++
  • Identifiers are names used to describe classes, methods, variables etc.
  • Keywords are words that have special meaning in C#
  • Variables are like containers, they are used to store data.
  • The major categories of operators in C# are; arithmetic, comparison and logical.
  • Namespace are used to group similar classes and methods together

Tutorial History

Tutorial version 1: Date Published 2015-08-21