1. Framework Overview
- .NET Framework: Traditional Windows-based applications.
- .NET Core: Cross-platform development (now merged into .NET 5+).
- ASP.NET: Framework for building web applications.
- Entity Framework: ORM for database interactions.
2. Common Project Types
- Console Application: Simple command-line programs.
- Web Application: Dynamic web pages (ASP.NET MVC/Razor Pages).
- Web API: RESTful services for client-server communication.
- Class Library: Reusable code modules.
3. Basic Syntax
-
- Variables:
int number = 5; string name = "Hello"; bool isActive = true;
- Control Structures:
if (condition) { /* code */ } for (int i = 0; i < 10; i++) { /* code */ } foreach (var item in collection) { /* code */ } switch (value) { case x: /* code */ break; }
- Variables:
- Methods:
public int Add(int a, int b) { return a + b; }
4. Object-Oriented Programming
- Classes and Objects:
public class Person { public string Name { get; set; } public int Age { get; set; } } Person person = new Person { Name = "John", Age = 30 };
- Inheritance:
public class Employee : Person { public string Position { get; set; } }
- Interfaces:
public interface IAnimal { void Speak(); } public class Dog : IAnimal { public void Speak() { Console.WriteLine("Woof!"); } }
5. Common Libraries
- LINQ: Query collections.
var results = collection.Where(x => x.Property == value);
- Async/Await: Asynchronous programming.
public async Task<string> FetchDataAsync() { var result = await httpClient.GetStringAsync("url"); return result; }
- Dependency Injection: Used in ASP.NET Core.
services.AddScoped<IMyService, MyService>();
6. Error Handling
- Try-Catch:
try { // code that may throw } catch (Exception ex) { Console.WriteLine(ex.Message); }
7. Database Interaction with Entity Framework
- DbContext:
public class MyDbContext : DbContext { public DbSet<Person> People { get; set; } }
- CRUD Operations:
using (var context = new MyDbContext()) { // Create context.People.Add(new Person { Name = "Jane" }); context.SaveChanges(); // Read var people = context.People.ToList(); // Update var person = context.People.First(); person.Name = "Updated Name"; context.SaveChanges(); // Delete context.People.Remove(person); context.SaveChanges(); }
8. ASP.NET Core Routing
- Basic Route:
app.MapGet("/api/people", () => { return peopleList; });
- Route Parameters:
app.MapGet("/api/people/{id}", (int id) => { return peopleList.FirstOrDefault(p => p.Id == id); });
9. Unit Testing
- XUnit Example:
public class MyTests { [Fact] public void Test_Add() { var result = Add(2, 3); Assert.Equal(5, result); } }
10. Common Commands
- Create a New Project:
dotnet new console -n ProjectName
- Build the Project:
dotnet build
- Run the Project:
dotnet run
11. Useful Resources
- Microsoft Docs: Official documentation.
- Stack Overflow: Community Q&A.
- GitHub: Open-source projects for reference.