开发者

Is there any significant benefit to reading string directly from control instead of moving it into a variable?

sqlInsertFrame.Parameters.AddWithValue("@UserName", txtUserName.txt);

Given the code above...if I don't have any need to move the textbox data into a string variable, is it best to read the data directly from the control?

In terms of performance, it would see开发者_StackOverflow社区m smartest to not create any unnecessary variables which use up memory if its not needed. Or is this a situation where its technically true but doesn't yield any real world results due to the size of the data in question.

Forgive me, I know this is a very basic question.


No validation? Usually I cache the string and pass it to the business layer for validation, if the validation succeeds, then I save the cached string to the database. It is slower to get the string again from the Window than read the value off my cache.


Actually it's not a very basic question at all. As far as performance goes it's hard to say without measuring. In this case, a local variable in an inner scope may well get optimized completely out of the picture by a smart compiler. It may exist only in the CPU registers.

Introducing a local variable can make your code much more readable, if it does, do it!

See also ... Local variable assignment to avoid multiple casts and, in particular, the accepted answer.


Never cache what you can compute. Your reasoning is sound.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜