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.
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
.NET 7 Update: Raw String Literals in 10 Minutes or Less - Raw string literals are here in C# 11. And while they look wild, they actually have a good reason for existing. Let's see them in action in this 10-Minute Training video. Author: IAmTimCorey
- Length: 0:10:16
Building a Suggestion Site App Course - Welcome to the course on building a Suggestion Site App. In this course, we are going to design, build, and test a production-ready application using C#. This course is sponsored by MongoDB, who made it possible to get the videos from this course for free here on YouTube. Author:
IAmTimCorey
- Length: 0:20:39
C# Resume and Portfolio Review - Applying for a job can be tough. Often, you get rejected without finding out why. As an employer who has to look at hundreds of resumes every time I hire anyone, there are a lot of simple things that you can do to make your resume stand out and improve your chances with an employer. Author:
IAmTimCorey
- Length: 1:11:13
C# Tutorial - Full Course for Beginners - This course will give you a full introduction into all of the core concepts in C# (aka C Sharp). Follow along with the course and you'll be a C# programmer in no time! Author: FreeCodeCamp.org
- Length: 4:31:08
Correct String Initialization in C# - How do you properly initialize a string? Should it be null, string.Empty, empty quotes, or default? There is quite a bit of confusion on this topic, so let's look at the different options. Author: IAmTimCorey
- Length: 0:08:39
How to connect C# to SQL (the easy way) - Learn how to connect to SQL from C# the easy way, using a micro-ORM called Dapper (built by the Stack Overflow team). See how easy it is to get data in and out of SQL without complicated generated code or messy DataTables. Author: IAmTimCorey
- Length: 1:20:39
NET 7 Update: List Patterns in 10 Minutes or Less - In C# 11, we now have pattern matching on Lists, and it is awesome. Let's see it in action in this 10-Minute Training video. Author: Tim Corey
- Length: 0:09:59
$
- string interpolation (C# reference) - This article covers how to use string interpolation in C# code. Author: MSDN
C# console app template generates top-level statements - This article explains the changes made to the C# templates in Visual Studio starting with .NET 6. Author: MSDN
C# formatting options - This article covers the formatting options available to C# code. Author: MSDN
Composite formatting - This article covers how to implement composite formatting in C# code. Author: MSDN
Overview: How to format numbers, dates, enums, and other types in .NET - This article covers the process of converting an instance of a class or structure, or an enumeration value, to a string representation.. Author: MSDN
Serialization in .NET - This article covers the process of converting the state of an object into a form that can be persisted or transported. The complement of serialization is deserialization, which converts a stream into an object. Together, these processes allow data to be stored and transferred. Author: MSDN
The History of C# - This article provides a history of each major release of the C# language. Author: MSDN
Microsoft Application Architecture Guide, 2nd Edition - This link will take you to a page on the Microsoft.com Web site which has a Download button you can click on to download the Microsoft Application Architecture Guide, 2nd Edition. This guide, updated in 2009, provides the reader with in-depth explanations of the recommended principles for designing and developing modern-day application architectures based on the .NET framework.
Windows Dev Center - The Windows Developer Center is a good resource for learning about all things Microsoft when it comes to their application development technologies.
Microsoft Developer Network - The Microsoft Developer Network is a good resource for documentation on Microsoft APIs and language references.
C# Programming Guide - This Microsoft Developer Network (MSDN) page will help guide you through the C# programming process.
C# Reference - This Microsoft Developer Network (MSDN) page will help guide you through the C# programming language.
C# Helper - Rod Stephens is a writing of numerous programming books. This is a link to his Web site where you will find numerous C# coding examples.
C# Keywords - This Microsoft Developer Network (MSDN) page lists all of the keywords that are part of the C# programming language.
.NET Framework Class Library - This link will take you to the MSDN home page for the .NET framework class library.
.NET Framework Development Guide - This link will take you to an MSDN page which explains how to create, configure, debug, secure, and deploy your .NET Framework apps. The section also provides information about technology areas such as dynamic programming, interoperability, extensibility, memory management, and threading.
Understanding the stack and heap in .NET Programming - This is a good YouTube video which describes how reference variables and the instances they point to are allocated on the stack and the heap of managed memory.
.NET Framework 4.6 and 4.5 Design Guidelines - This link will take you to an MSDN page which divides the Microsoft .NET framework design guidlines into several sections: Naming Guidelines, Type Design Guidelines, Designing for Extensibility, Design Guidelines for Exceptions, Usage Guidelines, and Common Design Patterns.
Events (C# Programming Guide) - This page is an overview of Events in C# and has some good links to additional resources for learning more abour Events.
How to Create Classes and Objects in Visual C# - This step-by-step article shows you how to create a new class in C# to represent a baseball team. You will define fields, methods, and properties for the class. You will then create an object of this class type and make use of its methods and properties.
Constructor Design - This MSDN page provides valuable information and advice on how to best design constructors in your C# projects.
Enum
- This link will take you to the C# reference page for the enum keyword on the MSDN Web site.
ThreadPool
Class - This link will take you to the C# reference page for the ThreadPool class on the MSDN Web site.
Task
Class - This link will take you to the C# reference page for the Task class on the MSDN Web site.
An Introduction to C# - This link will take you to a C# technical article which explains the problem space generics address and how to implement them into your code.
Entity Framework - This CIS Web page provides a tutorial as well as numerous learning resources related to the Entity Framework.
101 LINQ Samples - A really nice repository of code samples on the Microsoft Developer Network Web site that you can download and use to gain a better understanding of LINQ and how to use it with SQL, DataSets, and XML.
The Repository Pattern - Using a repository pattern when designing the architecture of your application provides an intermediary layer that hides the implementation of data access from your Business Logic layer - also known as dependency injection.
The Liskov Substitution Principle - The Lsikov Substitution Principle is one of the SOLID design principles used in software architecture design and is a principle that all app developers should be aware when creating custom classes for their software projects.
Guidelines for Universal Windows Platform (UWP) Apps - Use this index of guidelines to help you design a great Universal Windows Platform (UWP) app.
What is Serialization? - This link will take you to a StackOverflow forum which includes a well-written concise explanation of what serialization is and it provides examples of when you would use it.
C# Developer Code Samples - This link will take you to the MSDN page which offers a plethora of C# developer code samples.
C# Corner - C# Corner is a developer forum where C# developers can stop on top of the latest C# topics.
C# Corner (Visual Studio Magazine) - Visual Studio's C# Corner page offers up-to-date information about cutting-edge technologies at the forefront of the .NET framework development arena.
Nullable Types - This link will take you to the MSDN page which defines and explains nullable types.
Interpolated Strings - This MSDN document explains how to use the new C# 6 interpolated strings feature.
⇑ Table of Contents