10/08/2011

What Great .NET Developers Ought To Know - Part IV

Ref:  Scott Hanselman


Senior Developers/Architects (Part II)


  • Does JITting occur per-assembly or per-method? How does this affect the working set?
A: Per-method. Since methods which are not called are not compiled at all, this reduces the working set.
  • Contrast the use of an abstract base class against an interface? 
A: If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. 

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

For more info: http://sendhil.spaces.live.com/blog/cns!30862CF919BD131A!576.entry
  • What is the difference between a.Equals(b) and a == b?
A: Cannot be answered unless you have the variable declarations for a and b.

The default implemenation for Equals on object checks for references or identity.

There is no default implementation for ‘==’ on value types.

The default implementation for ‘==’ on reference types checks for idenity or memory references

  • In the context of a comparison, what is object identity versus object equivalence?
A: Identity comparison: check if both the instances point to the same memory address.

Equivalence: two instances are considered equal if their values represented by them are equal, they can point to different memory locations. For example:
“Person p1 = new Person();p1.age = 25;Person p2 = new Person();p2.age = 25;”

Identity comparison of p1 and p2 should return false whereas equivalence should return true.
  • How would one do a deep copy in .NET?
A: Serialize / DeSerialize is an option, but it has a performance impact and required all the objects to be serializable.

ICloneable is another.

  • Explain current thinking around IClonable.
A: Because the interface contract does not specify the type of clone performed, different classes have different implementations. A consumer cannot rely on ICloneable to let them know whether an object is deep-cloned or not. May be I would use something like ICloneableEx with ShallowCopy and DeepCopy as members.
  • What is boxing?
A: Boxing is the mechanism by which a value type is converted to a reference type.  
For more info: http://sendhil.spaces.live.com/blog/cns!30862CF919BD131A!349.entry
  • Is string a value type or a reference type?
A: String is a reference type. But being immutable it gives the illusion of a value type.
  • What is the significance of the "PropertySpecified" pattern used by the XmlSerializer? What problem does it attempt to solve?
A: Nillable value types. condiitonal serialization of fields using the Property specified pattern.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlSerializationXmlSerializerClassTopic.asp
  • Why are out parameters a bad idea in .NET? Are they?
A: Out parameters can be abused however. As a matter of good programming style if you find yourself writing a method with many out parameters then you should think about refactoring your code. One possible solution is to package all the return values into a single struct.

http://msdn.microsoft.com/vcsharp/programming/language/ask/refandout/default.aspx

.Net doesn’t verify that an out parameter is set inside a method that uses an out parameter before an exception is called. This mean that you may use an uninitialized parameter without the compiler catching on to this. Use ref parameters instead.

  • Can attributes be placed on specific parameters to a method? Why is this useful?
A: Yes. Best example is MarshalAs attribute used for PInvoke.

No comments:

Post a Comment