5

Operators with different color in Visual Studio

 2 years ago
source link: https://www.tabsoverspaces.com/233875-operators-with-different-color-in-visual-studio?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Operators with different color in Visual Studio

24 Sep 2021 1 mins C#, Visual Studio

When writing some code day or two ago I noticed one operator color in Visual Studio being different color than it usually is. I initially thought it’s a fluke in syntax highlighting that will eventually go away. When it didn’t, I decided to investigate.

Look at this image, with double font size to make it more visible.

Different operator color

The first + sign is clearly different color than the other. What’s going on? It’s actually pretty easy to explain. If you have a custom type with overloaded operator, using it results in this highlighting.

class Program
{
	static void Main(string[] args)
	{
		var x = new Test();
		_ = x + x;
		_ = 1 + 1;
	}
}

class Test
{
	public static Test operator +(Test lhs, Test rhs) => default;
}

Plus operator is probably not overloaded that often, but for i.e. (in)equality, this might become handy. You’re clearly going to see the type has some handling of the (in)equality and whether that’s expected.

Jiří Činčura is an independent developer, .NET, C# and Firebird expert, focusing on data and business layers, language constructs, parallelism, databases and performance. He's Microsoft Most Valuable Professional and frequent speaker. You can read his articles, guides and tips and tricks at www.tabsoverspaces.com.

Copyright © Jiří Činčura. All rights reserved.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK