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

  • Home
  • SEARCH
  • 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 461467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:58:39+00:00 2026-05-12T22:58:39+00:00

Consider: object o = 123456U; ulong l = (ulong) o; // fails But this:

  • 0

Consider:

object o = 123456U;
ulong l = (ulong) o; // fails

But this:

object o = 123456U;
ulong l = (ulong) (uint) o; // succeeds

The real issue I have is that I’d like to have a function that depending on a parameter type, processes them differently. Such as:

void foo(object o)
{
switch (Type.GetTypeCode(o.GetType()))
   {
      case TypeCode.UInt32:
      case TypeCode.UInt64:
      ulong l = (ulong) o;
      RunUnsignedIntVersion(l);
      break;
      case TypeCode.Int32:
      case TypeCode.Int64:
      long n = (long) o;
      RunSignedVersion(n);
      break;
   }
}

and you can’t make the following calls:

foo(123456U);
foo(123456);

I know there are ways to do this using generics. But I’m using .net micro framework and generics are not supported. But any C# 3.0 compiler specific features are supported including anonymous functions.

Edit I’d like to avoid having to handle each type separately. Is there a way this can be done and still have a parameter of object type?

  • 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-12T22:58:39+00:00Added an answer on May 12, 2026 at 10:58 pm

    The unbox operations support only the unbox, not any coercion that you might expect.

    Whilst this can be frustrating it is worth noting that fixing this would

    1. make unboxing considerably more expensive
    2. possibly complicate the language due to nasty edge cases on method overload selection

    Amongst others, for some in depth explanation, Eric Lippert is, as ever, most instructive

    If you care about performance the only effective way to do this is (as Jimmy points out)

    case TypeCode.Int32:
        RunSignedVersion((int) o);
        break;
    case TypeCode.Int64:
        long n = (long) o;
        RunSignedVersion(n);
        break;
    

    This seems not too onerous.

    If this is too painful then you may make use of Convert.ToInt64 or Convert.ToUInt64() with the associated cost.

    void foo(object o)
    {
       switch (Type.GetTypeCode(o.GetType()))
       {
          case TypeCode.UInt32:
          case TypeCode.UInt64:
              ulong l = Convert.ToUInt64(o);
              RunUnsignedIntVersion(l);
              break;
          case TypeCode.Int32:
          case TypeCode.Int64:
              long n = Convert.ToInt64(o);
              RunSignedVersion(n);
              break;
       }
    }
    

    If Convert is not available here is the rotor source for the relevant methods:

        [CLSCompliant(false)]   
        public static ulong ToUInt64(object value) {
            return value == null? 0: ((IConvertible)value).ToUInt64(null);
        }
    
        [CLSCompliant(false)]   
        public static long ToInt64(object value) {
            return value == null? 0: ((IConvertible)value).ToInt64(null);
        }
    

    IConvertible is supported as an interface in the compact framework, I would assume this would therefore work but have not tried it.

    If you want the MicroFramework then I suggest simply implementing the conversion options on a per type basis is the best you can do. The API is so sparse that there really isn’t much else possible. I would also suggest that anything based on boxing is risky since this is a significant allocation overhead in a very memory constrained environment.

    If you are trying to implement a string.Format() alike have you considered System.Ext.Text.StringBuilder.AppendFormat followed by a ToString?

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

Sidebar

Related Questions

Consider this JavaScript code snippet: Object.prototype.log = function() { // here, you have a
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
I'm playing with Remote Actors but I'm facing some difficulties. Consider this server: object
Consider an object used to store a collection of items, but that collection may
Consider this multi-level nested JavaScript object. function foo() { var channels = { 2:
Consider this class: class foo(object): pass The default string representation looks something like this:
let consider i have a object 'msg' i re-define it in following cases but
I have this question about c# language's dynamic binding behavior. Consider the following object
Consider an object chain like: foo.bar.baz.bing In this particular instance, I am given foo
Consider example of the parser like this: object TestParser extends RegexParsers { override protected

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.