Introduction to C# Programming
Published: 2023-04-04 09:41:54



## What is C#.

C# is a high-level programming language developed by Microsoft as one of the languages in the .NET framework. Other languages in .NET include F# and VB.NET. .NET provides an infrastructure on which these languages can be used to develop applications that run on various platforms like Windows, Linux, and macOS.

**Architecture of .NET Applications**

.NET consists of two main components:

1. The Common Language Runtime (CLR). 
1. Class Libraries.

**CLR (Common Language Runtime)**

CLR is a runtime environment responsible for executing managed code written in any of the .NET framework languages, including C#. It offers services such as memory management, thread management, type safety, security, and exception handling. When a C# code is executed, it is not converted to machine-native language; instead, it is converted to an intermediate language known as CIL (Common Intermediate Language), which is independent of the machine it is running on. Then, CLR translates CIL to a native language, which a computer can understand, through a process called JIT (Just-In-Time) compilation. This architecture eliminates the need to convert your app into native languages for different machines.

*Class Libraries*

Class libraries are blocks of codes called classes that work together at runtime, giving the application its functionalities. A class is a container with some data called attributes and functions also called methods. A class may contain many functions, each designed to perform specific tasks in the application. C# classes are organized and contained in namespaces, which group related classes performing different tasks. For example, you can have a namespace for databases, security, graphics, etc. The C# namespaces are then organized in an assembly, which is a container of namespaces. An assembly is also referred to as a Dynamically Linked Library (DLL).

## C# Variables and Constants

A variable is a name given to a storage location in memory, while a constant is an immutable value (mostly used to create safety).

*Variable Declaration*

When declaring a variable, we start with a type followed by an identifier and finally a semicolon.

**For Example:**

    int number;

In the example above, "int" is a C# data type (integer), while "number" is the identifier. At times, you may need to give a variable a value.

**For Example:**

    int number = 1;

This is called *instantiation*. Every variable needs to be instantiated before it can be used in an application.

*C# Identifier Don'ts*  

C# language doesn't allow the following ways of naming identifiers:

- Cannot start with a number.

**For example:**

     int 4number;
     
 Instead, use: 
 
    int fournumber;
    
- Cannot include whitespace.

**For Example:**

    string first name;

Instead, use: 

    string firstname.

- Cannot be a keyword. 

**For Example:**

     string int;
 Instead, use: 
 
    string @int.
It is important to use meaningful names as you name identifiers.

**For Example:**, when naming a string,
use:

    string firstname;

instead of:

    string fn;

*Naming Conventions*

Note the casing. There are two types of naming conventions used in C#:


I.  Camel case:

**For Example:** 

    string firstName;
II. Pascal Case:

**For Example:**

    const FirstName;

*C# Primitive Types*

.NET has the following primitive types:

I. Integral numbers

II. Real numbers 

III. Characters

Integral numbers include:
-	Byte
-	Short
-	Int
-	Long

Real numbers include:
-	Float
-	Double
-	Decimal

Characters include:

-	Char
-	Bool

*Non-primitive types*

They include the following:

I. String

II.Array 

III. Enum 

IV. Class

*Uses of C#*

-	Backend development of web applications and services.
-	Building desktop applications. 
-	 Game development used together with Unity game Engine. 
-	 Development of mobile applications.