There is no right or wrong right in this case. There is the indicated and the most efficient. If the fields are integers, treat them as such. It may seem that there is nothing stopping you from using the integers converted to string in this situation, but there is a "rule" in system development that I follow very successfully: Do the less surprising thing. Always.
Amazingly, in this case, it should be read as "something that begets less surprise".
In this case, if the field is integer (or long
) in the database, treat it as such in your code as well. This will generate less surprise to some other programmer, or even yourself when you come to fix a bug in the code in a few years. In addition, using Convert.ToInt32()
instead of ToString()
will not make any difference, but just the fact that you wonder if you treat the integer as string in your code is right, already shows that the ideal would not even need to ask that question and treat integer as the integer that is.
I also guarantee that in%% of% this field is integer. If it compares the ID of the GridView with the id in the dataset / datatable, it may have to convert back to integer from any
There are even future features that might be affected by this unnecessary conversion made today. For example, you may need to put the Ids inside a DataTable
and if they are strings the operations will be slower (not much, since Hashset uses a hash to be found, but there is a loss of performance). You may want to sort the Ids to know what was created before in the database (since it is identity) and the sorting of strings, in addition to much slower, does not follow the numeric order: Hashset
while "10" < "2"
.
In short: while there are reasons to use integer as an integer, with advantages, there are no advantages or reasons (in your case) to treat them as string.
Do not preach parts yourself (and others who use your code). Do what you generate the least surprise. Ask yourself: What is the most anticipated? Is an integer field in the database treated as a string in your code, or is it treated as an integer?