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 have created a class library in VB .NET. Some code in the library
In the STL library some containers have iterators and it is commonly held that
I have several C# projects along with some internal library components that I'm trying
I have a class library with some extension methods written in C# and an
Is there some library for using some sort of cursor over a file? I
I've got some library code that works on a range of .NET runtimes (regular,
Here's the scenario. I'm debugging my own app (C/C++) which is using some library
Is is there some sort of library that would allow me to convert a
One of classes in my program uses some third-party library. Library object is a
Some time ago I put together a time based library that could be used

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.