An warware: duba idan nau'in yana aiwatar da dubawa

Tabbas, ga labarin ku.

C# yana ba da zaษ“i mai ฦ™arfi, inganci, da sauฦ™in amfani don bincika ko a nau'in yana aiwatar da dubawa. Wannan yana da amfani musamman a yanayin yanayi inda nau'in bayanai na iya zama mara tabbas, kuma fahimtar ko yana aiwatar da wani ฦ™ayyadaddun mu'amala zai iya jagorantar dabaru da aiwatar da lambar. A cikin wannan labarin, mun zurfafa cikin cikakkun bayanai na yadda za a iya cimma wannan ta hanyar nuna mafita ga matsalar, bayyana lambar, da kuma nuna ษ—akunan karatu da ayyuka masu dacewa.

Dubawa Idan Nau'in Yana Aiki Interface - Magani

C # yana ba mu damar tantance idan nau'in da aka bayar yana aiwatar da wani ฦ™ayyadaddun dubawa ta amfani da hanyar IsAssignableDaga hanyar da nau'in nau'in ya samar. Anan ga mafita don kwatanta wannan fasalin.

IMyInterface na jama'a
{
}

MyClass na jama'a: IMyInterface
{
}

MyTest ajin jama'a
{
Babu Jama'a CheckIfImplementsInterface()
{
var myObj = sabon MyClass ();
nau'in var = nau'in (IMyInterface);
var doItImplement = type.IsAssignableFrom(myObj.GetType());
Console.WriteLine (yinImplement); // Sakamakon: Gaskiya
}
}

Fahimtar Code

Mun fara kashewa ta hanyar ma'anar keษ“ance mai suna IMyInterface ba tare da wata hanya ba. Na gaba, mun ayyana ajin MyClass wanda ke aiwatar da wannan keษ“ancewa. Keษ“ancewar ba ta da wata hanya ko kaddarorin, don haka ba ma buฦ™atar ayyana wani ฦ™arin abu a cikin MyClass ษ—in mu.

Sihiri yana faruwa a cikin ajin MyTest. Muna hanzarta MyClass kuma muna samun nau'in abu don IMyInterface. Sannan, muna amfani da hanyar IsAssignableFrom don bincika ko nau'in misalin yana aiwatar da aikin mu. Idan abu ya aiwatar da dubawa, yana fitar da Gaskiya; in ba haka ba, yana fitar da Karya.

Babban aikin anan shine IsAssignableDaga. Wannan hanya, na cikin tsarin System.Type, yana ฦ™ayyade ko za a iya sanya misali na wani nau'i na musamman zuwa misalin nau'in halin yanzu.

Mabuษ—in Laburaren da Ayyuka

A cikin misalinmu, muna amfani da su System namespace, wanda ke ba da mahimman azuzuwan da azuzuwan tushe masu amfani wajen haษ“aka aikace-aikace ta amfani da C #. Babban aikin anan shine Nau'in.IsAssignableDaga - aikin da ke bincika idan ana iya sanya misali na wani nau'i zuwa misalin wani nau'in.

  • Nau'in: Yana wakiltar nau'in sanarwa: nau'ikan aji, nau'ikan mu'amala, nau'ikan tsararru, nau'ikan ฦ™ima, nau'ikan ฦ™idayar, nau'in sigogi, nau'in nau'in ma'anar ma'anar, da buษ—e ko rufe nau'ikan nau'ikan jeri.
  • Nau'in.IsAssignableDaga: Yana ฦ™ayyade ko ana iya sanya misali na wani Nau'i daga misalin wani Nau'in.

Wannan fasalin duba ko nau'in yana aiwatar da hanyar sadarwa yana ba da iko mai girma akan kwararar bayanai da dabaru a cikin shirye-shiryenmu na C #. Tare da wannan fahimtar mafita da sabbin ษ—akunan karatu da ayyuka, zaku iya dacewa da mahimmancin wannan dabarun don tabbatar da cewa bayanan sun yi daidai da takamaiman kwangilolin mu'amala da ku.

Shafi posts:

Leave a Comment