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 7902949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:38:16+00:00 2026-06-03T09:38:16+00:00

I want to give a parameter to a method and i want my method

  • 0

I want to give a parameter to a method and i want my method to return data by looking the parameter. Data can be in type of boolean, string, int or etc. How can i return a variable type from a method? I don’t want to return an object type and then cast it to another type. For example:

BlaBla VariableReturnExampleMethod(int a)
{
    if (a == 1)
        return "Demo";
    else if (a == 2)
        return 2;
    else if (a == 3)
        return True;
    else
        return null;
}

The reason why i want that is i have a method that reads a selected column of a row from the database. Types of columns are not same but i have to return every column’s information.

  • 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-06-03T09:38:18+00:00Added an answer on June 3, 2026 at 9:38 am

    How can i return a variable type from a method? I don’t want to return an object type and then cast it to another type.

    Well that’s basically what you do have to do. Alternatively, if you’re using C# 4 you could make the return type dynamic, which will allow the conversion to be implicit:

    dynamic VariableReturnExampleMethod(int a)
    {
        // Body as per question
    }
    
    ...
    
    // Fine...
    int x = VariableReturnExampleMethod(2);
    
    // This will throw an exception at execution time
    int y = VariableReturnExampleMethod(1);
    

    Fundamentally, you specify types to let the compiler know what to expect. How can that work if the type is only known at execution time? The reason the dynamic version works is that it basically tells the compiler to defer its normal work until execution time – so you lose the normal safety which would let the second example fail at compile time.

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

Sidebar

Related Questions

I want to give different transition effects like : $(div).show(fadeIn) $(div).show(shake) $(div).show(fold) etc How
I want to give a blur effect to my DrawingVisual . I can do
Haskell beginner here. I want to pass a type parameter with JSON, and have
If I want to insert into a database column of type char(2) from C#.
I want to copy int[] named idNumbers from session scope to request scope. The
I want to make a method, that throws a specific exception, by a parameter
I want to give a meaningfull name for method which i am not able
I have a method with an out parameter that tries to do a type
I have a method in my code which must have a void return type
By SOAP method i want to consume a web service from android/eclipse.But the way

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.