C#Tingkat: Menengah7 min
Generics Type Safety & Koleksi Data (List<T>, Dictionary<K,V>)
Hilangkan overhead proses boxing/unboxing dan tingkatkan keamanan kompilasi dengan C# Generics: List<T>, Dictionary<TKey, TValue>, dan generic constraints (where T : class).
Keunggulan Generics di C#
Pratinjau TerbukaGenerics memungkinkan penulisan kelas atau method yang menunda penentuan satu atau lebih tipe data sampai kelas tersebut diinstansiasi oleh kode klien.
Contoh Kode Sintaks
using System;
using System.Collections.Generic;
// Kelas Generic dengan constraint 'where T : class'
public class GudangMemori<T> where T : class
{
private readonly Dictionary<string, T> _items = new();
public void Simpan(string key, T data) => _items[key] = data;
public T? Ambil(string key) => _items.TryGetValue(key, out var val) ? val : null;
}
public record ProfilUser(string Nama, string Role);
var cache = new GudangMemori<ProfilUser>();
cache.Simpan("u1", new ProfilUser("Azzam", "Architect"));
var hasil = cache.Ambil("u1");
Console.WriteLine($"Nama: {hasil?.Nama} (Role: {hasil?.Role})");Live Compiler Runner
Akses Kursus Wajib Login
Buka Akses Penuh Silabus & Praktikum
Masuk dengan akun GitHub, Google, atau Akses Cepat Developer untuk membuka seluruh materi mendalam, menjalankan compiler interaktif, mengerjakan kuis, dan menyimpan progres belajar Anda.
145+ Modul Lengkap•Tersimpan Otomatis•Sertifikat Kelulusan Resmi