假设我有一个包含五列的DataTable。 我很好奇为什么以下工作:
dt.Columns.Add("Blah").SetOrdinal(5);但是以下引发了ArgumentOutOfRangeException :
dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count);我也试过了
dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count - 1);哪个有效,但我不完全确定原因。 是否与执行SetOrdinal之前添加的列有关,从而增加了超出列范围的计数?
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?
最满意答案
“它是否与在执行SetOrdinal之前添加的列有关”
是。
在评估最后一部分的时候:
.SetOrdinal(dt.Columns.Count);dt.Columns.Count == 6.一般来说,你应该避免使用引用同一语句中变化的复合语句。 虽然评估顺序是可预测的,但它并不是特别直观 - 你最终会犯错误。 这个更好:
var count = dt.Columns.Count; dt.Columns.Add("Blah").SetOrdinal(count);甚至更好:
dt.Columns.Add("Blah"); dt.Columns.SetOrdinal(dt.Columns.Count-1);不要只是为了缩短代码来缩短代码。 如果保存一些字符(这在编译代码中实际上没有任何意义)会使意图不那么明确,那么它绝对不值得。
"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.
使用SetOrdinal将列添加到DataTable(Adding columns to a DataTable using SetOrdinal)假设我有一个包含五列的DataTable。 我很好奇为什么以下工作:
dt.Columns.Add("Blah").SetOrdinal(5);但是以下引发了ArgumentOutOfRangeException :
dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count);我也试过了
dt.Columns.Add("Blah").SetOrdinal(dt.Columns.Count - 1);哪个有效,但我不完全确定原因。 是否与执行SetOrdinal之前添加的列有关,从而增加了超出列范围的计数?
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?
最满意答案
“它是否与在执行SetOrdinal之前添加的列有关”
是。
在评估最后一部分的时候:
.SetOrdinal(dt.Columns.Count);dt.Columns.Count == 6.一般来说,你应该避免使用引用同一语句中变化的复合语句。 虽然评估顺序是可预测的,但它并不是特别直观 - 你最终会犯错误。 这个更好:
var count = dt.Columns.Count; dt.Columns.Add("Blah").SetOrdinal(count);甚至更好:
dt.Columns.Add("Blah"); dt.Columns.SetOrdinal(dt.Columns.Count-1);不要只是为了缩短代码来缩短代码。 如果保存一些字符(这在编译代码中实际上没有任何意义)会使意图不那么明确,那么它绝对不值得。
"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.
发布评论