C# Programming Notes (Detailed)
1. Introduction to C#
- C# is an object-oriented programming language developed by
Microsoft.
- It runs on the .NET framework.
- C# is used to develop web apps, desktop apps, games, and more.
2. Basic Syntax
- Every C# program must contain a Main method:
static void Main(string[] args) { ... }
- Statements end with a semicolon `;`
- Curly braces `{}` define blocks of code.
3. Data Types
- int, float, double, char, string, bool
- Example: int x = 10;
4. Variables and Constants
- Variables store data. Must declare before use.
- Constants: `const int MAX = 100;`
5. Operators
- Arithmetic: + - * / %
- Comparison: == != > < >= <=
- Logical: && || !
6. Control Statements
- if, else if, else
- switch-case
- Loops: for, while, do-while, foreach
7. Arrays and Strings
- Arrays: int[] arr = new int[5];
- Strings: string name = "Hello";
8. Functions (Methods)
- Methods allow code reuse.
- Example:
int Add(int a, int b) {
return a + b;
}
9. Object-Oriented Programming
- Class: Blueprint for objects.
- Object: Instance of class.
- Concepts: Inheritance, Polymorphism, Encapsulation, Abstraction
10. Constructors and Destructors
- Constructor: special method called when object is created.
- Destructor: used for cleanup (rarely used in modern C#).
11. Exception Handling
- try { ... } catch(Exception e) { ... } finally { ... }
12. File Handling
- Use System.IO namespace
- File.ReadAllText(), File.WriteAllText()
13. Collections
- List<T>, Dictionary<K,V>, Stack<T>, Queue<T>
- Example: List<int> nums = new List<int>();
14. LINQ (Language Integrated Query)
- Query data using SQL-like syntax.
- Example: var result = from n in nums where n > 10 select n;
15. Delegates and Events
- Delegate: reference type that refers to methods.
- Events: used in GUI apps for handling actions.
16. Namespaces
- Used to organize code.
- Example: using System;
17. Access Modifiers
- public, private, protected, internal
18. Static vs Instance
- Static: shared by all instances.
- Instance: belongs to object.
19. Interfaces and Abstract Classes
- Interface: defines method signature only.
- Abstract class: can have both abstract and concrete methods.
20. Useful Tools
- IDE: Visual Studio, VS Code
- .NET CLI: dotnet new, dotnet run