Introduction
C# is a statically-typed programming language, meaning everything will have a type at compile-time. When we assing a value to a name, it is called as defining a variable. We can define a variable either by explicitly specifying it’s type or by letting the complier infer it’s type based on the assigned value(type inference).
1
2
| int expVar = 7; // Explicitly typed
var impVar = 7; // Implicitly typed
|
Object Oriented Concepts
- C# is an object-oriented language and requires all the functions to be defined in a class.
- The class keyword is used to define a class.
- Objects (or instances) are created by using the new keyword.
1
2
3
4
5
| class Calculator
{
// ...
}
var calculator = new Calculator();
|
- A function within a class is referred to as a method.
- Each method can have zero or more parameters.
- All parameters must be explicitly typed, there is no type inference for parameters
- The return type must also be made explicit.
- Values are returned from methods using the return keyword. To allow a method to be called by code in other files, the public access modifier must be added.
1
2
3
4
5
6
7
| class Calculator
{
public int Add(int x, int y)
{
return x + y;
}
}
|
- Methods are invoked using dot (.) syntax on an instance
1
2
3
| var calculator = new Calculator();
var sum_v1 = calculator.Add(1, 2);
var sum_v2 = calculator.Add(x: 1, y: 2);
|
- Scope in C# is defined between the { and } characters.
- C# supports single line comments
//
and multi-line comments inserted betewwn /*
and */