Say I have a method that returns a business object:
public static MyObject GetObject()
{
return Blah.Blah();
}
Now I need another method that does the same thing, but returns MyObject in an XML format:
public static string GetObject(bool returnXml)
{
return Blah.Blah.Xml();
}
I started with this approach, but soon realized that caller could specify false for returnXml.
Is my only option to rename my method to something like GetObjectAsXml?
Update…thanks all.
My original methods look like this.
public static MyObject GetObject()
{
return ConvertToMyObject(GetResponseAsXML());
}
I just need a new set of methods that look like this:
public static string GetObject()
{
return GetResponseAsXML();
}
From the answers it seems like the best way is to have a second set of methods that are named GetObjectAsXML, right? I don’t really want to do GetObject().ToXml() because I want to return the original response back.
I’m not sure why you would overload the same method here.
One is returning the object, the other is returning an XML representation/serialization of that object. I’d use two different methods.
What’s more interesting is what you do with these objects and XML serializations; the consumer would probably want 2 overloads that take either type as a parameter, but that would be dependent on your needs.