-4

I just started coding in C++ and I saw in some example codes this symbol: <<
is there a equavalent in C# if so What is it? Thank you in advance.

Djeroen
  • 571
  • 6
  • 21

2 Answers2

4

Disclaimer: I don't know anything about C#; this answer just describes the operator in C++.

It depends on context; that operator is often overloaded to mean different things for different types.

For integer types, it's the bitwise left shift operator; it takes the bit pattern of a value, and moves it to the left, inserting zero into the less significant bits:

unsigned x = 6;       // decimal  6, binary 00110
unsigned y = x << 2;  // decimal 24, binary 11000

In general, a left-shift by N bits is equivalent to multiplying by 2N (so here, shifting by 2 bits multiplies by 4).

I'm fairly sure this use of the operator is the same in C# as in C++.

The standard library overloads the operator to insert a value into an output stream, in order to produce formatted output on the console, or in files, or in other ways.

#include <iostream> // declare standard input/output streams

std::cout << 42 << std::endl;   // print 42 to the console, end the line, and flush.

I think C# has a TextWriter or something to handle formatted output, with Console.Out or something being equivalent to std::cout; but C# uses normal method calls rather than an overloaded operator.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

operator<< means exactly the same in C++ as it does in C#; it is the left-shift operator and moves all the bits in a number one bit to the left.

But, in C++, you can overload most operators to make them do whatever you like for user-defined types. Perhaps most commonly, the left- and right-shift operators are overloaded for streams to mean 'stuff this thing into that stream' (left-shift) or 'extract a variable of this type from that stream' (right-shift).

Tom
  • 7,269
  • 1
  • 42
  • 69