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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:11:48+00:00 2026-05-15T11:11:48+00:00

I have a function which performs some operation i.e. fetches some data from database.

  • 0

I have a function which performs some operation i.e. fetches some data from database.
The generic it takes is primitive type always i.e. int, char, bool, string etc. I can easily create its instance. But I can’t set the value that I have fetched from database to this instance.

public T PerformOperation<T>()
{    
    object instance = (T)Activator.CreateInstance(typeof(T));

    object result=FetchData();

    instance = (T)result; //It gives error on this statement
}

The function is called as:

int result = PerformOperation<int>();

Is there some way of casting the object Type to any generic type which is always primitive?

  • 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-15T11:11:48+00:00Added an answer on May 15, 2026 at 11:11 am

    When you already have type T, why box it into Object unnecessary.

    public T PerformOperation<T>()
    {    
        T instance = (T)Activator.CreateInstance(typeof(T)); // why do you need this ?
    
        T result = FetchData();
    
        //instance = (T)result;
        return result;
    }
    

    or may be this way, if you must use Object.

    public T PerformOperation<T>()
    {    
        //object instance = (T)Activator.CreateInstance(typeof(T));    
        //object result=FetchData();
    
        return (T)FetchData(); // If I could get you correctly.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some R code which performs some data extraction operation on all files
I have a function which performs a foreach loop on an array from a
I have written a little helper function which performs some sort of drawing operations,
I have a JavaScript function which is quite long and performs a number of
I have a for-loop which performs the following function: Take a M by 8
I have a function which takes two arguments, the ID of an item (wine)
I have a function which takes several boolean template arguments: template<bool par1, bool par2,
I would like to create an advancedmodule with a cmdlet function which performs some
I have a function which takes as an argument an XDocument object. I need
I'm trying to implement a function which allows the user to input some type

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.