Qhov Tseem Ceeb Qhov Sib Txawv - Generic vs Non-Generic Collection hauv C
A Generic collection yog ib chav kawm uas muab hom kev nyab xeeb yam tsis tas yuav tau txais los ntawm hom sau los ntawm lub hauv paus thiab siv cov tswv cuab tshwj xeeb. Kev sau tsis yog-generic yog chav kawm tshwj xeeb rau kev khaws cia thiab khaws cov ntaub ntawv uas muab kev txhawb nqa rau pawg, kab, npe thiab hashtables. Qhov sib txawv tseem ceeb ntawm Generic thiab Non-generic Collection hauv C yog qhov Generic Collection yog qhov zoo tshaj plaws thaum lub Non-Generic Collection tsis yog ntaus ntawv.
Dab tsi yog Generic Collection hauv C?
Cov khoom tsis muaj xws li ArrayList, Queue, Stack, thiab lwm yam.tuaj yeem khaws cov ntsiab lus ntawm cov ntaub ntawv sib txawv. Thaum tau txais cov khoom, tus programmer yuav tsum ntaus ntawv pov rau cov ntaub ntawv raug. Tsis tas li ntawd, nws tuaj yeem ua rau lub sijhawm ua haujlwm. Cov chav kawm sau generic tuaj yeem siv los daws qhov teeb meem no. Generic collections khaws cov khoom sab hauv hauv arrays ntawm lawv cov hom tiag tiag. Yog li ntawd, hom casting tsis tas yuav tsum tau. Lawv tuaj yeem siv los khaws cov ntsiab lus ntawm hom lossis hom. Qee cov chav kawm sau cov Generic yog List, Dictionary, SortedList, HashSet, Queue, Stack.
Cov GenericList muaj cov ntsiab lus ntawm hom teev tseg. Nws tuaj yeem nce cov npe raws li thaum ntxiv cov ntsiab lus. Thaum muaj nqe lus raws li hauv qab no, tag nrho cov ntsiab lus uas tuaj yeem khaws cia rau hauv daim ntawv teev npe1 yuav tsum yog cov lej, List list1 – New List ();
Phau ntawv txhais lus Generic hauv C yog ib phau ntawm cov yuam sij thiab qhov tseem ceeb. Thaum muaj ib nqe lus raws li hauv qab no, cov khoom siv phau ntawv txhais lus1 tuaj yeem khaws cov int hom keys thiab cov hlua hom nqi.
Dictionary Dictionary1=phau ntawv txhais tshiab tshiab ();
A Generic SortedList sau khaws cov khoom tseem ceeb thiab tus nqi sib tw hauv kev nce qib ntawm tus yuam sij los ntawm lub neej ntawd. Hauv qab no piv txwv khaws cov yuam sij ntawm hom int thiab tus nqi ntawm cov hlua hom.
SortedList s0=tshiab SortedList ();
Cov no yog ob peb yam piv txwv rau Kev Sau Npe hauv C. Cov kev sau no tuaj yeem khaws ntau yam txiaj ntsig ntawm hom ntaub ntawv teev tseg. Yog li ntawd, lawv ntaus ntawv heev.
Dab tsi yog Kev Sau Tsis-Generic hauv C?
Arrays tuaj yeem siv los khaws ntau lub ntsiab lus. Ib qho tsis zoo yog tias nws tuaj yeem khaws cov ntsiab lus ntawm tib hom ntaub ntawv. Muaj cov chav kawm hauv C uas tuaj yeem siv los khaws ntau qhov tseem ceeb lossis cov khoom hu ua kev sau. Cov ntawv sau pab khaws cia, hloov kho, rho tawm, tshawb nrhiav, txheeb cov khoom. Qhov loj ntawm kev sau tuaj yeem nce lossis txo qis dua dynamically.
Qee cov chav sau tsis yog cov npe yog ArrayList, SortedList, Stack, Queue thiab HashTable. Txhua chav sib sau ua ke siv IEnumerable interface. Nws pab kom rov hais dua los ntawm cov ntsiab lus ntawm cov khoom hauv kev sau los ntawm kev siv lub voj voog foreach.
ArrayList yog lwm txoj hauv kev rau ib qho array. Yog tias muaj ib qho array uas tuaj yeem khaws 10 lub ntsiab lus, nws tsis tuaj yeem khaws 20 lub ntsiab lus. Yog tias qhov array pib rau 10 lub ntsiab tab sis tsuas yog khaws 5 lub ntsiab lus, ces tus so tsis siv. Yog li ntawd, ib qho array yog tsau. Hauv ArrayList, nws tuaj yeem ntxiv lossis tshem tawm cov ntsiab lus nyob ntawm qhov ntsuas. Nws tso cai rau dynamic nco faib. Cov txheej txheem tuaj yeem siv los txheeb xyuas cov ntsiab lus hauv kev nce qib.
The HashTable yog siv los sawv cev rau ib phau ntawm tus nqi tseem ceeb. Lawv raug teeb tsa raws li hashCode ntawm tus yuam sij. Yog li ntawd, txhua yam khoom muaj tus nqi tseem ceeb khub. Tus yuam sij tuaj yeem siv los nkag mus rau ib qho tshwj xeeb hauv kev sau. Stack sawv cev rau qhov kawg hauv, thawj zaug nkag mus rau cov khoom. Lub Queue yog siv rau thawj zaug hauv kev nkag mus rau cov khoom. Cov no yog qee qhov Tsis-generic Collections txhawb los ntawm C. Cov ntawv sau no tuaj yeem khaws cov khoom sib txawv.
Dab tsi yog qhov zoo sib xws ntawm Generic thiab Non-generic Collection hauv C?
Ob qho tib si Generic thiab Non-Generic Collection tuaj yeem siv los khaws ntau yam hauv C
Dab tsi yog qhov sib txawv ntawm Kev Sau thiab Tsis-Generic hauv C?
Generic vs Non-Generic Collection in C |
|
A Generic collection yog ib chav kawm uas muab hom kev nyab xeeb yam tsis tas yuav muab los ntawm hom sau los ntawm lub hauv paus thiab siv cov tswv cuab tshwj xeeb. | Kev sau tsis yog dav dav yog chav tshwj xeeb rau cov ntaub ntawv khaws cia thiab khaws cov ntaub ntawv uas muab kev txhawb nqa rau pawg, kab, cov npe thiab cov lus hash. |
Cov chav kawm Generic Collection yog nyob rau hauv System. Kev sau. Generics namespace. | Cov chav kawm Tsis yog-generic Collection yog nyob rau hauv System. Collections namespace. |
Type | |
A Generic Collection is strongly typed. | Kev sau tsis yog hom tsis yog ntaus ntawv. |
Cia Khoom | |
Cov Generic Collections khaws cov khoom sab hauv hauv arrays ntawm lawv cov hom tiag tiag. | Cov khoom tsis-generic khaws cia cov ntsiab lus sab hauv hauv cov khoom arrays yog li nws tuaj yeem khaws txhua hom ntaub ntawv. |
Summary – Generic vs Non-Generic Collection in C
Kab lus no tau tham txog qhov sib txawv ntawm Generic thiab Non-generic Collection hauv C. Qhov sib txawv ntawm Generic thiab Non-generic Collection yog qhov Generic Collection yog xav tau ntaus thaum lub Non-generic Collection yog tsis xav ntaus.