×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.