Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
IEquatable revised book
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { class MyClass : IComparable<MyClass>, IEquatable<MyClass> { public int Val; public MyClass(int x) { Val = x; } // Implement IComparable<T>. public int CompareTo(MyClass other) { Console.WriteLine("In MyClass.CompareTo\n"); return Val - other.Val; // Now, no cast is needed. } // Implement IEquatable<T>. public bool Equals(MyClass other) { Console.WriteLine("In MyClass.Equals\n"); return Val == other.Val; } // An override of Equals(Object). public override bool Equals(Object obj) { Console.WriteLine("In MyClass.bool.Equals\n"); if(obj is MyClass) return Equals((MyClass) obj); return false; } // An override of GetHashCode(). /*public override int GetHashCode() { return Val.GetHashCode(); }*/ } class CompareDemo { // Require IEquatable<T> interface. public static bool IsIn<T>(T what, T[] obs) where T : IEquatable<T> { Console.WriteLine("In CompareDemo.bool.IsIn\n"); Console.WriteLine(what.GetType()); foreach(T v in obs) if(v.Equals(what)) // Uses Equals() return true; return false; } // Require IComparable<T> interface. This method assumes // a sorted array. It returns true if what is inside the range // of elements passed to obs. public static bool InRange<T>(T what, T[] obs) where T : IComparable<T> { Console.WriteLine("In CompareDemo.bool.InRange\n"); Console.WriteLine(what.GetType()); if(what.CompareTo(obs[0]) < 0 || what.CompareTo(obs[obs.Length-1]) > 0) return false; return true; } } public class Program { public static void Main(string[] args) { // Use IsIn() with int. int[] nums = { 1, 2, 3, 4, 5 }; if(CompareDemo.IsIn(2, nums)) Console.WriteLine("2 is found."); if(!CompareDemo.IsIn(99, nums)) Console.WriteLine("99 is not there."); // Use IsIn() with MyClass. MyClass[] mcs = { new MyClass(1), new MyClass(2), new MyClass(3), new MyClass(4) }; if(CompareDemo.IsIn(new MyClass(3), mcs)) Console.WriteLine("MyClass(3) is found."); if(!CompareDemo.IsIn(new MyClass(99), mcs)) Console.WriteLine("MyClass(99) is not found"); Console.WriteLine("---------------------------------------"); // Use InRange() with int. if(CompareDemo.InRange(2, nums)) Console.WriteLine("2 is within the range of nums."); if(CompareDemo.InRange(1, nums)) Console.WriteLine("1 is within the range of nums."); if(CompareDemo.InRange(5, nums)) Console.WriteLine("5 is within the range of nums."); if(!CompareDemo.InRange(0, nums)) Console.WriteLine("0 is NOT within the range of nums."); if(!CompareDemo.InRange(6, nums)) Console.WriteLine("6 is NOT within the range of nums."); Console.WriteLine("---------------------------------------"); // Use InRange() with MyClass. if(CompareDemo.InRange(new MyClass(2), mcs)) Console.WriteLine("MyClass(2) is within the range of mcs."); if(CompareDemo.InRange(new MyClass(1), mcs)) Console.WriteLine("MyClass(1) is within the range of mcs."); if(CompareDemo.InRange(new MyClass(4), mcs)) Console.WriteLine("MyClass(4) is within the range of mcs."); if(!CompareDemo.InRange(new MyClass(0), mcs)) Console.WriteLine("MyClass(0) is NOT within the range of mcs."); if(!CompareDemo.InRange(new MyClass(5), mcs)) Console.WriteLine("MyClass(5) is NOT within the range of mcs."); } } }
run
|
edit
|
history
|
help
0
Linq FirstOrDefault() null check in c#
test
Ligma Bofa Joe
Sort A List of Ages of Students (Solution: Stack / O(N^3) Complexity)
Generic Coveriant
3
2.2 Basic types: Dictionary
Write a program to filter distinct items from the array
Determine if a string has all unique characters.
Decorator Design Pattern