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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:44:23+00:00 2026-05-13T20:44:23+00:00

In some library I create I have to use following cast: public void Foo(IList<uint>

  • 0

In some library I create I have to use following cast:

public void Foo(IList<uint> uintsList) // I need to use uint in the interface for this method
{
    List<double> doublesList = uintsList.Cast<double>().ToList();
    // Do something with the doublesList
}

I assumed that cast uint -> double should be always valid, and during my test it always worked fine.

But in the application, which uses this method the InvalidCastException occured. Unfortunately I do not have access to this application. So here are my questions:

  • What could cause this exception occured? Isn’t the cast uint->double always valid?
  • How can I secure my algorithm to avoid this exception?

EDIT
Of course, before casting I always perform check to avoid situation when uintsList is null or empty

EDIT 2
OK, the problem is solved, I changed the cast using the ConvertAll method, but still I don’t understand how it could happen?
So this question still bothers me: how the same part of the code could run properly at my computer, and throw an exception at another? Different compiler/environment versions? Some specific settings? Can anyone tell me where should I seek for the reasons of this situation to avoid it in the future?

  • 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-13T20:44:24+00:00Added an answer on May 13, 2026 at 8:44 pm

    See: C# Converting List<int> to List<double>

    The cast uint -> double is valid, but you are casting a list of uints to a list of doubles. On most architectures the lists won’t even the same size (in bytes) – you will need to create an entirely new list and cast each element individually. I would use ConvertAll:

    List<double> doublesList = uintsList.ConvertAll(x => (double)x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to IMPLEMENT(not to use some library/open source) an event/message system. I have
I'm developing a library to IOS, which at some point need to create a
I have a library that I created with some business logic that includes writing
I need some library which would be able to keep my urls Indexed and
Suppose, I have an opensource project that depends on some library, that must be
I have the following object: public class MyClass { public int Id { get;
I use the following types to create a new function at runtime: typedef int
I have some utility functions like: void myVibratePhone() { AudioServicesPlaySystemSound (kSystemSoundID_Vibrate) ; } that
I need to create a thread in Delphi with the following characteristics: Waits until
I'm attempting to create a small ORM library for use in a Mojolicious web-application.

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.