Introduction to C#

C# (pronounced C sharp) is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed around 2000 by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2018). C# is one of the programming languages designed for the Common Language Infrastructure (CLI).

C# was designed by Anders Hejlsberg, and its development team is currently led by Mads Torgersen. The most recent version is C# 10, which was released on November 8, 2021.

Keywords

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword.

The first table in this topic lists keywords that are reserved identifiers in any part of a C# program. The second table in this topic lists the contextual keywords in C#. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. Generally, as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.

C# Keywords
abstract as base bool
break byte case catch
char checked class const
continue decimal default delegate
do double else enum
event explicit extern false
finally fixed float for
foreach goto if implicit
in int interface internal
is lock long namespace
new null object operator
out override params private
protected public readonly ref
return sbyte sealed short
sizeof stackalloc static string
struct switch this throw
true try typeof uint
ulong unchecked unsafe ushort
using using static virtual void
volatile while    

Contextual Keywords
add alias ascending
async await by
descending dynamic equals
from get global
group into join
let nameof on
orderby partial (type) partial (method)
remove select set
value var when (filter condition)
where (generic type constraint) where (query clause) yield

Arithmetic Operators

Just like mathematics, C# has several arithmetic operators that you can use for computation. They are:

C# Arithmetic Operators
Operator Meaning Example Outcome
+ Add x, y  = 3, 4
z = x + y
z is equal to 7
- Subtract x, y = 3, 4
z = x - y;
z is equal to -1
* Multiply x, y = 3, 4
z = x * y
z is equal to 12
/ Divide x, y = 6, 2
z = x / y
z is equal to 3
% Modulus x, y = 5, 3
z = x % y
z is equal to 2
** Exponent x, y = 2, 3
z = x ** y
z is equal to 8
// Floor Division x, y = 20, 12
z = x // y
z is equal to 1

There are a couple of these operators that need some explanation. First the modulus operator will return the remainder for a division

  • 10 % 3 results in 1 be case 10 / 3 = 3 with a remainder of 1
  • 12 % 7 results in 5 Because 12 / 7 = 1 with a remainder of 5

The floor division operator gives a division of a number where the numbers after the decimal point are truncated.

  • 15 // 4 is 3 because 4 goes into 15 three times; the remainder is truncated.

In other high level languages whole number division gives you a whole number result. This is not the case in Python. If you divide two whole numbers like

Escape Characters

The following table is a complete list of escape characters that can be used to format strings.

C# Escape Characters
Character Description
 \a  Bell 
 \b  Backspace
 \Cx  Control-x
 \e  Escape
 \f  Formfeed
 \M-\C-x  Meta-Control-x
 \n  Newline
\r  Carriage Return
\s  Space
\t  Tab
\v  Vertical Tab
\x  Character x 
\xnn  Hexadecimal notation. Where n is in the range of 0-9, a-f or A-F

Bitwise Operators

Bitwise operators in C#
Operator Meaning Example
& Bitwise AND x& y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 40 (0010 1000)

C# Links of Interest

Table of Contents