Iyi ngingo irahagije kubantu bose bashaka kwerekana ubuhanga bwabo .NET!
Mwaramutse .Net Abashinzwe iterambere,
Muri iyi ngingo, Nzasesengura C # imyumvire itandukanye buri muterimbere agomba kumenya. Burigihe nigitekerezo cyiza cyo kugerageza ubumenyi bwawe kandi nashize hamwe ingero nke muriki kiganiro hamwe nibisobanuro hamwe nurugero rwa code.
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.
Birasabwa gukoresha igishushanyo mbonera cya DI mugihe ushaka kwandika bidahuye & code igeragezwa, i. Hamwe na DI, abitezimbere barashobora gucunga ibintu biterwa hagati yamasomo byoroshye.
.NET Core yubatswe muri DI, byoroshye kuyishyira mubikorwa.
Nigute umuterimbere ashobora gushyira mubikorwa DI muri C #
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.
Muri C #, ref no hanze bikoreshwa mugutambutsa ibipimo byerekanwa, ariko bifite ibiranga bitandukanye.
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.
Muri .NET, gahunda idahwitse ningirakamaro mu kwandika porogaramu ikora neza munsi yumutwaro. Async no gutegereza ijambo ryibanze byoroshe gukorana nibikorwa bidahuje.
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.
Gukemura ibitemewe neza ningirakamaro mugukomeza imbaraga kandi zikoresha abakoresha. .NET Core itanga uburyo butandukanye bwo gukemura ibibazo.
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.
Idosiye ya porogaramu.json ikoreshwa mugucunga ibyangombwa bya porogaramu, nk'imirongo iboneza hamwe nizindi mfunguzo zihariye. Igenamiterere rishobora kugabanwa nkuko ibidukikije nabyo
{ "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.
Byombi Inshingano ninsanganyamatsiko zikoreshwa mugutegura gahunda muri C #, ariko zikora intego zitandukanye.
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.
Kugera kuri C # .Net Ikiganiro Cyuzuye
Niba uzi igitekerezo ufite ikizere kandi ukumva ingero za kode, birashoboka ko uzi neza .NET.
Reka dukomeze ikiganiro kandi dufashanye gukura nkabanyamwuga .NET.
Kode nziza!