Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Inputbox Pin
Kannan Kalyanaraman11-Jun-03 18:53
Kannan Kalyanaraman11-Jun-03 18:53 
GeneralRe: Inputbox Pin
Nick Seng11-Jun-03 19:57
Nick Seng11-Jun-03 19:57 
QuestionAdd copyright symbol to Label or TextBox, anyone? Pin
Khang Nguyen11-Jun-03 12:54
Khang Nguyen11-Jun-03 12:54 
AnswerRe: Add copyright symbol to Label or TextBox, anyone? Pin
dynamic11-Jun-03 13:39
dynamic11-Jun-03 13:39 
GeneralRe: Add copyright symbol to Label or TextBox, anyone? Pin
Khang Nguyen12-Jun-03 5:02
Khang Nguyen12-Jun-03 5:02 
GeneralC# Excel COM Add-in Pin
Francis B.11-Jun-03 9:32
Francis B.11-Jun-03 9:32 
GeneralRe: C# Excel COM Add-in Pin
Kannan Kalyanaraman11-Jun-03 19:01
Kannan Kalyanaraman11-Jun-03 19:01 
GeneralWriting Faster Managed Code Pin
MtnBiknGuy11-Jun-03 8:36
MtnBiknGuy11-Jun-03 8:36 
Jan Gray of the Microsoft CLR Performance Team wrote an excellent article
titled "Writing Faster Managed Code: Know What Things Cost"
(http://msdn.microsoft.com/library/?url=/library/en-us/dndotnet/html/fastmanagedcode.asp).

This article has motivated me to seek answers to the following questions.
Any replies from the group are appreciated. The questions probably
(hopefully) have very simple answers.

Between these two foreach loops, is there any difference in performance,
safety or correctness? (All examples are in C#)

1. declare and initialize new type in body of loop:
foreach (T t in tList)
{
A a = new A(t.ID);
//do more work
myCollection.Add(a);
}

2. declare new type outside loop, and 'new' it inside loop:
A a = null;
foreach (T t in tList)
{
a = new A(t.ID);
//do more work
myCollection.Add(a);
}

I would think #2 is preferrable, but I see that most C# examples use the
style of #1. In fact, I often see the following in C# examples:

T MyMethod()
{
T t = new T();
return t;
}
This would be a big problem in C++, but in C# the garbage collector makes it
OK. But does that mean it's a good practice? Why is it so common in
examples?

Finally, I'm curious if there are any performance differences between the
following two loops. The reason I ask is that I'm one of those people who
(prior to reading the above mentioned article) pulled the array.Length
property out of the loop header in the interest of avoiding extra calls to
get the length value. I now understand why that is not optimal. I'm curious
if any processor optimizations or compiler optimizations make choice 1 equal
to or better than choice 2 below:

1. repeat same property calls inside a loop:
for (int i = 0; i < 10; ++i)
{
DoAlgorithm1(i, T.x);
DoAlgorithm2(i, T.x + y);
DoAlgorithm3(i, T.x + y);
}

2. pull property calls outside the loop:
double x = T.x;
double w = x + y;
for (int i = 0; i < 10; ++i)
{
DoAlgorithm1(i, x);
DoAlgorithm2(i, w);
DoAlgorithm3(i, w);
}

All input is appreciated!
GeneralRe: Writing Faster Managed Code Pin
Daniel Turini11-Jun-03 9:50
Daniel Turini11-Jun-03 9:50 
GeneralXML parsing solution for C# Pin
Anonymous11-Jun-03 6:49
Anonymous11-Jun-03 6:49 
GeneralRe: XML parsing solution for C# Pin
leppie11-Jun-03 7:00
leppie11-Jun-03 7:00 
GeneralRe: XML parsing solution for C# Pin
Daniel Turini11-Jun-03 7:43
Daniel Turini11-Jun-03 7:43 
GeneralRe: XML parsing solution for C# Pin
Nick Parker11-Jun-03 8:03
protectorNick Parker11-Jun-03 8:03 
GeneralRemoting !! Need Help !!!! Pin
Gaurika Wijeratne11-Jun-03 6:47
Gaurika Wijeratne11-Jun-03 6:47 
GeneralRe: Remoting !! Need Help !!!! Pin
leppie11-Jun-03 7:11
leppie11-Jun-03 7:11 
Generalmp3 Pin
kgoodrich11-Jun-03 5:45
kgoodrich11-Jun-03 5:45 
GeneralRe: mp3 Pin
shaunAustin11-Jun-03 5:57
shaunAustin11-Jun-03 5:57 
GeneralRe: mp3 Pin
kgoodrich11-Jun-03 6:17
kgoodrich11-Jun-03 6:17 
GeneralRe: mp3 Pin
shaunAustin11-Jun-03 7:56
shaunAustin11-Jun-03 7:56 
GeneralRe: mp3 Pin
J. Dunlap11-Jun-03 8:04
J. Dunlap11-Jun-03 8:04 
QuestionWhere's that article about Shell Toolbars? Pin
ThaRudeDude11-Jun-03 5:29
ThaRudeDude11-Jun-03 5:29 
AnswerRe: Where's that article about Shell Toolbars? Pin
Kannan Kalyanaraman11-Jun-03 5:45
Kannan Kalyanaraman11-Jun-03 5:45 
GeneralRe: Where's that article about Shell Toolbars? Pin
ThaRudeDude18-Jun-03 3:12
ThaRudeDude18-Jun-03 3:12 
GeneralXml Serialization Help Needed!! Pin
Ricardo Kirkner11-Jun-03 5:11
Ricardo Kirkner11-Jun-03 5:11 
GeneralRe: Xml Serialization Help Needed!! Pin
shaunAustin11-Jun-03 7:59
shaunAustin11-Jun-03 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.