Overriding the + and – operators in C# .NET

It’s easy to override the mathemtical operators like + or – in C# to build custom operations.

Consider the following simple Rectangle class:

public class Rectangle
{
	public Rectangle(int width, int height)
	{
		Height = height;
		Width = width;
	}

	public int Width
	{
		get;
		set;
	}

	public int Height
	{
		get;
		set;
	}
}

…and the following two Rectangle objects:

Rectangle start = new Rectangle(8, 10);
Rectangle additional = new Rectangle(4, 3);

Let’s say that you’d like to add the width and height of “additional” to “start” so that “start” will grow according to the dimensions of “additional”. There are several ways to achieve this in code one of them is to override the mathematical operators ‘+’ and ‘-‘. It’s very easy, you just need to be aware of the “operator” keyword. The Rectangle class can be extended as follows:

public static Rectangle operator +(Rectangle startingRectangle, Rectangle anotherRectangle)
{
	startingRectangle.Height += anotherRectangle.Height;
	startingRectangle.Width += anotherRectangle.Width;
	return startingRectangle;
}

public static Rectangle operator -(Rectangle startingRectangle, Rectangle anotherRectangle)
{
	startingRectangle.Height -= anotherRectangle.Height;
	startingRectangle.Width -= anotherRectangle.Width;
	if (startingRectangle.Height < 0) startingRectangle.Height = 0;
	if (startingRectangle.Width < 0) startingRectangle.Width = 0;
	return startingRectangle;
}

…and then you can add the two rectangles:

start += additional;

“start” will now have a height of 13 and width of 12. Similarly “start” can shrink as follows:

start -= additional;

“start” will now have a height of 7 and width of 4.

View all various C# language feature related posts here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Overriding the + and – operators in C# .NET

  1. Brian Dead Rift Webb says:

    Reblogged this on Brian By Experience.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

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: