Home »
.Net »
C# Programs
C# - Can we overload left shift operator (<<) to print a string like C++?
In this C# program, we are going to learn can we overload a left shift operator (<<) to print a string like C++? And if, not then how to overload it in C# to meet the result.
Submitted by IncludeHelp, on March 18, 2018
In C#, we cannot overload left shift operator (<<) like C++ directly. If you try to do it like C++, then produce compiler error. We can do it, but not exactly same using a second parameter of int type
Example:
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Sample
{
public static string operator<<(Sample s, int shft)
{
return "IncludeHelp.com";
}
}
class Program
{
static void Main()
{
Sample S1 = new Sample();
string str = S1 << 3;
Console.WriteLine(str);
}
}
}
Output
IncludeHelp.com
C# Operator Overloading Programs »