icomparable

Having trouble with a method call and getting a correct value back

对着背影说爱祢 提交于 2019-12-13 00:47:48
问题 the method call in question is within this writeline call at the end Console.WriteLine("Taxpayer # {0} SSN: {1}, Income is {2:c}, Tax is {3:c}", i + 1, taxArray[i].SSN, taxArray[i].grossIncome, taxRates.CalculateTax(taxArray[i].grossIncome)); Here is the rates class which the method is in public class Rates { // Create a class named rates that has the following data members: int incLimit; double lowTaxRate; double highTaxRate; // use read-only accessor public int IncomeLimit { get { return

Define how a comparison operator is applied to a type?

ⅰ亾dé卋堺 提交于 2019-12-12 04:38:51
问题 How can I define whether and how a comparison operator is applied to operands of my type? 回答1: You implement the IComparable interface with the CompareTo method. To use all of the operators, try this: public sealed class Foo : IEquatable<Foo>, IComparable<Foo> { public static int Compare(Foo first, Foo second) { if (Object.ReferenceEquals(first, null)) return (Object.ReferenceEquals(second, null) ? 0 : -1); return first.CompareTo(second); } public static bool operator==(Foo first, Foo second)

How do you use a custom type for a dictionary key?

孤者浪人 提交于 2019-12-12 03:13:20
问题 I have a custom class which uses generics. I need to use this class as the key of a dictionary as shown in the code example below: I am able to hit the overridden Object.GetHashCode method, but i'm not sure how to proceed from there. Please help, Thanks. Module Module2 Dim myStore As New Dictionary(Of Pair(Of Long, Integer), String) Public Function ContainsItem(id As Long, code As Integer) As Boolean Return myStore.ContainsKey(New Pair(Of Long, Integer)(id, code)) End Function Public Class

How to Naturally Sort a DataView with something like IComparable

给你一囗甜甜゛ 提交于 2019-12-10 16:59:39
问题 My DataView is acting funny and it is sorting things alphabetically and I need it to sort things numerically. I have looked all across the web for this one and found many ideas on how to sort it with ICompare, but nothing really solid. So my questions are How do I implement ICompare on a DataView (Looking for code here). How to correctly decipher from a column full of strings that are actual strings and a column full of numbers(with commas). I need code to help me out with this one guys. I am

SortedList that just takes IComparable<T>

醉酒当歌 提交于 2019-12-10 09:37:45
问题 I have an interface IScriptItem that implements IComparable<IQueueItem> . In my eyes it would seem enough to have IComparable items in order to have a sorted anything. But all I can find is Dictionaries, Hashtables and SortedLists that are actually SortedTrees. What I'm looking for is a sorted generic list that takes IComparables. Am I looking in the wrong places? 回答1: There's nothing built-in. You have some options: Use SortedList with a dummy TValue. Use a list or array and call List.Sort()

C#: Range intersection when endpoints are null (infinity)

坚强是说给别人听的谎言 提交于 2019-12-08 07:28:29
Ok, I have these intersection methods to work with ranges, and they work well as long as the range endpoints are not null: public static bool Intersects<T>(this Range<T> first, Range<T> second, IComparer<T> comparer) { return comparer.Compare(first.Start, second.End) <= 0 && comparer.Compare(first.End, second.Start) >= 0; } public static Range<T> GetIntersectionWith<T>(this Range<T> first, Range<T> second, IComparer<T> comparer) { // Return null, if any range is null or if they don't intersect at all if (first == null || second == null || !Intersects(first, second, comparer)) return null; var

C#: Range intersection when endpoints are null (infinity)

有些话、适合烂在心里 提交于 2019-12-08 06:55:01
问题 Ok, I have these intersection methods to work with ranges, and they work well as long as the range endpoints are not null: public static bool Intersects<T>(this Range<T> first, Range<T> second, IComparer<T> comparer) { return comparer.Compare(first.Start, second.End) <= 0 && comparer.Compare(first.End, second.Start) >= 0; } public static Range<T> GetIntersectionWith<T>(this Range<T> first, Range<T> second, IComparer<T> comparer) { // Return null, if any range is null or if they don't

Why is C# Array.BinarySearch so fast?

ぃ、小莉子 提交于 2019-12-06 18:50:17
问题 I have implemented a very simple binarySearch implementation in C# for finding integers in an integer array: Binary Search static int binarySearch(int[] arr, int i) { int low = 0, high = arr.Length - 1, mid; while (low <= high) { mid = (low + high) / 2; if (i < arr[mid]) high = mid - 1; else if (i > arr[mid]) low = mid + 1; else return mid; } return -1; } When comparing it to C#'s native Array.BinarySearch() I can see that Array.BinarySearch() is more than twice as fast as my function, every

Implementing IComparable

混江龙づ霸主 提交于 2019-12-06 18:03:57
问题 I am implmenting the IComparable to sort like typed objects. My question is why does it cast type person to int32? The array's Sort() seems to cast each type in the array to the type that I am using for comparison. Comparable: public class Person:IComparable { protected int age; public int Age { get; set; } public int CompareTo(object obj) { if(obj is Person) { var person = (Person) obj; return age.CompareTo(person.age); } else { throw new ArgumentException("Object is not of type Person"); }

SortedList that just takes IComparable<T>

浪子不回头ぞ 提交于 2019-12-05 15:00:40
I have an interface IScriptItem that implements IComparable<IQueueItem> . In my eyes it would seem enough to have IComparable items in order to have a sorted anything. But all I can find is Dictionaries, Hashtables and SortedLists that are actually SortedTrees. What I'm looking for is a sorted generic list that takes IComparables. Am I looking in the wrong places? There's nothing built-in. You have some options: Use SortedList with a dummy TValue. Use a list or array and call List.Sort() or Array.Sort() when necessary. Write your own. Use a third party library For this particular case check