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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:03:10+00:00 2026-06-07T04:03:10+00:00

A coworker and myself where discussing conversion in .NET he pointed out there are

  • 0

A coworker and myself where discussing conversion in .NET he pointed out there are four ways to convert a type, at first I could only come up with two, implicit and explicit then he pointed out user defined conversions and using helper classes so I decided to look it up here http://msdn.microsoft.com/en-us/library/ms173105.aspx while reading that it dawned on me that I do a lot of WPF and often times use the IValueConverter interface.

My Question started as – isn’t IValueConverter just an example of a conversion with a helper class (when you implement it of course)? Then I was like well, what is the real difference between user-defined conversions and conversions with a helper class? If you follow the links from the above mentioned MSDN page the documentation is sorda slim. Like this is the example from the conversion operators page.

class SampleClass
{
    public static explicit operator SampleClass(int i)
    {
        SampleClass temp = new SampleClass();
        // code to convert from int to SampleClass...

        return temp;
    }
}

It doesn’t really make it so clear. To me it looks like a static class that needs an int in the ctor?

Anyway – Hopefully some C# ninja can illuminate this subject. One final thought is, I generally try and stay away from converting things unless there is a real good reason (i.e. parsing a spreadsheet) in regular everyday code I tend to think of it as a code smell, is that considered best practice?

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-06-07T04:03:13+00:00Added an answer on June 7, 2026 at 4:03 am

    This isn’t a full answer to your questions, but that code snippet overrides the “explicit cast” of the class, which isn’t really intuitive fromt eh method signature. Basically it would allow you to do:

    int one = 1;
    SampleClass x = (SampleClass)one;
    

    Common sense says that cast should fail, because and int isn’t a SampleClass but the code snippet in your question comes into play, and makes the cast possible.

    The other complementing method is:

    public static implicit operator SampleClass(int i)
    

    Note the keyword here is implicit instead of explicit, and this version would allow for implicit casting, so this would work:

    int one = 1;
    SampleClass x = one;
    

    Note that you no longer have to specify the cast.

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

Sidebar

Related Questions

My coworker and I were discussing the correct way to write a unit test
I have a coworker that is against type inference in C#. I believe most
Myself and coworker both have the same version of Git Tower, when he is
My coworker's installation of visual studio 2008 breaks every time there's an exception, mine
First a little about myself. I am not an experienced software engineer, architect or
My coworker and I were discussing the time complexity of writing a string to
a former coworker (artist) and myself (programmer) are currently developing a small game together
My coworker and I were discussing our desire to write iPhone applications. He seems
Had a conversation with a coworker the other day about this. There's the obvious
A coworker of mine created a basic contact-us type form, which is mangling accented

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.