Loading, please wait...

MCQ

Operator overloading and Indexers in C# | C# Tutorial

Dec 21, 2020 C#, C Sharp, Shibam Dhar, 1031 Views
Most of the C# operators can be overloaded which means they can be redefined for custom actions. To do various operations by a single operator, it can be done with the help of operator overloading.

Operator overloading in C#

Most of the C# operators can be overloaded which means they can be redefined for custom actions. To do various operations by a single operator, it can be done with the help of operator overloading.

Operator overloading enables the making of user-defined operators implemented in such a way where operands are of user-defined class. Only the predefined set of C Sharp operators can be overloaded.

Operators are needed to be overloaded according to the programmer’s requirement to use operators with user-defined data types. An operator can be overloaded by defining a function to it. The function of the operator is declared by using the operator keyword.

Syntax:

access specifier  className  operator Operator_symbol (parameters)

{

    // Code

}

Overloading ability of various operators:

+, -, !, ~, ++, – –: They are unary operator which can be overloaded and it takes one operand.

+, -, *, /, %: They are binary operators that can be overloaded and it takes two operands.

==, !=, = : They are comparison operators with overloading features.

&&, ||: They are conditional logical operators and they cannot be overloaded directly.

+=, -+, *=, /=, %=, =: They are assignment operators and cannot be overloaded.

Overloading Unary Operators

In this, the return type can be anything except the void. the return type must be the type of ‘Type’ for – and ++ operators and must be a bool type for true as well as false operators. Here is a small condition about Booleans is that they can be overloaded as pairs only.

Overloading Binary Operators

They work with two operands. Arithmetic operators, Relational operators, and Arithmetic assignment operators are examples of binary operators. Overloading binary operators requires additional parameters which makes it different from unary operator overloading or else everything is the same.

Indexers in C#

Indexers allow objects to be indexed like an array.

A string variable is actually an object of the String class. Further, the string class is actually an array of char objects. In this way, the string class can be implemented as indexers.

string str = “Hello World”;

char x = str[4];

Console.WriteLine(x);

The above code will give “o” like results.

Anything whether class or struct everything can be put under indexers. If the user decides to create a class and define indexers for it, it will behave like a virtual array. So, with this user can send and retrieve data to and from the array respectively. Indexers are almost similar to the Properties. The main difference between Indexers and Properties is that the accessors of the Indexers will take parameters.

Syntax:

[access_modifier] [return_type] this [argument_list]

{

  get

  {

     // get block code

  }

  set

  {

    // set block code

  }




}

In the above syntax:

  • access_modifier: It can be public, private, protected, or internal.
  • return_type: It can be any valid C# type.
  • this: It is the keyword that points to the object of the current class.
  • argument_list: This specifies the parameter list of the indexer.
  • get{ } and set { }:These are the accessors.

Important things about indexers.

There are two types of indexers, which are: One-dimensional and Multi-dimensional indexers. Indexers can be overloaded too and they are quite different from properties. To declare an indexer this keyword is always used and a set accessor will always assign the value while the get accessor will return the value.

MCQ

Related Article