11.4 What is the difference between an event and a delegate?
An event is just a wrapper for a multicast delegate. Adding a public event to a class is almost the same as adding a public multicast delegate field. In both cases, subscriber objects can register for notifications, and in both cases the publisher object can send notifications to the subscribers. However, a public multicast delegate has the undesirable property that external objects can invoke the delegate, something we'd normally want to restrict to the publisher. Hence events - an event adds public methods to the containing class to add and remove receivers, but does not make the invocation mechanism public.
11.5 What size is a .NET object?
Each instance of a reference type has two fields maintained by the runtime - a method table pointer and a sync block. These are 4 bytes each on a 32-bit system, making a total of 8 bytes per object overhead. Obviously the instance data for the type must be added to this to get the overall size of the object. So, for example, instances of the following class are 12 bytes each:
class MyInt
{
...
private int x;
}
However, note that with the current implementation of the CLR there seems to be a minimum object size of 12 bytes, even for classes with no data (e.g. System.Object).
Values types have no equivalent overhead.
12. .NET 2.0
12.1 What are the new features of .NET 2.0?
Generics, anonymous methods, partial classes, iterators, property visibility (separate visibility for get and set) and static classes. See http://msdn.microsoft.com/msdnmag/issues/04/05/C20/default.aspx for more information about these features.
12.2 What are the new 2.0 features useful for?
Generics are useful for writing efficient type-independent code, particularly where the types might include value types. The obvious application is container classes, and the .NET 2.0 class library includes a suite of generic container classes in the System.Collections.Generic namespace. Here's a simple example of a generic container class being used:
List
myList.Add( 10 );
Anonymous methods reduce the amount of code you have to write when using delegates, and are therefore especially useful for GUI programming. Here's an example
AppDomain.CurrentDomain.ProcessExit += delegate { Console.WriteLine("Process ending
..."); };
Partial classes is a useful feature for separating machine-generated code from hand-written code in the same class, and will therefore be heavily used by development tools such as Visual Studio.
Iterators reduce the amount of code you need to write to implement
IEnumerable/IEnumerator. Here's some sample code:
static void Main()
{
RandomEnumerator re = new RandomEnumerator( 5 );
foreach( double r in re ) Console.WriteLine( r );
Console.Read();
}
class RandomEnumerator : IEnumerable
{
public RandomEnumerator(int size) { m_size = size; }
public IEnumerator
{
Random rand = new Random();
for( int i=0; i < m_size; i++ )
yield return rand.NextDouble();
}
int m_size = 0;
}
The use of 'yield return' is rather strange at first sight. It effectively synthethises an implementation of IEnumerator, something we had to do manually in .NET 1.x.
12.3 What's the problem with .NET generics?
.NET generics work great for container classes. But what about other uses? Well, it turns out that .NET generics have a major limitation - they require the type parameter to be constrained. For example, you cannot do this:
static class Disposer
{
public static void Dispose(T obj) { obj.Dispose(); }
}
The C# compiler will refuse to compile this code, as the type T has not been constrained, and therefore only supports the methods of System.Object. Dispose is not a method on System.Object, so the compilation fails. To fix this code, we need to add a where clause, to reassure the compiler that our type T does indeed have a Dispose method
static class Disposer
{
public static void Dispose(T obj) { obj.Dispose(); }
}
The problem is that the requirement for explicit contraints is very limiting. We can use constraints to say that T implements a particular interface, but we can't dilute that to simply say that T implements a particular method. Contrast this with C++ templates (for example), where no constraint at all is required - it is assumed (and verified at compile time) that if the code
invokes the Dispose() method on a type, then the type will support the method.
In fact, after writing generic code with interface constraints, we quickly see that we haven't gained much over non-generic interface-based programming. For example, we can easily rewrite the Disposer class without generics:
static class Disposer
{
public static void Dispose( IDisposable obj ) { obj.Dispose(); }
}
12.4 What's new in the .NET 2.0 class library?
Here is a selection of new features in the .NET 2.0 class library (beta 1): Generic collections in the System.Collections.Generic namespace.
The System.Nullable
The GZipStream and DeflateStream classes in the
System.IO.Compression namespace.
The Semaphore class in the System.Threading namespace.
Wrappers for DPAPI in the form of the ProtectedData and ProtectedMemory classes in the System.Security.Cryptography namespace.
The IPC remoting channel in the System.Runtime.Remoting.Channels.Ipc namespace, for optimised intra-machine communication.
1) The C# keyword .int. maps to which .NET type?
1. System.Int16
2. System.Int32
3. System.Int64
4. System.Int128
2) Which of these string definitions will prevent escaping on backslashes in C#?
1. string s = #.n Test string.;
2. string s = ..n Test string.;
3. string s = @.n Test string.;
4. string s = .n Test string.;
3) Which of these statements correctly declares a two-dimensional array in C#?
1. int[,] myArray;
2. int[][] myArray;
3. int[2] myArray;
4. System.Array[2] myArray;
4) If a method is marked as protected internal who can access it?
1. Classes that are both in the same assembly and derived from the declaring class.
2. Only methods that are in the same class as the method in question.
3. Internal methods can be only be called using reflection.
4. Classes within the same assembly, and classes derived from the declaring class.
5) What is boxing?
a) Encapsulating an object in a value type.
b) Encapsulating a copy of an object in a value type. c) Encapsulating a value type in an object.
d) Encapsulating a copy of a value type in an object.
6) What compiler switch creates an xml file from the xml comments in the files in an assembly?
1. /text
2. /doc
3. /xml
4. /help
7) What is a satellite Assembly?
1. A peripheral assembly designed to monitor permissions requests from an application.
2. Any DLL file used by an EXE file.
3. An assembly containing localized resources for another assembly.
4. An assembly designed to alter the appearance or .skin. of an
application.
8) What is a delegate?
1. A strongly typed function pointer.
2. A light weight thread or process that can call a single method.
3. A reference to an object in a different process.
4. An inter-process message channel.
9) How does assembly versioning in .NET prevent DLL Hell?
1. The runtime checks to see that only one version of an assembly is on the machine at any one time.
2. .NET allows assemblies to specify the name AND the version of any assemblies they need to run.
3. The compiler offers compile time checking for backward compatibility.
4. It doesn.t.
10) Which .Gang of Four. design pattern is shown below?
public class A { private A instance; private A() {
}
public
static A Instance {
get {
if ( A == null )
A = new A();
return instance;
}
}
}
1. Factory
2. Abstract Factory
3. Singleton
4. Builder
11) In the NUnit test framework, which attribute must adorn a test class in order for it to be picked up by the NUnit GUI?
1. TestAttribute
2. TestClassAttribute
3. TestFixtureAttribute
4. NUnitTestClassAttribute
12) Which of the following operations can you NOT perform on an
ADO.NET DataSet?
1. A DataSet can be synchronised with the database.
2. A DataSet can be synchronised with a RecordSet.
3. A DataSet can be converted to XML.
4. You can infer the schema from a DataSet.
13) In Object Oriented Programming, how would you describe encapsulation?
1. The conversion of one type of object to another.
2. The runtime resolution of method calls.
3. The exposition of data.
4. The separation of interface and implementation.
No comments:
Post a Comment