How to declare natural ordering by overriding the comparison operators in C# .NET

In this post we saw one way to declare natural ordering for a custom class by implementing the generic IComparable of T interface. In this post we’ll look at how to achieve the same by overriding the 4 comparison operators:

<
>
<=
>=

Read more of this post

Advertisements

How to declare natural ordering by implementing the generic IComparer interface in C# .NET

In this post we showed how to declare natural ordering for a custom type by implementing the generic IComparable interface. We saw that it required us to implement the CompareTo method. The example we looked at was a simple Triangle class where we said that triangles can be ordered based on their areas. That’s probably a reasonable comparison for triangle.

However, what about the following object?

public class Building
{
	public double Area { get; set; }
	public int NumberOfRooms { get; set; }
	public string Address { get; set; }
	public bool ForSale { get; set; }
	public DateTime DateBuilt { get; set; }
}

Read more of this post

How to declare natural ordering by implementing the generic IComparable interface in C# .NET

Primitive types such as integers can be ordered naturally in some way. Numeric and alphabetical ordering comes in handy with numbers and strings. However, there’s no natural ordering for your own custom objects with a number of properties.

Consider the following Triangle class:

public class Triangle
{
	public double BaseSide { get; set; }
	public double Height { get; set; }

	public double Area
	{
		get
		{
			return (BaseSide * Height) / 2;
		}
	}
}

Read more of this post

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: