Home » 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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.