473,799 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# interview question

Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve
Mar 12 '07 #1
34 2585
My guess - and it is just a guess - is that the implicit cast probably uses
the ToString() method anyway. I would have thought any difference between
the two would have been optimized away by the compiler, but what do I know?

One thing's for sure, if you (or rather your prospective employers) need to
worry about performance at this level, then they're using the wrong
language. To me, it's on a par with worrying about whether:

i++;

is more efficient than:

++i;

Who cares. If you're writing code where these differences are significant,
use C or C++, or even Assembly language.

Just my 2c. YMMV.
Peter

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve

Mar 12 '07 #2
Steve

The code with the ToString() call would be quicker 'cus I don't think the
first would even compile.

Personally, I think it should have been

Console.WriteLi ne( "This value is: {0}", i );

In this case ToString would be called anyway, so an explicit call to
ToString() is superfluous.

Glenn

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve

Mar 12 '07 #3
On 12 Mar, 09:39, Steve Bugden <SteveBug...@di scussions.micro soft.com>
wrote:
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve
Well if we look at the IL of the two statements you can see the IL
takes a slightly different route on each go:

Here's the first one ( Console.WriteLi ne("The value is: " + i) ):

L_0007: call void [mscorlib]System.Console: :WriteLine(stri ng)
L_000c: ldstr "The value is: "
L_0011: ldloc.0
L_0012: box int32
L_0017: call string [mscorlib]System.String:: Concat(object,
object)
L_001c: call void [mscorlib]System.Console: :WriteLine(stri ng)

And here's the second:

L_0026: call void [mscorlib]System.Console: :WriteLine(stri ng)
L_002b: ldstr "The value is: "
L_0030: ldloca.s i
L_0032: call instance string [mscorlib]System.Int32::T oString()
L_0037: call string [mscorlib]System.String:: Concat(string,
string)
L_003c: call void [mscorlib]System.Console: :WriteLine(stri ng)

Now the two things that leap out immediately, in the first
String.Concat is called taking two objects and the int is boxed.
Boxing always carries an overhead. It has to be boxed to pass it as an
object (value type and reference type issue).

In the second it calls Concat taking two strings.
Here's the interesting bit. If we look at Concat(object, object) we
see

public static string Concat(object arg0, object arg1)
{
if (arg0 == null)
{
arg0 = Empty;
}
if (arg1 == null)
{
arg1 = Empty;
}
return (arg0.ToString( ) + arg1.ToString() );
}
So it takes the two objects and attempts to call ToString on both of
them. Internally then it looks like the first lot of IL that uses the
two object signature will end up calling an internal version of the
second lot of IL, ie calling string.Concat(s tring, string).

Therefore I'd say the second version is quicker as it only runs Concat
once.

Mar 12 '07 #4
"Peter Bradley" <pb******@uwic. ac.ukwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
One thing's for sure, if you (or rather your prospective employers) need
to worry about performance at this level, then they're using the wrong
language.
I couldn't agree more!!! What an utterly pointless and irrelevant
question...

Having been on both sides of the interview process many times over the
years, if you want to find out whether the prospective candidate is a good
developer or not, the most useful question is "Tell me about your last
development project"... You'll know in probably less than a minute if you
want to hire the person or not...
Mar 12 '07 #5
The first version, Console.WriteLi ne("The value is: " + i);, would force the
compiler to box the value of i into a reference type, and because of boxing,
it would also use string.Concat(o bject,object) which is slower than
string.Concat(s tring,string).

The second version, Console.WriteLi ne("The value is: " + i.ToString());,
needs no boxing and it would use string.Concate( string,string).

"Steve Bugden" <St*********@di scussions.micro soft.comha scritto nel
messaggio news:60******** *************** ***********@mic rosoft.com...
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve

Mar 12 '07 #6
Peter

You could argue they're testing the depth of knowledge by asking the
question, but I do agree, I wouldn't like to work for a company that
constantly questioned my choice of string concatenation technique.

Glenn

"Peter Bradley" <pb******@uwic. ac.ukwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
My guess - and it is just a guess - is that the implicit cast probably
uses the ToString() method anyway. I would have thought any difference
between the two would have been optimized away by the compiler, but what
do I know?

One thing's for sure, if you (or rather your prospective employers) need
to worry about performance at this level, then they're using the wrong
language. To me, it's on a par with worrying about whether:

i++;

is more efficient than:

++i;

Who cares. If you're writing code where these differences are
significant, use C or C++, or even Assembly language.

Just my 2c. YMMV.
Peter

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
>Hi,

I recently had an interview where I was asked a number of questions on
C#.
Unfortunatel y I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString() );

But I can't think why this should give a performance advantage. I can
only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and
generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve


Mar 12 '07 #7
I'm wrong, of course it would compile.

Must read documentation before posting, must read documentation before
posting...

"glenn" <gl**********@y ahoo.co.ukwrote in message
news:uZ******** *****@TK2MSFTNG P02.phx.gbl...
Steve

The code with the ToString() call would be quicker 'cus I don't think the
first would even compile.

Personally, I think it should have been

Console.WriteLi ne( "This value is: {0}", i );

In this case ToString would be called anyway, so an explicit call to
ToString() is superfluous.

Glenn

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
>Hi,

I recently had an interview where I was asked a number of questions on
C#.
Unfortunatel y I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString() );

But I can't think why this should give a performance advantage. I can
only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and
generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve


Mar 12 '07 #8
Hi Peter and Gelnn.

Thankyou for your comments.

It does compile by the way.

It could have been:
Console.WriteLi ne( "This value is: {0}", i );
I can't remember exactly.

I didn't get the job by the way. I narrowly missed out by a few percent on
the passmark and this question is one that cost me.

It does seem reasonable to me that the compiler should be able to deal with
it, as you mention.

Oh well I will know what to say next time.

Thanks once again.

Steve.

"glenn" wrote:
Steve

The code with the ToString() call would be quicker 'cus I don't think the
first would even compile.

Personally, I think it should have been

Console.WriteLi ne( "This value is: {0}", i );

In this case ToString would be called anyway, so an explicit call to
ToString() is superfluous.

Glenn

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?

I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve


Mar 12 '07 #9

"Steve Bugden" <St*********@di scussions.micro soft.comwrote in message
news:60******** *************** ***********@mic rosoft.com...
Hi,

I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.

It was something like this:

Consider the following code:

int i = 0;
Console.WriteLi ne("The value is: " + i);
Console.WriteLi ne("The value is: " + i.ToString());

Which would you use to write an integer to the console and why would it
give
better performance?
By definition, they mean exactly the same thing, so it doesn't matter which
one you use. (If it were possible for "i" to be null, the first one would
be better, since it could never cause a null reference exception, but a
value type can't be null.) The analyses in other posts in the thread aren't
about the difference between these two statements, they're about what IL a
particular version of the compiler happens to generate. If these people
expect you to know that off the top of your head, they're idiots. Likewise,
if these people think that the difference between the two sets of generated
IL cause any measurable difference in performance, they're idiots.

>
I said I would use: Console.WriteLi ne("The value is: " +
i.ToString());

But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is
required
from the integer for the line not using the ToString() method and generate
the same MSIL.

Can anyone please enlighten me?

Best Regards,

Steve

Mar 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
17441
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I give interviewees seems to trip them up all the time, and I wonder if I'm being too much of a hardass... it seems easy enough to ME, but these guys, when I get them up to the whiteboard, seem to get really confused. The exercise is this:
10
1888
by: Gopal Krish | last post by:
I was asked this question in an interview. How can you display the contents of an ASP page (from another web server) in a aspx page, ie, first half of the screen will display contents from asp and the other half will be from the current aspx page. Using frames is not an option. Any thoughts? (Is this even possible?)
0
6171
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip 2000 Interview questions of .NET , JAVA and SQL Server Interview questions (worth downloading it)
2
6971
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
4602
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
18
2868
by: Nobody | last post by:
I've been looking for a job for a while now, and have run into this interview question twice now... and have stupidly kind of blown it twice... (although I've gotten better)... time to finally figure this thing out... basically the interview question is: given an unsorted listed such as: 3,1,3,7,1,2,4,4,3 find the FIRST UNIQUE number, in this case 7... and of course the list can be millions long...
2
7229
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
2669
by: freesoftwarepdfs | last post by:
Ultimate list of Interview question website.....Do not miss it http://www.questpond.com http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html http://msdotnetsupport.blogspot.com/2006/08/net-windows-forms-interview-questions.html http://msdotnetsupport.blogspot.com/2006/08/net-remoting-interview-questions.html http://msdotnetsupport.blogspot.com/2006/08/c-interview-questions.html...
0
2233
by: Free PDF | last post by:
..NET , SQL Server interview questions websites.... http://www.questpond.com http://www.geocities.com/dotnetinterviews/ http://msdotnetsupport.blogspot.com/2007/01/net-interview-questions-by-dutt-part-2.html http://msdotnetsupport.blogspot.com/2006/08/net-windows-forms-interview-questions.html http://msdotnetsupport.blogspot.com/2006/08/net-remoting-interview-questions.html...
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.