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

  • Home
  • SEARCH
  • 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 7602815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:30:30+00:00 2026-05-30T23:30:30+00:00

I have a generic method in a base class similar to the following: protected

  • 0

I have a generic method in a base class similar to the following:

protected bool TryConvertTo<TIn, TOut>(TIn objIn, out TOut objOut)
    where TIn : MyObjectBase
    where TOut : MyObjectBase
{
    objOut = objIn is TOut ? (TOut) (object) objIn : default (TOut);
    return !ReferenceEquals(objOut, default (TOut));
}

Assume that I’m null-checking wherever necessary and handling any application exceptions accordingly. Is it bad practice to “double cast” objIn to object and then to TOut? If it is bad practice, are there any technical reasons why, or is it merely a reflection of an overall design issue?

I feel the usage is appropriate given my use case, but since I don’t normally use such an expression, I thought it might be safe to get the community’s opinion.

  • 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-30T23:30:31+00:00Added an answer on May 30, 2026 at 11:30 pm

    Given that you’ve constrained both of them to derive from a particular class (assuming MyObjectBase is indeed a class) why not just use:

    protected bool TryConvertTo<TIn, TOut>(TIn objIn, out TOut objOut)
        where TIn : MyObjectBase
        where TOut : MyObjectBase
    {
        objOut = objIn as TOut;
        return objOut != null;
    }
    

    (I’m assuming there are no custom conversion operators or == / != overloads in MyObjectBase.)

    On the other hand, this feels like a fairly strange thing to want to do in the first place… are you sure it’s an appropriate design, leaving aside the implementation?

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

Sidebar

Related Questions

In my base class I have a generic method (ideally this would be a
How can I invoke overloaded generic method in IronRuby? I have a .NET class
I'm trying to create a generic method to use in my base class for
does anyone know a way to call a generic method of a base class
I have a generic method: (simplified) public class DataAccess : IDataAccess { public List<T>
I have the following structure: abstract class Base { public abstract List<...> Get(); //What
I have a generic base wrapper class to wrap a couple of com components
I have a generic base repository class for LINQ-to-Entities that can do things like
There is a generic method that takes a class as parameter and I have
I have a base class, with a method, where I would like to use

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.