Proprietăți (Auto-implemented properties):
public string Name { get; set; } // Sintaxa clasică de proprietate
Delegates (Semnătura metodelor):
public delegate void MyDelegate(string message); // Definește un tip de referință către o metodă
Evenimente (Events):
public event EventHandler MyEvent; // Declarare
MyEvent?.Invoke(this, EventArgs.Empty); // Lansare (fire)
Expresii Lambda:
(x, y) => x + y; // Metodă anonimă scurtă
books.Find(b => b.Id == 1); // Utilizare în metode de căutare
Sintaxa de metodă (cea mai folosită):
var result = list.Where(x => x.Pret > 10)
.OrderBy(x => x.Nume)
.ThenByDescending(x => x.Data)
.Select(x => new { x.Nume, x.Pret }) // Proiecție anonimă
.ToList(); // Execuție imediată
Sintaxa de interogare (Query Syntax):
var result = from b in books
where b.Price > 10
orderby b.Title
select b;
Operatori cheie:
.SelectMany(), .Distinct(), .Union(), .Intersect(), .FirstOrDefault(), .SingleOrDefault().
Definire DbContext:
public class MyContext : DbContext {
public DbSet<Book> Books { get; set; } // Tabelă
protected override void OnConfiguring(DbContextOptionsBuilder options) {
options.UseSqlServer("ConnectionString"); // Configurare conexiune
}
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Book>().HasKey(b => b.ISBN); // Fluent API
}
}</Book></Book>
Adnotări (Data Annotations):
[Key] public int Id { get; set; }
[Required, StringLength(100)] public string Title { get; set; }
[ForeignKey(“AuthorId”)] public Author Author { get; set; }
Handlers (Metode de răspuns):
[BindProperty] public Book Book { get; set; } // Legare automată la formular
public async Task<IActionResult> OnGetAsync(int? id) { // Citire
if (id == null) return NotFound();
Book = await _context.Book.FirstOrDefaultAsync(m => m.ID == id);
return Page();
}</IActionResult>
public async Task<IActionResult> OnPostAsync() { // Salvare
if (!ModelState.IsValid) return Page();
_context.Book.Add(Book);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}</IActionResult>
Definire metodă:
public async Task<int> GetDataAsync() {
var data = await service.CallApi(); // Așteptare fără blocare
return data.Count;
}</int>
Configurare SQLite (Singleton/Lazy):
async Task Init() {
if (Database is not null) return;
Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
await Database.CreateTableAsync<TodoItem>();
}</TodoItem>
await Database.InsertAsync(item);
await Database.Table<TodoItem>().ToListAsync();
await Database.DeleteAsync(item);</TodoItem>
HttpClient & JSON (REST):
HttpClient client = new HttpClient();
var response = await client.GetAsync(“https://api.com/items”);
if (response.IsSuccessStatusCode) {
string content = await response.Content.ReadAsStringAsync();
var items = JsonSerializer.Deserialize<List<Item>>(content);
}</Item>
Dependency Injection:
builder.Services.AddScoped<IMyService, MyService>(); // Transient, Scoped, Singleton
Middleware Pipeline:
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
Cum verifici dacă o listă e goală în LINQ?
if (!list.Any())
Cum transformi un string în int în C#?
int.Parse(s) sau int.TryParse(s, out var result)
Cum forțezi EF Core să ignore o proprietate?
[NotMapped] deasupra proprietății.
Cum obții ID-ul utilizatorului logat?
User.FindFirst(ClaimTypes.NameIdentifier)?.Value
Cum navighezi la o pagină cu parametri în MAUI?
await Shell.Current.GoToAsync($”{nameof(DetailsPage)}?id={item.ID}”);