开发者

Interview Questions (in C#) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

So I was given a small program to write, which I did开发者_开发问答 well but then I was asked the following and I was a bit confused.

What are the values of the following?

// I told them that they would get an error as they are not initialized, so they are pointing to some address in memory...

int a;
Object b;
int d = a;
bool c; 

if in Java, replace the last line with

boolean c;

Please let me know the correct answer to this as I am sure it will be brought up again. Thank you :)


The .Net CLR initializes all fields and locals to their default values.
In your case, that's 0, null, and false.

However, this code will not compile under any circumstances.

Inside a method, C# will not allow you to use uninitialized locals, so d = b will not compile.
As instance fields, C# does not allow you to use this until you're inside the constructor, so d = a will not compile in a field initializer.

As static fields, this would compile, but the static keyword is missing.


In Java, all of this is also true, except that Java does allow you to use this in field initializers.
Therefore, this code is valid in Java as instance fields.


In C# the default value for an int is 0. An Object is null. And a bool is false.

A simple google found the asnwer.

http://msdn.microsoft.com/en-us/library/83fhsxwc(v=vs.80).aspx


a == 0, b == null, d == 0, c == false

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜