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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:36:13+00:00 2026-06-06T17:36:13+00:00

I read of a useful trick about how you can avoid using the wrong

  • 0

I read of a useful trick about how you can avoid using the wrong domain data in your code by creating a data type for each domain type you’re using. By doing this the compiler will prevent you from accidentally mixing your types.

For example, defining these:

public struct Meter
{
    public int Value;

    public Meter(int value)
    {
        this.Value = value;
    }
}

public struct Second
{
    public int Value;

    public Second(int value)
    {
        this.Value = value;
    }
}

allows me to not mix up meters and seconds because they’re separate data types. This is great and I can see how useful it can be. I’m aware you’d still need to define operator overloads to handle any kind of arithmetic with these types, but I’m leaving that out for simplicity.

The problem I’m having with this approach is that in order to use these types I need to use the full constructor every time, like this:

Meter distance = new Meter(5);

Is there any way in C# I can use the same mode of construction that a System.Int32 uses, like this:

Meter distance = 5;

I tried creating an implicit conversion but it seems this would need to be part of the Int32 type, not my custom types. I can’t add an Extension Method to Int32 because it would need to be static, so is there any way to do this?

  • 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-06T17:36:15+00:00Added an answer on June 6, 2026 at 5:36 pm

    You can specify an implicit conversion directly in the structs themselves.

    public struct Meter
    {
        public int Value;
    
        public Meter(int value)
        {
            this.Value = value;
        }
    
        public static implicit operator Meter(int a)
        {
             return new Meter(a);
        }
    }
    
    public struct Second
    {
        public int Value;
    
        public Second(int value)
        {
            this.Value = value;
        }
    
        public static implicit operator Second(int a)
        {
             return new Second(a);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me say I have read this useful article thoroughly and am using
I have searched for a CGI and read about it but nothing useful found.
Read about Server push here . I want to push data to client from
In the What is the most useful R trick? ( here ), I read
First of all, I read erickson's useful reply to Why can’t I define a
I've lately read about the MvcMiniProfiler, which I found really useful. However, we are
I read this [useful article] that says I can create a library of inline
I read this very useful article about disabling all the https certificates programmatically. I
I read about HMVC and is still not exactly sure how it can be
I read some useful posts here on SO about previous maven questions, I'm currently

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.