KeyCode.Alpha1 + 1

In the for loop video you use the + operator on KeyCode.Alpha1.

i.e. KeyCode.Alpha1 + index

KeyCode.Alpha1 is an object though, so I assume that this is overloading the + operator and the operation is defined within whatever class KeyCode.Alpha1 is an instance of.

Am i correct?

Hi,

Welcome to our community! :slight_smile:

That’s an interesting question which I cannot answer. I skimmed the Enum class in C# and it does not seem to provide any information on the + operator. Maybe I missed the part.

This article might be interesting for you. And this comment.

Last but not least, please hover your mouse over KeyCode in Visual studio, press down the ctrl key on your keyboard and click on KeyCode. A list should appear.

Hey Nina

Thanks for the reply.

I’ve just had a play around with some code after reading the stack overflow post you linked. I’m convinced now that it’s an enum.

For example you can do…

enum Day {Monday, Tuesday}

public class MyClass

{

static public void Main()

{

Console.WriteLine(Day.Monday + 1);

}

}

This would output Tuesday

Edit: Just to be clear to anyone reading this, because I know others have been confused too. The reason you can do the operation KeyCode.Alpha1 + 1 is because KeyCode is a variable of type enum.

Um, it has always been an enum, thus the enum keyword. Or did you mean something else?

I mean that I didn’t know KeyCode was an enum, and now I do. :wink:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms