You are here:

C#/Why operator over loading is used ?


I am beginner in c#. I could not understand about operator overloading. Please help me. Why should we use operator overloading? why not use function overloading instead? what is the real purpose of using operator overloading? could you give a real time simple example about operator overloading.
Please help me.
Thanks a billion.

ANSWER: Hi there,

Operator overloading is far different from function overloading.

Let's build a simple class, with one field:

public class MyClass
  public int myValue;

For example, normally you cant sum two classes:

MyClass one = new MyClass();
MyClass two = new MyClass();
one.myValue = 3;
two.myValue = 6;
int result = one + two;

It will simply throw an exception. However if you define what to do when you sum two classes, it will take action according to your overload. Let's add the operator overload to the previous class. It will look like this:

public class MyClass
  public int myValue;

  public static int operator +(MyClass c1, MyClass c2)
     return c1.myValue + c2.myValue;

Now if we call the same code up there, it wont throw exception and give us result as 9.

Additionally, you can find 2 great examples of operator overloading in here.

Hope that helps,


---------- FOLLOW-UP ----------

I am very satisfied in your answer. I have another question regarding attributes and reflection. could you give me answer in same manner.
Thanks a billion
Mohan J

ANSWER: Hi again,

Attributes are used to define features for methods, classes, properties, structs etc.
For example here is our class again:

public class MyClass
 public int myValue;

If we want to convert this class with its fields and values into xml format, we will have to add serializable attribute and it will look like this:

public class MyClass
 public int myValue;

If we need, we can read these attributes in runtime using reflection.


You can also define your own attributes by inheriting from System.Attributes. For a detailed example for this, see here.

---------- FOLLOW-UP ----------

QUESTION: Thank you for your reply.
Please help me in my another request.
In, I have read that if viewstate is set to false for a control, its state will not be maintained. but I set to false for textbox view state. but It still maintains its value between the round trips. what is the reason? How to disable the view state for textbox control in
Please help me.
Thanks a lot.

If you set an element's EnableViewState as false, then after page calls a post back, element should take its default value.
So either you see that situation without a post back (maybe just a client side js call), or your default value is same as the value you change later.

Hope that helps.


All Answers

Answers by Expert:

Ask Experts


Murat Mehmet


I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.


I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Was in R & D for 2 years in a popular Turkish technology website:

2011 Computer Engineering graduation in University of Trakia in Turkey.

©2017 All rights reserved.