Introduction

C# (pronounced "C sharp") is a programming language developed by Microsoft, designed for building a wide range of applications that run on the .NET framework. The syntax of C# is similar to other C-style languages like C, C++, and Java, but it also has its own unique features

Why Learn CSS?

Case Sensitivity

C# is case-sensitive, meaning that uppercase and lowercase letters are considered distinct. For example, "MyVariable" and "myvariable" are treated as different identifiers.

Comments

You can add comments to your code using // for single-line comments or /* */ for multi-line comments. Comments are ignored by the compiler and are used for documentation or notes within the code.

// This is a single-line comment

/* This is
‌ ‌ ‌ a multi-line
‌ ‌ ‌ comment */

Variables and Data Types

In C#, you declare variables with a specific data type. Common data types include int (integer), float (floating-point number), double (double-precision floating-point number), char (character), string (text), bool (Boolean), etc.

int myInt = 10;
float myFloat = 3.14f;
char myChar = 'A';
string myString = "Hello, world!";
bool myBool = true;

Control Structures

C# supports common control structures like if statements, switch statements, for loops, while loops, and do-while loops for flow control.

if (condition)
{
// code block
}
else if (anotherCondition)
{
// code block
}
else
{
// code block
}

switch (variable)
{
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
break;
}

for (int i = 0; i < 5; i++)
{
// code block
}

while (condition)
{
// code block
}

do
{
// code block
} while (condition);

Methods and Functions

You can define methods (functions) to encapsulate reusable pieces of code. Methods can have parameters and return values.

Functions in C# are essentially methods, named blocks of code that perform tasks or computations. Like methods, they handle parameters and return values, aiding in code structuring and modularity. Both methods and functions are essential for organizing and executing logic in C# programming.

// Method definition
int Add(int a, int b)
{
return a + b;
}
// Method call
int result = Add(5, 3);

Classes and Objects

In C#, classes define the structure and behavior of objects, encapsulating data and methods. They include constructors to initialize objects, destructors to clean up resources, and support inheritance and interfaces for code reuse and contract enforcement. Access modifiers like public and private control member visibility, ensuring encapsulation.

To create objects, developers use the new keyword followed by the class name. Objects access class members using dot notation, allowing data manipulation and method invocation. C# supports both value types, storing data directly, and reference types, managing memory allocation and deallocation through the .NET garbage collector. In summary, C# classes facilitate modular, reusable, and maintainable code in software development.

class MyClass
{
// Fields (variables)
public int MyField;

// Constructor
public MyClass(int initialValue)
{
MyField = initialValue;
}

// Method
public void MyMethod()
{
// code block
}
}

// Creating an object of MyClass
MyClass myObject = new MyClass(10);

// Accessing fields and methods of the object
myObject.MyField = 20;
myObject.MyMethod();