paint-brush
7 Ibyingenzi Byingenzi Kugerageza Ubuhanga bwawe .NET  na@ssukhpinder
112 gusoma

7 Ibyingenzi Byingenzi Kugerageza Ubuhanga bwawe .NET

na Sukhpinder Singh7m2024/09/30
Read on Terminal Reader

Birebire cyane; Gusoma

Iyi ngingo ikubiyemo C # ibitekerezo byingenzi kubateza imbere .NET, harimo IEnumerable na ICollection, Injection Yishingikirije, async / gutegereza, ref va hanze ibipimo, gukemura bidasanzwe, no gutandukanya Task hamwe ninsanganyamatsiko mugutegura gahunda hamwe. Itanga ingero zifatika zo kugerageza no kurushaho gusobanukirwa.
featured image - 7 Ibyingenzi Byingenzi Kugerageza Ubuhanga bwawe .NET
Sukhpinder Singh HackerNoon profile picture
0-item
1-item
2-item
3-item
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.

1. Itandukaniro hagati ya IE Itabarika <T> na ICollection <T>

Mugihe wandika logique muri C # , abitezimbere bakunze guhura na IE Umubare <T> na ICollection <T>. Bashobora gusa nkaho ariko bakora intego zitandukanye mugutezimbere software.


  • IEBitabara <T> ikora nkibanze shingiro kubikusanyirizo rusange. Ifasha gusubiramo hejuru yikusanyirizo ryubwoko bwasobanuwe.


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.

2. Uruhare rwo gutera inshinge muri .NET Core

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 #
  • Iyandikishe serivisi muburyo bwa ConfigureServices uburyo bwa dosiye yawe ya Startup.cs. Aha niho usobanura serivisi zizaboneka mugutera inshinge.
 public void ConfigureServices(IServiceCollection services) { // Register a transient service services.AddTransient<IMyService, MyService>(); }


  • Injira serivisi mumasomo yawe ukoresheje inshinge zubaka. Ibi byemeza ko amasomo yawe yakira ibyo ashingiyeho adakeneye kubikora muburyo butaziguye.
 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.

3. Itandukaniro riri hagati ya ref na hanze Ibipimo

Muri C #, ref no hanze bikoreshwa mugutambutsa ibipimo byerekanwa, ariko bifite ibiranga bitandukanye.
  • ibipimo bya ref bisaba ko impinduka yatangizwa mbere yuko inyuzwa muburyo. Uburyo burashobora noneho guhindura agaciro ka variable.
 public void UpdateValue(ref int number) { number += 10; } int myNumber = 5; UpdateValue(ref myNumber); Console.WriteLine(myNumber); // Outputs: 15


  • hanze ibipimo ntibisaba gutangira mbere yo gutambuka. Uburyo bugomba guha agaciro ibipimo byo hanze mbere yuko bigaruka.
 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.

4. Async no Gutegereza: Kunoza imikorere yo gusaba

Muri .NET, gahunda idahwitse ningirakamaro mu kwandika porogaramu ikora neza munsi yumutwaro. Async no gutegereza ijambo ryibanze byoroshe gukorana nibikorwa bidahuje.
  • uburyo bwa async bugufasha gukora imirimo idahwitse. Shyira akamenyetso hamwe nijambo ryibanze rya async kugirango ushoboze gukoresha gutegereza imbere.
 public async Task<string> FetchDataAsync() { await Task.Delay(1000); // Simulates an asynchronous operation return "Data fetched"; }


  • gutegereza bifasha gusaba UI mukudahagarika insanganyamatsiko nkuru.
 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.

5. Gukemura ibibazo muri .NET Core Porogaramu

Gukemura ibitemewe neza ningirakamaro mugukomeza imbaraga kandi zikoresha abakoresha. .NET Core itanga uburyo butandukanye bwo gukemura ibibazo.
  • Gukemura ibibazo bidasanzwe: Koresha kugerageza-gufata kugirango ufate kandi ukemure ibintu bidasanzwe.
 try { int result = 10 / 0; // This will throw a DivideByZeroException } catch (DivideByZeroException ex) { Console.WriteLine("An error occurred: " + ex.Message); }


  • Gukemura ibibazo bidasanzwe kwisi muri Core irashobora gucungwa ukoresheje ibikoresho byo hagati. Fata ubwoko bwose bwibidasanzwe ahantu hamwe bifasha abitezimbere gucunga ubutumwa bworoheje / ubutumwa bwibibazo bisanzwe kubakoresha.
 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.

6. Uruhare rwa porogaramu.json Core

Idosiye ya porogaramu.json ikoreshwa mugucunga ibyangombwa bya porogaramu, nk'imirongo iboneza hamwe nizindi mfunguzo zihariye. Igenamiterere rishobora kugabanwa nkuko ibidukikije nabyo
  • Urugero rwa porogaramu.json dosiye:
 { "ConnectionStrings": { "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" }, "Logging": { "LogLevel": { "Default": "Warning" } } }


  • Kugera kubiciro byagaciro mubisabwa byawe biroroshye ukoresheje sisitemu yimiterere.
 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.

7. Gusobanukirwa Inshingano ninsanganyamatsiko muri C #

Byombi Inshingano ninsanganyamatsiko zikoreshwa mugutegura gahunda muri C #, ariko zikora intego zitandukanye.
  • Urudodo rugaragaza inzira imwe yo gusohoza kandi ni urwego rwo hasi rwubaka. Itanga igenzura ryinshi kubikorwa ariko bisaba gucunga intoki.
 Thread thread = new Thread(() => { Console.WriteLine("Running on a new thread"); }); thread.Start();


  • Inshingano itanga imikorere idasobanutse yo gushyira mubikorwa ibikorwa bya async kandi ikoreshwa hamwe na async / gutegereza.
 Task.Run(() => { Console.WriteLine("Running asynchronously"); });
Inshingano zorohereza imiyoborere yimikorere idahwitse kandi akenshi niyo ihitamo guhitamo C # iterambere.


Kugera kuri Cyuzuye

None wabigenze ute?

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!
바카라사이트 바카라사이트 온라인바카라