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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:45:14+00:00 2026-05-25T01:45:14+00:00

I have a class with a bunch of implicit operator s. In the code

  • 0

I have a class with a bunch of implicit operators. In the code below, only a few examples of the implicit operators are used. I’m looking for any ideas how to refactor this, without making the SafeValue class generic. Any ideas?

public class SafeValue {
    private readonly object value;
    public SafeValue(object value) {
        if (value == somethingSpecial) {
            value = null;
        }
        this.value = value;
    }
    public static implicit operator string(SafeValue instance) {
        return (string)instance.value;
    }
    public static implicit operator int(SafeValue instance) {
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (int)instance.value;
    }
    public static implicit operator int?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new int?((int)instance.value);
    }
    public static implicit operator DateTime(SafeValue instance)
        if (instance.value == null) {
            throw new InvalidCastException("Cannot convert type");
        }
        return (DateTime)instance.value;
    }
    public static implicit operator DateTime?(SafeValue instance) {
        if (instance.value == null) {
            return null;
        }
        return new DateTime?((DateTime)instance.value);
    }
}
  • 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-25T01:45:14+00:00Added an answer on May 25, 2026 at 1:45 am

    As i said in the comments, i think it would be best to replace the code completely.

    If that is not an option, you might consider the following, mostly cosmetic changes:

    • You might want to get rid of the manually thrown exceptions. Why check for null and throw an exception if you would do so anyway in the next line? You are throwing exceptions in there anyway. Casting null to int will result in an exception.

    • Why all the new s ? No need for them. Just cast.

    • No need to doublecast your Nullables.

      DateTime? dt = (DateTime?) DateTime.Now; 
      

    Works as expected

        DateTime? dt = (DateTime?) null; 
    

    Also works (that is what you are doing anyway, since you return null; in your function that has a return value of DateTime? in the signature, right?

    All your functions get reduced to only one line of code. Here is the DateTime? one in all its glory:

    public static implicit operator DateTime?(SafeValue instance) {    
        return (DateTime?)instance.value;    
    }
    

    Repeat for int? and the like.

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

Sidebar

Related Questions

I have a class with a bunch of properties that look like this: public
Say I have: class Vector3 { float x, y, z; ... bunch of cuntions
I have a class which is essentially just holds a bunch of constant definitions
Let's say I have one class Foo that has a bunch of logic in
I have a bunch of class who implement a common interface : Command. And
I have a helper class that is just a bunch of static methods and
Let's say that I have a bunch of class instances that serve different purposes,
I have a bunch of entity class (generated by Linq to SQL) with a
I have a bunch of textboxcontrol on my page all with class 'red'. On
I have a class with a bunch of methods. some of these methods are

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.