overloading a method with different outputs
i recently discovered a nice feature called overloading, now the question is can i also do that for outputs? for example i want to call a function that would automatically return the datatype i assign the function to
right now im geting an error saying "blabla and blabla cant overload eachother because they only differ by return types
Re: overloading a method with different outputs
Overloading has nothing to do with return type. Overloading means that the members have the same name and different numbers and/or types of parameters and that's all. Multiple overloads can return different types but that has nothing to do with the overloading. Consider what you're asking for. How could the system decide which overload to invoke based solely on return type given that there's no requirement that the return value even be used? Let's say that you had two DoSomething methods that returned a String and an Integer. If I did this:which one am I calling?
You might be able to use a generic method but then you only have so much control over what types it can return. For instance, there's no way to restrict it to return just an Integer or String. Basically, you should simply be implementing multiple methods. What benefit would overloading be over that anyway?
Re: overloading a method with different outputs
too bad, i figured it would look neat for reading memory and not having to do a byte conversion on the recieving end whether im reading an int or float etc, i guess i could write them out as "readbyte, readfloat" etc tho but it could take alot of writing since i also overloaded the input types into 3 varietys
Re: overloading a method with different outputs
It's not really going to take a lot of writing because, even if it did work the way you wanted, you'd still have to write out every overload. All this means is that you will have to add one extra word (about 4 or 5 characters) to each method. Everything esle will be exactly the same as it would if you could overload this way. For an example of exactly what you're looking at, check out the the BinaryReader and BinaryWriter classes. The BinaryWriter has one Write method that is overloaded to accept an argument of any of the basic types, while the BinaryReader class has multiple methods dedicated to one type each, e.g. ReadInt32 and ReadByte.