Post

C# - Introduction

Official C# Logo

C#

1
[![C#](https://img.shields.io/badge/C%23-9.0-blue.svg)](https://docs.microsoft.com/en-us/dotnet/csharp/)

C# is a general-purpose, multi-paradigm programming language developed by Microsoft.

The C# programming language allows you to build many types of applications, like:

  • Business applications to capture, analyze, and process data
  • Dynamic web applications that can be accessed from a web browser
  • Games, both 2D and 3D
  • Financial and scientific applications
  • Cloud-based applications
  • Mobile applications

Printing “Hello, World!” in C#

To print “Hello, World!” in C#, you can use the following code:

1
Console.WriteLine("Hello, World!");

This code will output Hello, World! to the console.

Diferences between Console.WriteLine and Console.Write

  • Console.WriteLine() prints a message to the output console. At the end of the line, it adds a line feed similar to pressing Enter or Return to create a new line.

  • To print to the output console, but without adding a line feed at the end, you use the second technique, Console.Write(). So, the next call to Console.Write() prints another message to the same line.

C# is:

  • Strongly typed
  • Object-oriented
  • Case-sensitive Hello != hello
  • Quotes-sensitive '' != ""

Literal Values in C#

In C#, you can use literal values to represent fixed values in your code. A literal value is a constant value that never changes. For example:

  • 123 is an integer literal = int
  • 'a' is a character literal = char
  • "Hello, World!" is a string literal = string
  • Floating-point literals = float, double, decimal
1
2
3
4
5
Float Type    Precision
----------------------------
float         ~6-9 digits e.g. 3.14F
double        ~15-17 digits e.g. 4.14159
decimal        28-29 digits e.g. 5.78349m

Here, precision reflects the number of digits past the decimal that are accurate.

  • F is used to denote a float literal.
  • m is used to denote a decimal literal.
  • double literals are the default floating-point type in C#.

Declaring Variables in C#

To create a new variable, you must first declare the data type of the variable, and then give it a name. For example:

1
int myNumber;

Variable naming rules and conventions

A software developer once famously said “The hardest part of software development is naming things.”

Not only does the name of a variable have to follow certain syntax rules, it should also be used to make the code more human-readable and understandable.

That’s a lot to ask of one line of code!

Here’s a few important considerations about variable names:

  • They can contain alphanumeric characters and the underscore character. Special characters like the hash symbol # (also known as the number symbol or pound symbol) or dollar symbol $ are not allowed.
  • They must begin with an alphabetical letter or an underscore, not a number.
  • They are case-sensitive, meaning that string Value; and string value; are two different variables.
  • They must not be a C# keyword. For example, you cannot use the following variable declarations: decimal decimal; or string string;.

Assigning Values to Variables in C#

  • To assign a value to a variable, you use the assignment operator =. For example:
1
2
int myNumber;
myNumber = 123;
  • You can also declare and assign a value to a variable in a single line.
  • For example:
1
int myNumber = 123;
  • This is called variable initialization.

Implicit and Explicit Variable Declaration

  • In C#, you can declare a variable using the var keyword.
  • This is called an implicit variable declaration.
  • For example:
1
var myNumber = 123;
  • The C# compiler will automatically determine the data type of the variable based on the value assigned to it.

  • You can also declare a variable using an explicit variable declaration. For example:

1
int myNumber = 123;

In this case, you explicitly specify the data type of the variable.

Variables using the var keyword must be initialized

  • It’s important to understand that the var keyword is dependent on the value you use to initialize the variable.
  • If you try to use the var keyword without initializing the variable, you’ll receive an error when you attempt to compile your code.
1
var myNumber; // Error: Implicitly-typed variables must be initialized
This post is licensed under CC BY 4.0 by the author.