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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:37:39+00:00 2026-05-14T04:37:39+00:00

I disassembled the .NET ‘System’ DLL and looked at the source code for the

  • 0

I disassembled the .NET ‘System’ DLL and looked at the source code for the variable classes (string, int, byte, etc.) to see if I could figure out how to make a class that could take on a value. I noticed that the “Int32” class inherits the following: IComparable, IFormattable, IConvertible, IComparable, IEquatable.

The String and Int32 classes are not inheritable, and I can’t figure out what in these inherited interfaces allows the classes to hold a value. What I would want is something like this:

public class MyVariable : //inherits here
{
     //Code in here that allows it to get/set the value
} 

public static class Main(string[] args)
{
     MyVariable a = "This is my own custom variable!";
     MyVariable b = 2976;

     if(a == "Hello") { }
     if(b = 10) { }
     Console.WriteLine(a.ToString());
     Console.WriteLine(a.ToString());
}
  • 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-14T04:37:39+00:00Added an answer on May 14, 2026 at 4:37 am

    You can do that by overloading operators. There is a tutorial on MSDN.

    Let’s say you want a type that can be either a string or an int (like Haskell’s Either):

    public sealed class StringOrInt32
    {
        private string stringValue;
        private int int32Value;
        private bool isString;
    
        public bool IsString { get { return isString; } }
        public bool IsInt32 { get { return !isString; } }
    
        public string StringValue
        {
            get
            {
                if(!isString) throw new InvalidOperationException();
                return stringValue;
            }
        }
    
        public int Int32Value
        {
            get
            {
                if(isString) throw new InvalidOperationException();
                return int32Value;
            }
        }
    
        public StringOrInt32(string value)
        {
            isString = true;
            stringValue = value;
        }
    
        public StringOrInt32(int value)
        {
            isString = false;
            int32Value = value;
        }
    
        // Allows writing this:
        // StringOrInt32 foo = "Hello world!";
        public static implicit operator StringOrInt32(string value)
        {
            return new MyVariable(value);
        }
    
        // Allows writing this:
        // StringOrInt32 foo = 42;
        public static implicit operator StringOrInt32(int value)
        {
            return new MyVariable(value);
        }
    
        // Allows writing this:
        // StringOrInt32 foo = "Hello world!;
        // string bar = (string)foo;
        // Though foo.StringValue directly would be better
        public static explicit operator string(StringOrInt32 value)
        {
            return value.StringValule;
        }
    
        // Allows writing this:
        // StringOrInt32 foo = 42;
        // int bar = (int)foo;
        // Though foo.Int32Value directly would be better
        public static explicit operator int(StringOrInt32 value)
        {
            return value.Int32Value;
        }
    
        public static bool operator==(StringOrInt32 left, StringOrInt32 right)
        {
            if(left.IsString != right.IsString)
                return false;
            if(left.IsString)
                return left.StringValue == right.StringValue;
            else
                return left.Int32Value == right.Int32Value;
        }
    
        public static bool operator!=(StringOrInt32 left, StringOrInt32 right)
        {
            return !(left == right)
        }
    
        // Don't forget to override object.Equals(), object.GetHashCode(),
        // and consider implementing IEquatable<StringOrInt32>
        // Also, null checks, etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It'd be easiest to use phpjs.org's nl2br function: http://phpjs.org/functions/nl2br:480 EDIT… May 15, 2026 at 2:45 am
  • Editorial Team
    Editorial Team added an answer A Windows device driver is a bit like a DLL:… May 15, 2026 at 2:45 am
  • Editorial Team
    Editorial Team added an answer This module should handle RESTful uri's, access a database and… May 15, 2026 at 2:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.