0% found this document useful (0 votes)
2 views

Advanced C#01

The document discusses the concepts of equality and hash code in programming, focusing on generics, type safety, and the importance of the Equals method versus the = operator. It covers various constraints for generic types, including class, struct, enum, and interface requirements, as well as casting operators. Additionally, it provides examples of linear search and bubble sort algorithms, emphasizing the use of user-defined classes and interfaces for effective comparison and sorting.

Uploaded by

elmaznyx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced C#01

The document discusses the concepts of equality and hash code in programming, focusing on generics, type safety, and the importance of the Equals method versus the = operator. It covers various constraints for generic types, including class, struct, enum, and interface requirements, as well as casting operators. Additionally, it provides examples of linear search and bubble sort algorithms, emphasizing the use of user-defined classes and interfaces for effective comparison and sorting.

Uploaded by

elmaznyx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
Equality and Get Hash Code Lec 01 15) that allows you to write more flexible end 1. Type Safety Benefits of Generics: 2 Code Reusobility 3. Performence Mclhod > dadabyee Alias navne Stage For mull dxbakyee can bedebetbee Aukemati oly by Comper SGpti- (GGT GEES TE > must detervine thedabe ype Linear Search Example : > Cort use, bean nobeverg ob fmplements it sows emery wjeck inherts = Operator Vs Equals Method © ae Lowe ches With User Defined Struct + =e VS = operators rot ex «Equals Metod Irherted From Veletype class which shen from — ‘Object And Overridden to Com Id values Gealue equality) With User Defined Class : ie Compares re Soonk, N Faun Method taherited From Object Tht Compares references (Gemory adresses) (eye ls Is.) Compare references , bo Compartvalues s- override Masheades. = 2 epee Equals Bubble Sort Example : Compares each elements becide eaclother & pub dhe greater on Righ sid Cy au >< operators —> Se we use «Comparele implimenited fam =F Cemparple a . —) Sewe put Corstraint ong T> tak mot . Fomplements TCompaable Enteface sing Where Keyword Orn fe Is and As Casting Operator ‘The is operator is used to check if an object is of a specific type. It returns a boolean value’ true if the object is of the specified type (or can be cast to that type) false otherwise The as operator is tries to cast an object toa specified type if the jecessed returns casted object if the cast fails, it returns null instead of throwing on a> incascel Ast. ng obsek pst C Built in Generic Duterfaces > Generic earowmeler S re implewerded in OF kb allases aud use As on 15 instecdl of Pit saves Lime of checking Casting but still needs bol ck For pull A Built-in interface Contain a Signature for Equals Method Equals(object obj) method, as TEquatablecT> allows This is typically more efficient than using 1) iy with the specific type (T), without needing to box or perform unnecessary type casts «Equals + LEquatable ¢ Pet > Class naame - ES LG TTT > equalityConparer) Cree permease xc etree a een Poach arch(arr,new Point(10,4),new PointVEqualityComparer() )); public Er Comparer) where T : Icouparable i for Cint 4 arr.Length; 1+ Poe Ree eC CLS , r er eer) ee nila arte Gaigomp PR rien Point(] arr = {new Point(2,6), new Point(4,4)}; Helper .BubbleSort(arr, new PointYComparer()); aints On Generic Type : T Can be base class or derive from the specified base class [ren-rllable reference type] T : Employee => Tmust be Employee or class That Inherit from Employee 1.2.2. cbase class name>? : T Cen be bose class or derive from the specified bose class [either nullable or non-ullable type T : Employee? => Tmust be Employee or clase That Inherit from Employee (KOSS) 2.1 : T Must be Implement interface Interface name T: IComparable => T must be Type Implement Generic Interface IComparable 3.1 new() : T Must have a public parameterless constructor constraint must be specified last and can’t be combined with the struct and unmanaged constraints

You might also like