Iyi ngingo irahagije kubantu bose bashaka kwerekana ubuhanga bwabo .NET!
Mwaramutse .Net Abashinzwe iterambere,
Niba uzi kandi wunvise ingero za kode, birashoboka ko ukora neza muri .NET.
Mugihe wandika logique muri C # , abitezimbere bakunze guhura na IE Umubare <T> na ICollection <T>. Bashobora gusa nkaho ariko bakora intego zitandukanye mugutezimbere software.
Nibyiza kugira icyegeranyo cyoroheje gifite umutekano kurushaho, nkuko gukoresha amakuru bitemewe. Icyegeranyo icyo aricyo cyose niba ushaka gukumira amakuru yimikorere, irasabwa gukoresha IE Umubare <T>
IEnumerable<int> numbers = new List<int> { 1, 2, 3 }; foreach (var number in numbers) { Console.WriteLine(number); // Outputs: 1 2 3 }
ICollection <T> yagura IE Umubare <T> yemerera guhinduka hifashishijwe uburyo bwo guhindura. Isohora rigizwe nuburyo bwo kongeramo, gukuraho no kugenzura ibarwa mu cyegeranyo.
ICollection<int> data = new List<int> { 12,43,556}; numbers.Add(4); Console.WriteLine(data.Count); // Outputs: 4
Birasabwa kuri ICollection <T> mugihe uburyo bwo guhindura busabwa mugukusanya hamwe na IE Umubare <T> kubikorwa byoroshye byo gusoma.
public void ConfigureServices(IServiceCollection services) { // Register a transient service services.AddTransient<IMyService, MyService>(); }
public class MyController : Controller { private readonly IMyService _myService; public MyController(IMyService myService) { _myService = myService; } public IActionResult Index() { var data = _myService.GetData(); return View(data); } }
Kode irashobora kubungabungwa kandi ikageragezwa mugihe uwatezimbere yanze kurema serivisi.
public void UpdateValue(ref int number) { number += 10; } int myNumber = 5; UpdateValue(ref myNumber); Console.WriteLine(myNumber); // Outputs: 15
public void GetValues(out int value1, out int value2) { value1 = 10; value2 = 20; } GetValues(out int a, out int b); Console.WriteLine(a); // Outputs: 10 Console.WriteLine(b); // Outputs: 20
ref isanzwe ikoreshwa mugihe uburyo bukeneye guhindura impinduka zihari, mugihe hanze ikoreshwa mugihe uburyo bukeneye gusubiza indangagaciro nyinshi cyangwa gutangiza indangagaciro zitatanzwe numuhamagaye.
public async Task<string> FetchDataAsync() { await Task.Delay(1000); // Simulates an asynchronous operation return "Data fetched"; }
public async Task ShowDataAsync() { string data = await FetchDataAsync(); Console.WriteLine(data); }
Niba ushaka kunoza imikorere ya progaramu yawe koresha async hanyuma utegereze neza kandi icyarimwe ukomeze gusaba UI kwitabira.
try { int result = 10 / 0; // This will throw a DivideByZeroException } catch (DivideByZeroException ex) { Console.WriteLine("An error occurred: " + ex.Message); }
public void Configure(IApplicationBuilder app) { app.UseExceptionHandler("/Home/Error"); }
Kwisi yose ikora ibikoresho byo hagati bitanga ahantu hamwe kugirango bakemure amakosa yose yo gusaba hamwe nibidasanzwe. Iremeza gusaba buri gihe gukemura ibisubizo.
{ "ConnectionStrings": { "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" }, "Logging": { "LogLevel": { "Default": "Warning" } } }
public class MyService { private readonly string _connectionString; public MyService(IConfiguration configuration) { _connectionString = configuration.GetConnectionString("DefaultConnection"); } }
Idosiye ya porogaramu.json itanga uburyo bworoshye bwo kubona igenamiterere, ikeneye gukemurwa neza.
Thread thread = new Thread(() => { Console.WriteLine("Running on a new thread"); }); thread.Start();
Task.Run(() => { Console.WriteLine("Running asynchronously"); });
Inshingano zorohereza imiyoborere yimikorere idahwitse kandi akenshi niyo ihitamo guhitamo C # iterambere.