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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:28:38+00:00 2026-05-11T20:28:38+00:00

I need to downcast a long to an int in a method where the

  • 0

I need to downcast a long to an int in a method where the long is passed as a ref variable:

public void Foo(ref long l)
{
    // need to consume l as an int
}

How can I easily 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-05-11T20:28:38+00:00Added an answer on May 11, 2026 at 8:28 pm

    You can’t. However, any value you want to put into a ref int can be put into a ref long anyway – you’ve just got to worry about the initial value, and what you want to do if it’s outside the range of int.

    How many places do you need to write to the ref parameter or read it within your code? If it’s only in one or two places, you should be okay just to cast appropriately at the right times. Otherwise, you might want to introduce a new method:

    public void Foo(ref int x)
    {
        // Here's the body I *really* want
    }
    
    public void Foo(ref long x)
    {
        // But I'm forced to use this signature for whatever
        // reasons. Oh well. This hack isn't an *exact* mimic
        // of ref behaviour, but it's close.
    
        // TODO: Decide an overflow policy
        int tmp = (int) x;
        Foo(ref tmp);
        x = tmp;
    }
    

    The reason I say in the comments that it’s not an exact mimic for the behaviour is that normally changes to the original ref parameter are visible even before the method returns, but now they’ll only be visible at the very end. Also, if the method throws an exception, the value won’t have been changed. The latter could be fixed with try/finally, but that’s a bit clunky. In fact, if you want the try/finally behaviour you can do it all in a single method easily:

    public void Foo(ref long x)
    {
        int y = (int) x;
        try
        {
            // Main body of code
        }
        finally
        {
            x = y;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help to solve this. I have a gridview and inside the gridview
need a little help with this one. I have a form that I am
I need a way to verify during compile time that upcast/downcast of a pointer
Need a way to navigate/browse XSLT files easily with Vim. Similar to the way
I've heard it said several times that if you need to dynamically downcast then
How can I downcast a list of objects so that each of the objects
I've something along this lines: public class Something { private IDictionary<object,Activity> fCases; public IDictionary<object,Activity>
Need to insert selected text on the page into textarea. There must be some
Need to call a filter function on some options based on a radio selected
need to add a 2nd css stylesheet to a page. do i add a

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.