namespace first
{
   public class index
    {
        int count;
        public index()
        {
            count = 0;
        }
        public static index operator++(index x)
        {
            x.count++;
            return count;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            index i = new index();
            i++;

            Console.WriteLine(i);
        }
    }
}

when i am typing this program i got the error like.
first.index.operator++(first.index):not all code paths return a value;

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Operator overloading is the least useful thing in C#, the operators are not any different than method calls, it is just good for mathematic obsessive people who like to use smybols rather than words to feel special as they are not valued when they go to techno bar to catch some chicks.

public static index operator++(index x)
{
x.count++;
return count;
}

The return type of this function is of type 'index'. But you return an integer type. This fucntion should be written as

public static index1 operator ++(index1 x)
        {
            x.count++;
            return x;
        }

Operator overloads sure have their use. I don't see why you want to overload the C# ++ operator! It is there for your use! So use it as such, don't get fancy because you like to do it.
An index can easely be incremented with ++. Einstein once said: keep it simple, not simpler.

>keep it simple, not simpler.

that is what i told Scott just yesterday night :)

Einstein and i must think in paralel :)

But back to topic, even the + operator in math can be used with the Add name as a method. at compiler level each operator and function does the same thing. it is just for us, representation of that functionality is what changes.

what would you lose if you instead of int a = b + c use int a = Math.Add(b,c) ?

Serkan:
I think a=b+c is faster than a = Math.Add(b,c).
But for example the string class overloads the + operator to perform string concatenation. You could also use the Concat method for this, but a simple + is IMHO simpler. If you look in the produced IL code you would see that this + is translated into Concat by the C# compiler:cool: Let the compiler worry about the gory details, that way you don't have to do that.
But you are right, most of the time we don't need operator overloading.

i got what you mean, what i say is that "+" is a paradigm that we use for addition, to enable polimorphysm for addition they have the same operator concat strings when the type is string. It is our brain what makes the abstraction here, so instead of associating addition or concatenation with + sign, we could do it with a word "Add". the only problem would be of what data structure would this Add be part of. + does not belong to any class since it is an operator whereas Add is implemented inside different classes.

>
what would you lose if you instead of int a = b + c use int a = Math.Add(b,c) ?

In addition to what danny said calling Math.Add() pushes another call on the stack where as a=b+c does not

Math.Add ??? Exists in Math class??! The answer shouldn't be to use it or not to use it.
Regardless this small operation in sometimes I shouldn't do that. Try we log error\exceptions
I can't permit developers team to do that

try
{
}
catch (Exception ex)
{
File.AppendAllText(ExceptionFilePath, ex.Message);
}

BUT, They must have method logs the error

try
{
}
catch (Exception ex)
{
LogException(ex);
}
void LogException(Exception ex)
{
//Today DM (Development Manager) said we should use File
//Yesterday they said we should log in database
//tomorrow I think they aren't interested to know what exceptions happened
}

Centralize operations important.

Again, regardless small adding operation mentioned above.

Comments
Keep us awake man!

Math.Add ??? Exists in Math class??! The answer shouldn't be to use it or not to use it.
Regardless this small operation in sometimes I shouldn't do that. Try we log error\exceptions
I can't permit developers team to do that

If serkan uses then we should use it! We're all guys and we measure things .. we can't let serkan have a bigger call stack than us!

//Today DM (Development Manager) said we should use File
//Yesterday they said we should log in database
//tomorrow I think they aren't interested to know what exceptions happened

You should talk to your DM and write your own error reporting routines across the web. I record exceptions locally to a file and push the error to my company's webserver so I can check on application problems throughout the day. I usually know a customers problem and solution before they call me. Unfortunately that means I am also admitting my programs sometimes crash :P

start quote:

namespace first
{
   public class index
    {
        int count;
        public index()
        {
            count = 0;
        }
        public static index operator++(index x) /* static fun cant manupulate the data member of object. you should use it as " public void operator++(int x)"  here int x is just  to differentiate the prefix & postfix increment not any other use. fun^  should'nt return any value.  you should write  count++ only within the fun body*/
        {
            x.count++;
            return count;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            index i = new index();
            i++;

            Console.WriteLine(i);
        }
    }
}

when i am typing this program i got the error like.
first.index.operator++(first.index):not all code paths return a value;

look between /* */

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Ramesh already answered your quesiton. Change it to:

public static index operator ++(index x) 
    {
      x.count++;
      return x;
    }
This article has been dead for over six months. Start a new discussion instead.