Re: some c# questions!! please send me the answer



You may get more responses in microsoft.public.languages.csharp.

I guess I would fail the interview! :-) Here are the answers I know:

> What's wrong with a line like this? DateTime.Parse(myString);

Well, nothing is _wrong_ with it, strictly speaking, so long as you
realize that it's going to try to parse the DateTime string using the
current UI culture. In other words, by not specifying an
IFormatProvider on the call you're letting the culture default to
whatever is set for the current user when the code runs. If that's what
you wanted to do, then no problem. However, it would be better style to
explicitly specify that you want the current UI culture, thus making
your intent unambiguous.

I believe that in .NET 2.0, as well, there is a TryParse method on
DateTime. (I may be wrong.) In that case it would be better to use
TryParse first to see if the string is a valid DateTime before doing
the Prase. In .NET 1.1 all you can do is Prase and catch any exceptions
resulting from invalid strings.

> What are PDBs? Where must they be located for debugging to work?

A .pdb file is generated on a Debug build. It contains symbolic
information about variables, arguments, etc in the corresponding dll or
exe so that the debugger can show the original names from the code. I
believe that it must be located in the same directory as the resulting
assembly, but I could be wrong about that.

> Write a standard lock() plus "double check" to create a critical
> section around a variable access.

Well, that's a bit of a stupid request. Double-check locking has long
been demonstrated not to work on many architectures. People still do
it, but it's generally a bad idea. A double-check lock looks like this:

static readonly object myLock = new object();
private Thing myVar = null;

public Thing Thing
{
get
{
if (this.myThing == null)
{
lock (myLock)
{
this.myThing = new Thing();
}
}
return myThing;
}
}

The problem is that the idiom doesn't work. See the page
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
for more information.

> What is FullTrust? Do GAC'ed assemblies have FullTrust?

Full trust means that a .NET application can do anything it wants on
your machine. I'm still learning about .NET security, so I can't answer
the second part.

> Why is catch(Exception) almost always a bad idea?

Well, it's not _always_ a bad idea. As with all such rules, there are
exceptions. However, the short answer is that you almost never want to
catch every possible exception that a method or property might throw.
OutOfMemoryException is one example of an exception that many methods /
properties may throw that you definitely don't want to catch, because
there's nothing you can do about it.

You should catch only those exceptions that you can handle in some
intelligent way, so it's almost never a good idea to catch _all_
exceptions, because you can never deal intelligently with any possible
exception.

The only exception to the rule (no pun intended) is when you're dealing
with a method for which you have no documentation about which
exceptions it can throw, and for which you want to continue if it
fails. In that case you don't have much choice but to catch all of
them, since you don't know which specific ones to catch.

> What is the difference between a Debug and Release build? Is there a
> significant speed difference? Why or why not?

As with all questions about speed (or efficiency), the answer is, "It
depends." The short answer is that I imagine that there would be a
speed difference because a Debug build definitely won't be optimized.
However, keep in mind that some optimization is done in the compiler
and some in the JITter. Only compiler optimization matters in this
case.

> Contrast the use of an abstract base class against an interface?

An abstract class allows you to write code that will be inherited by
derived classes. An interface declares a contract, but does not allow
inheritance of code. An abstract class forms part of your class
hierarchy, and so affects how you organize your code. Interfaces cut
across the class hierarchy and so place no restrictions on how you
organize your classes.

> What is the difference between a.Equals(b) and a == b?

In value types (structs and native value types), there is no difference
(assuming that a struct is implemented properly and overrides ==
correctly). In reference types the standard is that .Equals compares
the contents of the types (typically comparing keys or other indicators
of business-level identity), while == determines whether the two object
references point to the same object in memory.

> In the context of a comparison, what is object identity versus object
> equivalence?

Well, I'm not up on the jargon terms, but "identity" to me means
business-level identity: do these two objects represent the same
customer, for example. Object equivalence might mean two references
pointing to the same object in memory, but it seems an odd term to
apply to that. Or maybe it means "all fields equal." I'd have to
consult a dictionary (or the Web).

> How would one do a deep copy in .NET?

You would have to write your own method / interface, since the
(unenforced) standard is that the Clone() method produce a shallow
copy.

> Explain current thinking around IClonable.

Current thinking is that ICloneable performs a shallow copy: all of the
fields of an object instance are copied, but if any fields contain
object references then the copy refers to the same objects as the
original.

You typically implement deeper copies by writing your own methods.

> What is boxing?

Boxing is creating a "reference" shell around a value object so that it
can be used as an object rather than as a value. For example, an
ArrayList is designed to hold a collection of objects. If you want to
put ints into an ArrayList, they have to be converted to something that
looks like an object, so the compiler creates an object-wrapper around
each "int" (boxes the int) and then adds those object wrappers to the
ArrayList.

When you fetch an item out of the ArrayList and cast it back to an
(int), the compiler generates code to remove the int from its box and
copy the int value into the receiving variable. This is called
unboxing.

> Is string a value type or a reference type?

It is an immutable reference type. Since it is immutable it acts like a
value type in some ways. In particular, String overrides == to do
content comparisons instead of an identity comparison.

> Why are out parameters a bad idea in .NET? Are they?

"out" parameters aren't a bad idea in .NET. FxCop warns you not to use
them, but only because other programmers might be unfamiliar with the
concept. There are situations in which methods return more than one
result, in which out parameters are the way to go. However, they should
be rare in most programs, as most methods return only one result.

.



Relevant Pages

  • Re: Hayes 1200 8 & 10 Dips
    ... Do you have any reference to the Int. ... String that works best?? ... Les ...
    (alt.security.alarms)
  • Re: Calling C++ from Fortran
    ... and it was suggested I post to a Fortran ... | int FillRandom; ... You declared FillRandom with REFERENCE attribute, ... length, passed by value, immediately succeeding the string address. ...
    (comp.lang.fortran)
  • Re: StringBuilder and memory use
    ... NullReferenceException then you're using the reference after it has ... private static string jmsSBuilder(string data, string idTag, int iSeqNr) ... int hdLength = jmsSBuilder(string.Empty, sGuid, SeqNr).Length; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to understand this line of c ?
    ... (in reference to casting a string to an int) ... int 123, so I could use it in a counting loop. ...
    (comp.lang.c)
  • Re: where are isinstance types documented?
    ... On 9/26/06, Simon Brunning wrote: ... > For example isinstance(a, int) works fine but isinstance ... > doesn't - because 'string is not known'. ... using the object as a string, and to deal with exceptions arising from ...
    (comp.lang.python)