Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:04:28+00:00 2026-05-28T20:04:28+00:00

I have 2 methods that are very similar. public string GetMyObjectInJson(parameters) { MyObject TheObject

  • 0

I have 2 methods that are very similar.

public string GetMyObjectInJson(parameters)
{
 MyObject TheObject = new MyObject(); 
 ...lots of work here
 Javascript Serializer = new Javascript Serializer();
 return Serializer.serialize(TheObject); 
}

public MyObject GetMyObject(parameters)
{
 MyObject TheObject = new MyObject(); 
 ...lots of work here
 return TheObject; 
}

How should I rewrite this so that I only have one method that can return 2 different types: sometimes a string and sometimes an object.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:04:30+00:00Added an answer on May 28, 2026 at 8:04 pm

    You shouldn’t. You should have two methods, so that it’s clear from the calling code what you want and what you’ll get back.

    You can, however, use one method to do most of the work:

    public string GetMyObjectInJson(parameters)
    {
        MyObject object = GetMyObject();
        Javascript Serializer = new JavascriptSerializer();
        return Serializer.serialize(TheObject); 
    }
    
    public MyObject GetMyObject(parameters)
    {
         MyObject TheObject = new MyObject(); 
         ...lots of work here
         return TheObject; 
    }
    

    However, this suggests that actually the MyObject class should potentially have a SerializeToJson method, so callers would write:

    string json = foo.GetMyObject().SerializeToJson();
    

    Note that even if you couldn’t chain one method directly to another, you could have one private method containing the common work, which would be called from both the public methods. You shouldn’t make your public API less clear when you can just refactor your implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two extension methods that are very similar. I'd like to remove the
I have two very similar methods: public IQueryable<User> Find(Func<User, bool> exp) { return db.Users.Where(exp);
I have a method that effectively takes a string. However, there is a very
I'm working on legacy code that looks very similar to this: public class BaseParser
I have two very similar classes that do essentially the same thing. The only
I have two pieces of code which are very similar in that they register
I have a very basic doubt regarding the method that gets executed when app
I have a very thin interface that's going to define one method. Should I
So I have a very simple class that has a method called getThumbUrl() but
In C# I have methods that use [WebMethod(EnableSession = true)] I consume them in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.