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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:57:35+00:00 2026-06-02T20:57:35+00:00

Let’s say I have a DataTable with five columns. I am curious as to

  • 0

Let’s say I have a DataTable with five columns. I am curious as to why the following works:

dt.Columns.Add("Blah").SetOrdinal(5);

But the following throws an ArgumentOutOfRangeException:

dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count);

I also tried

dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count - 1);

which works, but I’m not entirely sure why. Does it have something to do with the column being added before the SetOrdinal is executed, thus increasing the count beyond the range of columns?

  • 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-06-02T20:57:36+00:00Added an answer on June 2, 2026 at 8:57 pm

    “Does it have something to do with the column being added before the SetOrdinal is executed”

    Yes.

    At the time the last part is evaluated:

    .SetOrdinal(dt.Columns.Count);
    

    dt.Columns.Count == 6. Generally speaking you should avoid compound statements that reference something that changes in the same statement. Although the evaluation order is predictable it’s not especially intuitive — you’ll end up making mistakes. This is better:

    var count = dt.Columns.Count;
    dt.Columns.Add("Blah").SetOrdinal(count);
    

    or even better:

    dt.Columns.Add("Blah");
    dt.Columns.SetOrdinal(dt.Columns.Count-1);
    

    Don’t try to make your code shorter just for the sake of making it shorter. If saving a few characters (which really means nothing in compiled code) makes the intent less clear, then it’s definitely not worth it.

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

Sidebar

Related Questions

Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have the following type type Key = String type Score =
Let's say we have the following: abstract class A; class B : public A;
Let's say I have the following grid: I can't seem to figure out how
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \

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.