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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:03:40+00:00 2026-05-25T12:03:40+00:00

I just began work on a large c# code base. Several classes in the

  • 0

I just began work on a large c# code base. Several classes in the code are almost identical to others, which is a question in and of itself, and need to be converted in order to reuse existing logic.

Currently in the code there are extention methods scattered around which take a bit of effort to find and figure out what they are called in order to use them. I would like to move all the convert methods into the class so that they could very easily be found and used. However, I am not sure what is the most accepted way of converting one data type to another when you have the source for everything.

In order to convert ClassA to ClassB should I:

  1. add a method to ClassA,
  2. create a ClassAHelper,
  3. use an extention method for ClassA?
  • 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-25T12:03:41+00:00Added an answer on May 25, 2026 at 12:03 pm

    Code to convert one class to another (I assume you’re talking about runtime conversions) may not be the necessary way to go. For example, if you have class X and class Y that each have void Blah() that you wish to invoke from method C, and method C takes X as a parameter, what you might instead do is extract a common interface for X and Y and then have C accept the interface instead. Therefore your Y can remain Y.

    Another option is to use the adapter pattern which can make Y conform to the extracted interface of X, and this is particularly useful if Y has the same general functionality but under a different name, or if changing the code of one of the classes is painful, impossible, or simply undesired.

    An example, given

    interface ICanBlah { void Blah(); }
    class X : ICanBlah { public void Blah() { } }
    class Y { public void Blahhhhh() { } }
    

    You can write

    public class YBlahAdapter : ICanBlah 
    {
        Y itsY;
    
        public YBlahAdapter(Y y) { itsY = y; }
        public void Blah() { itsY.Blahhhhh(); }
    }
    

    So wherever you have a method that takes ICanBlah and you have an object of type Y, you can wrap it in an adapter and then pass the adapter to the method.

    On the other hand, if you really want to do a runtime conversion of X to Y or Y to X, you can define implicit or explicit conversion operators for one or both.

    public static explicit operator Y(X x) { /* build and return Y from X */ }
    

    (This operator would need to be declared in the source file of one of the types.)

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

Sidebar

Related Questions

I just began using Visual Studio to work on a practice .NET project. I
I've just started one of my courses, as classes just began 2 weeks ago,
I just began to work with Objective-C and I'm managing pretty well. My last
I just began reading more about Markov Chain Generators today, and am really intrigued
I just began looking into source control.... And installed subversion from collabnet... I also
I just began working on a little twitter-app using tweepy. is there any kind
I've recently began using dTrace and have noticed just how awesome it is. Its
I took the plunge this afternoon and began studying LINQ, so far just mucking
I have just begun working on a project which uses Mercurial as a version
I recently started working on a large complex application, and I've just been assigned

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.