|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Override_GetHashCode |
| 5 | +{ |
| 6 | + public class User |
| 7 | + { |
| 8 | + static void Wr_L(object o)=>Console.WriteLine(o);//Includes a new line char |
| 9 | + |
| 10 | + static void Wr_i(object m)=>Console.Write(m);//Includes a new line char |
| 11 | + |
| 12 | + public string TheFullName{ |
| 13 | + get{ |
| 14 | + return FName+" "+LName; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public string FName{get;set;} |
| 19 | + public string LName {get;set;} |
| 20 | + |
| 21 | + public override string ToString() |
| 22 | + { |
| 23 | + return TheFullName; |
| 24 | + } |
| 25 | + |
| 26 | + //Remember The data Structure Hash Table ≈ The hash table is a DS |
| 27 | + //which permits us to store information. Hash Table have what's called |
| 28 | + //a hash function. It is used to compute an index where the value can be |
| 29 | + //found at. |
| 30 | + |
| 31 | + //Obtaining hash Code method is useful for only to put our object in a hash table |
| 32 | + //If you're not planning to do that just do this: |
| 33 | + /* |
| 34 | + public override int GetHashCode() |
| 35 | + { |
| 36 | + return base.GetHashCode(); |
| 37 | + } |
| 38 | + */ |
| 39 | + |
| 40 | + //We have to create a method that determines whether |
| 41 | + //an object has already been put into a hash code. |
| 42 | + //This is because it will have the same hashed value. |
| 43 | + public override int GetHashCode() |
| 44 | + { |
| 45 | + return TheFullName.GetHashCode(); |
| 46 | + //Hashing works like this: if we take the same input and run it through |
| 47 | + //the function the ouput will be the same everytime. If two obj have |
| 48 | + //the same Full Name therefore the hash function should give us the same value |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public override bool Equals(object myObj) |
| 53 | + { |
| 54 | + //return base.Equals(myObj); |
| 55 | + //Casting myObj to User |
| 56 | + if(TheFullName == ((User)myObj).TheFullName) |
| 57 | + //This is saying if two Users have the same name then they are |
| 58 | + //the same entity |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + //Creating the static Method |
| 67 | + public static void PrintMe(List<User> users) |
| 68 | + { |
| 69 | + foreach(User user in users) |
| 70 | + { |
| 71 | + Wr_L(user.ToString()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + //Creating the static Method |
| 76 | + public static void PrintMe(User user) |
| 77 | + { |
| 78 | + Wr_L("Static Method"); |
| 79 | + user.outingToCons(2); |
| 80 | + } |
| 81 | + |
| 82 | + public string outingToCons(int num=5) |
| 83 | + { |
| 84 | + string msg=""; |
| 85 | + for(int j=0;j<num;j++) |
| 86 | + { |
| 87 | + msg += FName+ " " +LName+"\n"; |
| 88 | + } |
| 89 | + return msg; |
| 90 | + } |
| 91 | + |
| 92 | + public static int Find(List<User> users, string FuName) |
| 93 | + { |
| 94 | + for(int i=0;i<users.Count;i++) |
| 95 | + { |
| 96 | + if(users[i].TheFullName==FuName) |
| 97 | + { |
| 98 | + return i; |
| 99 | + } |
| 100 | + } |
| 101 | + //Error generated if you do not put the base case: |
| 102 | + //Not all code path return a value generated to overcome this do this: |
| 103 | + return -1;//MEANING NOT FOUND |
| 104 | + } |
| 105 | + //Overloading the Find Method as you can see the return type is the same |
| 106 | + //BUT THE PARAMETERS ARE NOT |
| 107 | + public static int Find(List<User> users, User user) |
| 108 | + { |
| 109 | + for(int i=0;i<users.Count;i++) |
| 110 | + { |
| 111 | + if(users[i].Equals(user)) |
| 112 | + { |
| 113 | + return i; |
| 114 | + } |
| 115 | + } |
| 116 | + //Error generated if you do not put the base case: |
| 117 | + //Not all code path return a value generated to overcome this do this: |
| 118 | + return -1;//MEANING NOT FOUND |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments