Thursday, October 23, 2008

The C# Tutorial

Lesson 10


by Joe Mayo, 02/10/01

Lesson 10: Properties

This lesson teaches C# Properties. Our objectives are as follows:

Understand What Properties Are For.
Implement a Property.
Create a Read-Only Property.
Create a Write-Only Property.

Properties are a new language feature introduced with C#. They provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field. To get an appreciation for what properties accomplish, let's take a look at how to provide field encapsulation by traditional methods.

Listing 10-1. An Example of Traditional Class Field Access: Accessors.cs

using System;

public
class PropertyHolder
{
private int someProperty = 0;

public int getSomeProperty()
{
return someProperty;
}

public
void setSomeProperty(int propValue)
{
someProperty = propValue;
}

}

public
class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold =
new PropertyHolder();

propHold.setSomeProperty(5);

Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());

return 0;
}
}

Listing 10-1 shows the traditional method of accessing class fields. The PropertyHolder class has the field we're interested in accessing. It has two methods, getSomeProperty and setSomeProperty. The getSomeProperty method returns the value of the someProperty field. The setSomeProperty method sets the value of the someProperty field.

The PropertyTester class uses the methods of the PropertyHolder class to get the value of the someProperty field in the PropertyHolder class. The Main method instantiates a new PropertyHolder object, propHold. Next it sets the someMethod of propHold to the value 5 by using the setSomeProperty method. Then the program prints out the property value with a Console.WriteLine method call. The argument used to obtain the value of the property is a call to the getSomeProperty method of the propHold object. It prints out "Property Value: 5" to the console.

This method of accessing information in a field has been good because it supports the object-oriented concept of encapsulation. If the implementation of someProperty changed from an int type to a byte type, this would still work. Now the same thing can be accomplished much smoother with properties.

Listing 10-2. Accessing Class Fields With Properties: Properties.cs

using System;

public class PropertyHolder
{
private int someProperty = 0;

public int SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty =
value;
}
}
}

public
class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold =
new PropertyHolder();

propHold.SomeProperty = 5;

Console.WriteLine("Property Value: {0}", propHold.SomeProperty);

return 0;
}
}

Listing 10-2 shows how to create and use a property. The PropertyHolder class has the "SomeProperty" property implementation. Notice that the first letter of the first word is capitalized. That's the only difference between the names of the property "SomeProperty" and the field "someProperty". The property has two accessors, get and set. The get accessor returns the value of the someProperty field. The set accessor sets the value of the someProperty field with the contents of "value". The "value" shown in the set accessor is a C# reserved word. It's normally an error to use the "value" keyword in any other context.

The PropertyTester class uses the SomeProperty property in the PropertyHolder class. The first line of the Main method creates a PropertyHolder object named propHold. Next the value of the someProperty field of the propHold object is set to 5 by using the SomeProperty property. It's that simple -- just assign the value to the property as if it were a field.

After that, the Console.WriteLine method prints the value of the someProperty field of the propHold object. It does this by using the SomeProperty property of the propHold object. Again, it's that simple -- just use the property as if it were a field itself.

Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation.

Listing 10-3. Read-Only Property: ReadOnlyProperty.cs

using System;

public class PropertyHolder
{
private int someProperty = 0;

public PropertyHolder(int propVal)
{
someProperty = propVal;
}


public
int SomeProperty

{
get
{
return someProperty;
}
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold =
new PropertyHolder(5);

Console.WriteLine("Property Value: {0}", propHold.SomeProperty);

return 0;
}
}

Listing 10-3 shows how to implement a read-only property. The PropertyHolder class has a SomeProperty property that only implements a get accessor. It leaves out the set accessor. This particular PropertyHolder class has a constructor which accepts an integer parameter.

The Main method of the PropertyTester class creates a new PropertyHolder object named propHold. The instantiation of the propHold object uses the constructor of the PropertyHolder that takes an int parameter. In this case, it's set to 5. This initializes the someProperty field of the propHold object to 5.

Since the SomeProperty property of the PropertyHolder class is read-only, there is no other way to set the value of the someProperty field. If you inserted "propHold.SomeProperty = 7" into the listing, the program would not compile, because SomeProperty is read-only. When the SomeProperty property is used in the Console.WriteLine method, it works fine. This is because it's a read operation which only invokes the get accessor of the SomeProperty property.

Listing 10-4. Write-Only Property: WriteOnlyProperty.cs

using System;

public class PropertyHolder
{
private int someProperty = 0;

public int SomeProperty
{
set
{
someProperty =
value;

Console.WriteLine("someProperty is equal to {0}", someProperty);
}
}
}

public class PropertyTester
{
public static int Main(string[] args)
{
PropertyHolder propHold =
new PropertyHolder();

propHold.SomeProperty = 5;

return 0;
}
}

Listing 10-4 shows how to create and use a write-only property. This time the get accessor is removed from the SomeProperty property of the PropertyHolder class. The set accessor has been added, with a bit more logic. It prints out the value of the someProperty field after it's been modified.

The Main method of the PropertyTester class instantiates the PropertyTester class with a default constructor. Then it uses the SomeProperty property of the propHold object to set the someProperty field of the propHold object to 5. This invokes the set accessor of the propHold object, which sets the value of it's someProperty field to 5 and then prints "someProperty is equal to 5" to the console.

In summary, you now know what properties are for and how they're used. You have a feeling for the difference between using properties and traditional techniques using class methods. Properties can be made read-only or write-only and you know how to implement each type.

I invite you to return for Lesson 11: Indexers.

Your feedback is very important and I appreciate any constructive contributions you have. Please feel free to contact me for any questions or comments you may have about this lesson

C# Frequently Asked Questions for C++ programmers

Andy Mc's C# FAQ for C++ programmers

1. Introduction

1.1 What is C#?

C# is a programming language designed by Microsoft. It is loosely based on C/C++, and bears a striking similarity to Java in many ways. Microsoft describe C# as follows:

"C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. C# (pronounced 'C sharp') is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++."

1.2 When will C# be released?

No firm date yet, but it is expected in the second half of 2001.

1.3 How do I develop C# apps?

The .NET SDK contains the C# command-line compiler (csc.exe). The next version of Visual Studio (called Visual Studio 7 or Visual Studio.NET) will have fully integrated support for C# development.

1.4 Where can I download the .NET SDK & Visual Studio 7?

You can download Beta 1 of the SDK from http://msdn.microsoft.com/net. If you are an MSDN Universal subscriber, you can also download Beta 1 of Visual Studio 7.

1.5 Does C# replace Java?

C# is a very Java-like language - the core of both languages have similar advantages and limitations when compared to C++. For example, both languages have garbage collection, but neither language has templates. Microsoft have ceased production of Visual J++, so it's hard not to view C# as Microsoft's alternative to Java.

1.6 Does C# replace C++?

The obvious answer is no. However it's difficult to see C++ as the best choice for new .NET code. For the .NET runtime to function fully, it requires the programming language to conform to certain rules - one of these rules is that language types must conform to the Common Type System (CTS). Unfortunately many C++ features are not supported by the CTS - for example multiple inheritance of classes and templates.

Microsoft's answer to this problem is to offer Managed Extensions (ME) for C++, which allows you to write C++ that conforms to the CTS. New keywords are provided to mark your C++ classes with CTS attributes (e.g. __gc for garbage collection). However, it's difficult to see why ME C++ would be chosen over C# for new projects. In terms of features they are very similar, but unlike C++, C# has been designed from the ground-up to work seamlessly with the .NET environment. The raison d'etre for ME C++ would therefore appear to be porting existing C++ code to the .NET environment.

So, in answer to the question, my suspicion is that C++ will remain an important language outside of the .NET environment, and will be used (via ME) to port existing code to .NET, but I think C# will become the language of choice for one-time C++ developers developing new .NET applications. But only time will tell ...

1.7 What does a simple C# program look like?

Sorry, my imagination has deserted me. Yes, you guessed it, here comes 'Hello, world' ...

class CApplication { 	public static void Main()  	{ 		System.Console.Write( "Hello, new .NET world." ); 	} } 

(No, you can't put Main() as a global function - global functions don't exist in C#.)

1.8 Is C# object-oriented?

Yes, C# is an OO language in the tradition of Java and C++.

1.9 Does C# have its own class library?

Not exactly. In common with all .NET languages (e.g. VisualBasic.NET, JScript.NET) C# has access to the .NET class library. C# does not have its own class library.

2. Basic types

2.1 What standard types does C# supply?

C# supports a very similar range of basic types to C++, including int, long, float, double, char, string, arrays, structs and classes. However, don't assume too much. The names may be familiar, but some of the details are different. For example, a long is 64 bits in C#, whereas in C++ the size of a long depends on the platform (typically 32 bits on a 32-bit platform, 64 bits on a 64-bit platform). Also classes and structs are almost the same in C++ - this is not true for C#.

2.2 Is it true that all C# types derive from a common base class?

Yes and no. All types can be treated as if they derive from object (System.Object), but in order to treat an instance of a value type (e.g. int, float) as object-derived, the instance must be converted to a reference type using a process called 'boxing'. In theory a developer can forget about this and let the run-time worry about when the conversion is necessary, but in reality this implicit conversion can have side-effects that may trip up the unwary.

2.3 So this means I can pass an instance of a value type to a method that takes an object as a parameter?

Yes. For example:

class CApplication { 	public static void Main() 	{  		int x = 25;  		string s = "fred";  		 		DisplayMe( x );  		DisplayMe( s );  	}  	 	static void DisplayMe( object o )  	{  		System.Console.WriteLine( "You are {0}", o );  	} }  

This would display:

You are 25 You are fred 


2.4 What are the fundamental differences between value types and reference types?

C# divides types into two categories - value types and reference types. Most of the basic intrinsic types (e.g. int, char) are value types. Structs are also value types. Reference types include classes, interfaces, arrays and strings. The basic idea is straightforward - an instance of a value type represents the actual data (stored on the stack), whereas an instance of a reference type represents a pointer or reference to the data (stored on the heap).

The most confusing aspect of this for C++ developers is that C# has predetermined which types will be represented as values, and which will be represented as references. A C++ developer expects to take responsibility for this decision.

For example, in C++ we can do this:

int x1 = 3;		// x1 is a value on the stack int *x2 = new int(3)	// x2 is a reference to a value on the heap 

but in C# there is no control:

int x1 = 3;		// x1 is a value on the stack int x2 = new int();  x2 = 3;		// x2 is also a value on the stack! 

2.5 Okay, so an int is a value type, and a class is a reference type. How can int be derived from object?

It isn't, really. When an int is being used as an int, it is a value (on the stack). However, when it is being used as an object, it is a reference to an integer value on the heap. In other words, when you treat an int as an object, the runtime automatically converts the int value to an object reference. This process is called boxing. The conversion involves copying the contents of the int from the stack to the heap, and creating an object instance which refers to it. Unboxing is the reverse process - the object is converted back to a stack-based value.

int x = 3;		// new int value 3 on the stack object objx = x;	// new int on heap, set to value 3 - still have x=3 on stack int y = (int)objx;	// new value 3 on stack, still got x=3 on stack and objx=3 on heap 

2.6 C# uses references instead of pointers. Are C# references the same as C++ references?

Not quite. The basic idea is the same, but one significant difference is that C# references can be null. So you cannot rely on a C# reference pointing to a valid object. If you try to use a null reference, a NullReferenceException is thrown.

For example, look at the following method:

void displayStringLength( string s ) { 	Console.WriteLine( "String is length {0}", s.Length ); }		 

The problem with this method is that it will throw a NullReferenceException if called like this:

string s = null; displayStringLength( s ); 

Of course for some situations you may deem a NullReferenceException to be a perfectly acceptable outcome, but in this case it might be better to re-write the method like this:

void displayStringLength( string s ) { 	if( s == null ) 		Console.WriteLine( "String is null" ); 	else 		Console.WriteLine( "String is length {0}", s.Length ); }		 

3. Classes and structs

3.1 Structs are largely redundant in C++. Why does C# have them?

In C++, a struct and a class are pretty much the same thing. The only difference is the default visibility level (public for structs, private for classes). However, In C# structs and classes are very different. In C#, structs are value types (stored on the stack), whereas classes are reference types (stored on the heap). Also structs cannot inherit from structs or classes, though they can implement interfaces. Structs cannot have destructors.

3.2 Does C# support multiple inheritance (MI)?

C# supports multiple inheritance of interfaces, but not of classes.

3.3 Is a C# interface the same as a C++ abstract class?

No, not quite. An abstract class in C++ cannot be instantiated, but it can (and often does) contain implementation code and/or data members. A C# interface cannot contain any implementation code or data members - it is simply a group of method names & signatures. A C# interface is more like a COM interface than a C++ abstract class.

The other major difference is that a C# class can inherit from only one class (abstract or not), but can implement multiple interfaces.

3.4 Are C# constructors the same as C++ constructors?

Very similar.

3.5 Are C# destructors the same as C++ destructors?

No! They look the same but they're very different. First of all, a C# destructor isn't guaranteed to be called at any particular time. In fact it's not guaranteed to be called at all. Truth be told, a C# destructor is really just a Finalize method in disguise. In particular, it is a Finalize method with a call to the base class Finalize method inserted. So this:

class CTest { 	~CTest() 	{ 		System.Console.WriteLine( "Bye bye" ); 	} } 

is really this:

class CTest { 	protected override void Finalize()  	{ 		System.Console.WriteLine( "Bye bye" );  		base.Finalize(); 	} } 

If you don't believe me, try putting a Finalize method and a destructor in a C# class and watch the compiler sheepishly confess its deceit.

3.6 If C# destructors are so different to C++ destructors, why did MS use the same syntax?

Because they're evil, and they want to mess with your mind.

3.7 What is a static constructor?

A constructor for a class, rather than instances of a class. The static constructor is called when the class is loaded.

3.8 Are all methods virtual in C#?

No. Like C++, methods are non-virtual by default, but can be marked as virtual.

3.9 How do I declare a pure virtual function in C#?

Use the abstract modifier on the method. The class must also be marked as abstract (naturally). Note that abstract methods cannot have an implementation (unlike pure virtual C++ methods).

4. Exceptions

4.1 Can I use exceptions in C#?

Yes, in fact exceptions are the recommended error-handling mechanism in C# (and in .NET in general). Most of the .NET framework classes use exceptions to signal errors.

4.2 What types of object can I throw as exceptions?

Only instances of the System.Exception classes, or classes derived from System.Exception. This is in sharp contrast with C++ where instances of almost any type can be thrown.

4.3 Can I define my own exceptions?

Yes, as long as you follow the rule that exceptions derive from System.Exception. More specifically, MS recommend that user-defined exceptions inherit from System.ApplicationException (which is derived from System.Exception).

4.4 Are there any standard exceptions that I can re-use?

Yes, and some of them loosely correspond to standard COM HRESULTs. The table below shows a mapping from HRESULTs to .NET (and therefore C#) exceptions:

HRESULT .NET EXCEPTION
E_INVALIDARG ArgumentOutOfRangeException, or the more general ArgumentException. Alternatively FormatException if a supplied parameter is not the correct format - e.g. a URL is specified as htp:// instead of http://
E_POINTER ArgumentNullException
E_NOTIMPL NotImplementedException
E_UNEXPECTED Some may consider InvalidOperationException to be equivalent. InvalidOperationException is normally used to indicate that an object cannot perform the requested operation because it is not in a suitable state to do so.
E_OUTOFMEMORY OutOfMemoryException

Other standard exceptions that you might want to re-use are IndexOutOfRangeException and ArithmeticException .

4.5 Does the System.Exception class have any cool features?

Yes - the feature which stands out is the StackTrace property. This provides a call stack which records where the exception was thrown from. For example, the following code:

using System;  class CApp  {   	public static void Main()  	{  		try  		{  			f();  		}  		catch( Exception e )  		{  			Console.WriteLine( "System.Exception stack trace = \n{0}", e.StackTrace );  		} 	}   	static void f()  	{  		throw new Exception( "f went pear-shaped" );  	}  } 

produces this output:

System.Exception stack trace =  	at CApp.f()  	at CApp.Main() 

Note, however, that this stack trace was produced from a debug build. A release build may optimise away some of the method calls which could mean that the call stack isn't quite what you expect.

4.6 When should I throw an exception?

This is the subject of some debate, and is partly a matter of taste. However, it is accepted by many that exceptions should be thrown only when an 'unexpected' error occurs. How do you decide if an error is expected or unexpected? This is a judgement call, but a straightforward example of an expected error is failing to read from a file because the seek pointer is at the end of the file, whereas an example of an unexpected error is failing to allocate memory from the heap.

4.7 Does C# have a 'throws' clause?

No, unlike Java, C# does not require (or even allow) the developer to specify the exceptions that a method can throw.

5. Run-time type information

5.1 How can I check the type of an object at runtime?

You can use the is keyword. For example:

using System;   class CApp { 	public static void Main() 	{  		string s = "fred";  		long i = 10;   		Console.WriteLine( "{0} is {1}an integer", s, (IsInteger(s) ? "" : "not ") );  		Console.WriteLine( "{0} is {1}an integer", i, (IsInteger(i) ? "" : "not ") );  	} 	 	static bool IsInteger( object obj ) 	{  		if( obj is int || obj is long ) 			return true;  		else  			return false; 	} }  

produces the output:

fred is not an integer  10 is an integer 

5.2 Can I get the name of a type at runtime?

Yes, use the GetType method of the object class (which all types inherit from). For example:

using System;   class CTest { 	class CApp  	{ 		public static void Main() 		{  			long i = 10;  			CTest ctest = new CTest();   			DisplayTypeInfo( ctest );  			DisplayTypeInfo( i );  		} 		 		static void DisplayTypeInfo( object obj )  		{  			Console.WriteLine( "Type name = {0}, full type name = {1}", obj.GetType(), obj.GetType().FullName );  		} 	} } 

produces the following output:

Type name = CTest, full type name = CTest  Type name = Int64, full type name = System.Int64 

6. Advanced language features

6.1 What are delegates?

A delegate is a class derived from System.Delegate. However the language has a special syntax for declaring delegates which means that they don't look like classes. A delegate represents a method with a particular signature. An instance of a delegate represents a method with a particular signature on a particular object (or class in the case of a static method). For example:

using System; delegate void Stereotype();  class CAmerican  { 	public void BePatriotic()  	{  		Console.WriteLine( "...  ... God bless America."); 	} }   class CBrit { 	public void BeXenophobic() 	{ 		Console.WriteLine( "Bloody foreigners ... " ); 	} }  class CApplication {  	public static void RevealYourStereotype( Stereotype[] stereotypes ) 	{  		foreach( Stereotype s in stereotypes )  			s(); 	} 	 	public static void Main()  	{ 		CAmerican chuck = new CAmerican();  		CBrit edward = new CBrit();   		// Create our list of sterotypes. 		Stereotype[] stereotypes = new Stereotype[2]; 		stereotypes[0] = new Stereotype( chuck.BePatriotic ); 		stereotypes[1] = new Stereotype( edward.BeXenophobic ); 		 		// Reveal yourselves! 		RevealYourStereotype(stereotypes );  	}  } 

This produces the following result:

... ... God bless America. Bloody foreigners ... 

6.2 Are delegates just like interfaces with a single method?

Conceptually delegates can be used in a similar way to an interface with a single method. The main practical difference is that with an interface the method name is fixed, whereas with a delegate only the signature is fixed - the method name can be different, as shown in the example above.

6.3 What is the C# equivalent of QueryInterface?

The as keyword. For example:

using System;   interface IPerson {  	string GetName();  }  interface IPerson2 : IPerson  {  	int GetAge();  }   class CPerson : IPerson  {  	public CPerson( string name ) 	{ 		m_name = name; 	}  	 	// IPerson  	public string GetName() 	{ 		return m_name; 	} 	  	private string m_name;  }  class CPerson2 : IPerson2 {  	public CPerson2( string name, int age )  	{  		m_name = name;  		m_age = age;  	} 	 	// IPerson2  	public string GetName() { return m_name; }  	public int GetAge() { return m_age; }  	private string m_name; private int m_age; }   public class CApp  { 	public static void Main() 	{  		CPerson bob = new CPerson( "Bob" );  		CPerson2 sheila = new CPerson2( "Sheila", 24 );  		 		DisplayAge( bob ); 		DisplayAge( sheila );  	} 	 	static void DisplayAge( IPerson person ) 	{ 		IPerson2 person2 = person as IPerson2;   // QueryInterface lives on !!!  		if( person2 != null ) 			Console.WriteLine( "{0} is {1} years old.", person2.GetName(), person2.GetAge() );  		else  			Console.WriteLine( "Sorry, don't know {0}'s age.", person.GetName() );  	}  } 

Running the program produces the following output:

Sorry, don't know Bob's age. Sheila is 24 years old. 

7. It doesn't work like that in C++ ...

7.1 I 'new'-ed an object, but how do I delete it?

You can't. You are not allowed to call the destructor explicitly, and no delete operator is provided. Don't worry, the garbage collector will destroy your object .... eventually .... probably .... :-)

7.2 I tried to create an object on the stack, but the C# compiler complained. What's going on?

Unlike C++, you cannot create instances of classes on the stack. Class instances always live on the heap and are managed by the garbage collector.

7.3 I defined a destructor, but it never gets called. Why?

A C# destructor is really just an implementation of Finalize, and the runtime doesn't guarantee to call Finalize methods. In particular, the runtime doesn't usually bother calling Finalize methods for objects that are still alive when you exit an application. However, you can politely ask it to reconsider that approach by calling the GC.RequestFinalizeOnShutdown() method.

7.4 Most of the C# basic types have the same names as C++ basic types? Are they the same?

No. A char in C# is equivalent to a wchar_t in C++. All characters (and strings, obviously) are Unicode in C#. Integral values in C# are concrete sizes, unlike C++ (where size depends on processor). For example, a C# int is 32 bits, whereas a C++ int is normally 32 bits on a 32-bit processor and 64 bits on a 64-bit processor. A C# long is 64 bits.

8. Miscellaneous

8.1 String comparisons using == seem to be case-sensitive? How do I do a case-insensitive string comparison?

Use the String.Compare function. Its third parameter is a boolean which specifies whether case should be ignored or not.

"fred" == "Fred"	// false System.String.Compare( "fred", "Fred", true )	// true 

8.2 I've seen some string literals which use the @ symbol, and some which don't. What's that all about?

The @ symbol before a string literal means that escape sequences are ignored. This is particularly useful for file names, e.g.

string fileName = "c:\\temp\\test.txt" 

versus:

string fileName = @"c:\temp\test.txt" 

8.3 Does C# support a variable number of arguments?

Yes, using the params keyword. The arguments are specified as a list of arguments of a specific type, e.g. int. For ultimate flexibility, the type can be object. The standard example of a method which uses this approach is System.Console.WriteLine().

8.4 How can I process command-line arguments?

Like this:

using System;  class CApp { 	public static void Main( string[] args ) 	{ 		Console.WriteLine( "You passed the following arguments:" ); 		foreach( string arg in args ) 			Console.WriteLine( arg ); 	} } 

8.5 Does C# do array bounds checking?

Yes. An IndexOutOfRange exception is used to signal an error.

8.6 How can I make sure my C# classes will interoperate with other .NET languages?

Make sure your C# code conforms to the Common Language Subset (CLS). To help with this, add the [assembly:CLSCompliant(true)] global attribute to your C# source files. The compiler will emit an error if you use a C# feature which is not CLS-compliant.

9. Resources

9.1 Where can I find out more about C#?

The Microsoft .NET homepage is at http://www.microsoft.com/net/

Robert Scoble has compiled an extremely comprehensive list of .NET on-line resources at http://www.devx.com/dotnet/resources/

There is a very snazzy site dedicated to C# at http://csharpindex.com/

9.2 Sample code and utilities

Peter Drayton has some .NET utilities (with C# source) at http://www.razorsoft.net/

FileShare Server / Client

Welcome to Learn C# - the easy way , by Saurabh Nandu.


I was on a look out for some concrete example on Sockets. I found a very good example of a POP and SMTP server written by my friend Pramod Singh. I have used the Logic from his example to Build this Example. Thank You Pramod !! This program is sort of a Peer to Peer Application where there is transfer of files between two peers.
This example uses the "System.Net.Sockets" Namespace to create a Client / Server. The server acts as a File Sharing Server, it listens for multiple clients to connect to it. The Clients have a option to either Download any of the Files available with the server for download. Also the Clients can Upload files to the Server (if the Server allows it).
The example uses a Command based talking between them i.e. The Server sends a Command to the Client , the Client decodes the Command and acts according to the command. If the Client Sends a Command, the Server acts according to the command sent by the server. Note all command I have used here have 4 characters and in Capital letters.

For Example this is the procedure of connection:
1) Server starts to listen on the given port.
2) Client connects to the Server on the given port.
3) Server sends a "CONN Welcome to File Share Server" message.
4) The Client decodes the message and sends a "USER guest" message to the server.
5) Server reads the User Name adds it to the User ListBox and Sends
"LIST c:\FileShare\server\download\mydown.txt@c:\FileShare\server\download\myfile.exe"
i.e. a list of al the files it has for download.
6) The client updates its Download File ListBox with the List of files sent by the server, and exists the Thread.

Steps Occurring while Downloading File by the Client
1) Client Sends a "DOWN myfile.exe" command to the server.
2) The Server has 2 options here
a) If File Name is Valid an Server permits Download then the server sends
"SIZE 35346" (the SIZE command with the size of the File Requested for download.
b) If the File Name is incorrect or the Server does not allow downloading the server sends
"NOPE FileNotFound" or "NOPE Download not Allowed!" .
3) If the NOPE command is Received then the client sends a "OHHH No Problem"
4) If the SIZE command is received then the client sends a "SEND myfile.exe" to indicate the server to start sending the file.
5) On Receiving a "SEND" command the Server opens a stream to the File and sends the File over the socket to the client.
6) After successful receipt of file the server sends a "RECD File!!" command.

Steps Occurring while Uploading File to the Server
1) Client sends a "UPFL myUpload.txt@65345" i.e. it sends the File Name and the Size of the File .
2) If the Server does not allow Upload it sends a "NOPE Server upload Disabled" ;
3) Otherwise a "SEND File" command is sent.
4) On getting a "SEND" command the client opens a stream to the file and sends it over the Socket to the Server.
5) After successful receipt of file the server sends a "RECD File Received Properly" command to the Client.

Execution :
1) Run the FileShareServer.exe first to start the server .(The server by default listens for connection on port 4455).
2) Run the FileShareClient.exe which will connect to the server.

.NET FAQ

In this section I will try to answer some of the most Basic question that may arise in the Minds of New Developers about the .NET Platform. Please remember that these answers are my Interpretations of the .NET platform and may be not be exactly right.

1) .NET Platform FAQ

1 What is .NET Platform?

According to Microsoft, .NET is a "revolutionary new platform, built on open Internet protocols and standards, with tools and services that meld computing and communications in new ways".
A simpler way of saying that would be that the .NET Platform is a new Runtime Environment for developing and running applications which are easier to build, deploy and scale. Also it features a a Common Language Specification which helps to solve the interoperability issue. Also components written in one programming language can be used by other programming language.

2 How do I develop in .NET ?

Get the .NET SDK from Microsoft. The .NET SDK contains command-line compilers and utilities which can be used to build .NET apps. Visual Studio (called Visual Studio 7 or Visual Studio.NET) also is fully supportive to .NET development.

3 Where can I download the .NET SDK & Visual Studio 7?

You can download the .NET SDK from http://msdn.microsoft.com/net (110Mb). You can also order CD's of Visual Studio 7 beta from Microsoft (Visual Studio 7 contains the .NET SDK).

4 What are the key technologies within .NET?

ASP.NET, CLR (Common Language Runtime), C# (a new Java-like language), Visual Basic .NET, Managed C++, Visual Foxpro .NET, SOAP, XML, ADO+, multi-language support (Eiffel, COBOL etc)

5 What platforms does .NET run on?

This version of .NET runs on Win95/98/ME & Windows NT/2000. Please remember to install Personal Web Server on Win95/98/ME or IIS 5.0 on WinNT/2000 which is needed to run ASP.NET before installing the .NET SDK.

6 What languages does .NET support?

At present MS provides compilers for C#, C++, VB and JScript. Other vendors have announced that they intend to develop .NET compilers for languages such as COBOL, Eiffel, Perl and Python.

2 C# FAQ

1 What is C# ?

C# can be defined as, "C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. C# (pronounced C sharp) is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++."

2 Is C# C+++ ( a new version of C++)?

C# is not a upgrade to C++. C# is a totally new Object oriented programming language. You could say its much more easier and Object oriented than C/ C++.

3 Does C# support RAD like Visual Basic ?

Yep C# is a RAD (Rapid Application Development) Language like Visual Basic. It supports the Drag- Drop and Build features of Visual Basic.

4 Where can I get C# Compiler ?

The C# compiler is not distributed as a separate entity yet. You will need to download the .NET SDK (110 Mb) from http://msdn.microsoft.com/net . The SDK contains the C#, VB.NET, JScript Compilers and full documentation and its Free.

5 How can I start learning C# ?

First download the .NET SDK. The SDK contains compilers, documentation, code samples and Quick start pages to help you learn faster. You can also visit many web sites which provide tutorials and articles on .NET.

6 Where can I get information on Books on C#?

Check out http://www.dotnetbooks.com/ they have a listing of all books available for the .NET platform.

7 Is C# easy to learn ?

If you are a Java programmer then you will find it very easy to migrate to C# (I did migrate from Java to C#). Also if you are a C / C++ programmer then you will find much similar syntax of C#.

8 Is C# platform Independent ?

The answer to this is a bit tactical. C# applications cannot run without the .NET runtime. As present the .NET platform has been released for Win95/98/ME and Win NT/2000. MS plans to release the .NET runtime for other platforms soon . Now if a .NET runtime has been released for your platform then all the .NET programs will run on your platform. This is quite similar to the working of SUN Java which is considered to be Platform Independent. In the case of Sun Java you need to install a Java Virtual Machine on your Platform to run Java programs. If your platform does not have a Java VM then it cannot run Java programs. Hence you can consider the concept Java VM similar to the .NET Runtime Environment (not literally ).

9 I have written programs in C# how can I run it on my Clients Machine ?

Till the time a separate runtime environment is released for .NET, you will have to install the Full .NET SDK on every machine you can to run you programs.

10 Is C# better than Java ?

I will not fall in the heated debate that's going on on this topic. You will find many sites which have discussion boards running for this debate. I will just like to add that Microsoft Products has a very big market, and MS will surely do all it can to promote its Feature language C#. Thus there surely will be a demand for C# programmers, now it depends on you to use Java or C#.

11 Is C# Object Oriented ?

Yes, C# is a fully Object Oriented Programming Language.

3 ASP.NET

1 Is ASP.NET and ASP+ the same ?

Yep ASP+ and ASP.NET are the same thing. First ASP.NET was called ASP+ since its a upgrade to ASP. Its also called ASP.NET since its a part of the .NET platform.

2 Is ASP.NET a upgrade to ASP ?

Yes again, ASP.NET is a major upgrade over ASP. It does have backward support of ASP, but ASP.NET is better managed, and well developed.

3 Should all ASP programmers learn ASP.NET ?

This depends on what kind of ASP programmer you are. One thing is sure that ASP.NET will overtake ASP in the coming years, but it will at least take a year for that to happen since to run ASP.NET you need the .NET platform installed on the server. Till that time ASP will surely be a more popular choice.

4 Does ASP.NET need the .NET Platform ?

Since ASP.NET is part of .NET platform you have to install the .NET SDK first till a separate runtime is released .

5 I am a Web Server Admin, How do I install support for ASP.NET on my server ?

You will have to install the .NET SDK. The SDK contains all the code to convert your server to ASP.NET. Just remember that the .NET SDK is it its beta stage and can affect your Web Servers performance.

Introduction to C#

C# (pronounced C-Sharp, just like in musical notation) is a new language for Windows applications, intended as an alternative to the main previous languages, C++ and VB. Its purpose is twofold:
  1. It gives access to many of the facilities previously available only in C++, while retaining some of the simplicity to learn of VB.
  2. It has been designed specifically with the .NET framework in mind, and hence is very well structured for writing code that will be compiled for .NET.

Where Does C# Fit In?

In programming language terms, if you start from the lowest level of language available to Windows developer and move up, you'd see something like this:

» Assembly Language
» C, C++
» C#, J++
» VB
» VBA, scripting languages

As you move down the list you get to languages that are considered progressively easier to learn and quicker to develop code in, but which leads to less efficient code. Thus, assembly language will give you absolute top-performance code, but it's so hard to write and debug that you'll only really find it used in things like selected subroutines in some high-performance games, and some software that controls hardware devices where performance is critical. For most applications requiring good performance, C++ has traditionally been the language of choice, while if you're developing GUI applications and want them written quickly, you'll probably opt for VB. Performance isn't really important for GUI applications because the limiting factor tends to be how fast the user can click the mouse!

C# is designed to fill a perceived gap between VB and C++. It's aimed at developers who would like to be able to write code more easily than is possible with C++, but without the overhead of VB. (A gap that J++ seems to have largely failed to satisfy - although it has to be said that there are a lot of similarities between C# and J++). C# has a syntax that is very similar to C++, and supports many C++ language features, for example inheritance (though not multiple inheritance) of classes. For this reason C++ programmers are likely to find C# easy to learn. By contrast, I suspect for VB programmers, it's likely to be only marginally easier to learn C# than to learn C++. C# is also heavily integrated into COM: C# classes are automatically COM objects. (If you don't want that overhead, you can declare your class as a struct instead).

A good example of the way C# steers a middle ground between C++ and VB is on the question of memory management. In VB, the only memory management you ever need to do is remember to Set your objects to Nothing when you've finished with them - and the only penalty if you forget to do this is system resources and memory may be used for slightly longer than necessary. On the other hand, C++ not only gives developers the chance to fine tune exactly what variables occupy memory for how long (thus potentially leading to highly efficient memory usage) - it largely requires them to do so. Which means C++ programs are prone to memory management bugs, often leading to memory leaks, in a way that can't really happen in VB.

C# mostly takes the VB path on memory management - the language itself takes responsibility for handling it. But unlike VB, in C# if you want to sort out the memory yourself, you can opt to do so by declaring a section of your code as 'unsafe', and then allocating and deallocating memory in the same way as is done in C++.

Thursday, July 17, 2008

C# developer interview questions

Is it possible to inline assembly or IL in C# code? - No.

Is it possible to have different access modifiers on the get/set methods of a property? - No. The access modifier on a property applies to both its get and set accessors. What you need to do if you want them to be different is make the property read-only (by only providing a get accessor) and create a private/internal set method that is separate from the property.

Is it possible to have a static indexer in C#? - No. Static indexers are not allowed in C#.

If I return out of a try/finally in C#, does the code in the finally-clause run?

Yes. The code in the finally always runs. If you return out of the try block, or even if you do a “goto” out of the try, the finally block always runs:

Both “In Try block” and “In Finally block” will be displayed. Whether the return is in the try block or after the try-finally block, performance is not affected either way. The compiler treats it as if the return were outside the try block anyway. If it’s a return without an expression (as it is above), the IL emitted is identical whether the return is inside or outside of the try. If the return has an expression, there’s an extra store/load of the value of the expression (since it has to be computed within the try block).

I was trying to use an “out int” parameter in one of my functions. How should I declare the variable that I am passing to it?

- You should declare the variable as an int, but when you pass it in you must specify it as ‘out’, like the following: int i; foo(out i); where foo is declared as follows: [return-type] foo(out int o) { }

How does one compare strings in C#?

- In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings’ values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { … } Here’s an example showing how string compares work:

How do you specify a custom attribute for the entire assembly (rather than for a class)? - Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

Note that in an IDE-created project, by convention, these attributes are placed in AssemblyInfo.cs.

How do I simulate optional parameters to COM calls?

- You must use the Missing class and pass Missing.Value (in System.Reflection) for any values that have optional parameters.

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?

A peripheral assembly designed to monitor permissions requests from an application.

1. Any DLL file used by an EXE file.

2. An assembly containing localized resources for another assembly.

3. 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.

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 .

Interview Questions C#

General Questions


1. Does C# support multiple-inheritance?
No, use interfaces instead.

2. Describe the accessibility modifier “protected internal”.
It is available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

3. What’s the top .NET class that everything is derived from? System.Object.

4. Can you store multiple data types in System. Array? No.

5. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.

6. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception?
A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.

7. Can multiple catch blocks be executed?
No. Once the proper catch code fires off, the control is transferred to the finally block.

8. What is the role of the DataReader class in ADO.NET connections?
It returns a read-only dataset from the data source when the command is executed.

Class Questions:

0. How do you inherit from a class in C#?
Place a colon and then the name of the base class.

1. Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

2. Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes. Just leave the class public and make the method sealed.

3. What’s an abstract class?
A class that cannot be instantiated. Abstract class is an inherited class that have the methods overridden.

4. When do you absolutely have to declare a class as abstract?

When at least one of the methods in the class is abstract.

5. What’s an interface class?
It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.

6. Why can’t you specify the accessibility modifier for methods inside the interface?
They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.

7. Can you inherit multiple interfaces?Yes.

8. What’s the difference between an interface and abstract class?
In an interface no accessibility modifiers are allowed, which is ok in an abstract class.

9. What is the difference between a Struct and a Class?
Structs are value-type variables and are thus saved on the stack -> additional overhead but faster retrieval. Another difference is that structs CANNOT inherit. (Questions courtesy of Eyal)

Method and Property Questions

10. What’s the implicit name of the parameter that gets passed into the set method/property of a class?
Value. The data type of the value parameter is defined by whatever data type the property is declared as.

11. What does the keyword “virtual” declare for a method or property?
The method or property can be overridden.

12. How is method overriding different from method overloading?
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

13. Can you override private virtual methods?
No. Private methods are not accessible outside the class.

Original answer: No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.

14. What are the different ways a method can be overloaded?
Different parameter data types, different number of parameters, different order of parameters.

15. What’s a delegate?
A delegate object encapsulates a reference to a method.

16. What’s a multicast delegate?
It’s a delegate that points to and eventually fires off several methods.

17. What is a satellite assembly?
When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

18. What namespaces are necessary to create a localized application?
System.Globalization and System.Resources.

19. What is the difference between and XML documentation tag?
Single line code example and multiple-line code example.

20. Is XML case-sensitive? Yes.

Debugging and Testing Questions

21. What debugging tools come with the .NET SDK?

1. CorDBG – command-line debugger.

2. DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR.

22. What does the “This” window show in the debugger?
It points to the object that’s pointed to by this reference. Object’s instance data is shown.

23. What does assert() method do?
In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.

24. What’s the difference between the Debug class and Trace class?
Documentation looks the same. Use Debug class for debug builds, use Trace class for both debug and release builds.

25. How do you debug an ASP.NET Web application?
Attach the aspnet_wp.exe process to the DbgClr debugger.

26. What are three test cases you should go through in unit testing?

1. Positive test cases (correct data, correct output).

2. Negative test cases (broken or missing data, proper handling).

3. Exception test cases (exceptions are thrown and caught properly).

27. Can you change the value of a variable while debugging a C# application? Yes.

ADO.NET and Database Questions

1. What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?
SQLServer.NET data provider is high-speed and robust. OLE-DB.NET is universal for accessing other sources, like Oracle, DB2, Microsoft Access. OLE-DB.NET is not as fastest and efficient as SqlServer.NET.

2. Explain ACID rule of thumb for transactions.
A transaction must be:

1. Atomic - It is one unit of work and does not dependent on previous and following transactions.

2. Consistent - Data is either committed or roll back, no “in-between” case where something has been updated and something hasn’t.

3. Isolated - No transaction sees the intermediate results of the current transaction).

4. Durable - The values persist if the data committed even if the system crashes right after.

3. What connections does Microsoft SQL Server support?
Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and password).

4. What does the Initial Catalog parameter define in the connection string?
The database name to connect to.

5. What is the data provider name to connect to an Access database?
Microsoft.Access.

6. What does the Dispose method do with the connection object?
Deletes it from the memory.

7. What is a pre-requisite for connection pooling?
Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings. The connection string must be identical.

Some More C# Questions

1. When you inherit a protected class-level variable, who is it available to? Classes in the same namespace.

2. Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.

3. Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).

4. How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.

5. What does the keyword virtual mean in the method definition? The method can be over-ridden.

6. Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.

7. How can you overload a method? By using parameters.

8. What’s the advantage of using System.Text.StringBuilder over System.String? StringBuilder is more efficient in the when lot of manipulations have to be done to the text.

9. How can you sort the elements of the array in descending order? By calling Sort() and then Reverse() methods.

10. What’s the .NET datatype that allows the retrieval of data by a unique key? HashTable.

11. What’s class SortedList underneath? A sorted HashTable.

12. Will finally block get executed if the exception had not occurred? Yes.

13. What’s the C# equivalent of C++ catch (…), which was a catch-all statement for any possible exception? A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.

14. What’s a delegate? A delegate object encapsulates a reference to a method. In C++ they were referred to as function pointers.

15. What’s a multicast delegate? It’s a delegate that points to and eventually fires off several methods.

16. How’s the DLL Hell problem solved in .NET? Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32), but also the version of the assembly.

17. What’s a satellite assembly? When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

18. What namespaces are necessary to create a localized application? System.Globalization, System.Resources.

19. What’s the difference between and XML documentation tag? Single line code example and multiple-line code example.

20. What debugging tools come with the .NET SDK? CorDBG – command-line debugger, and DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.

21. How do you debug an ASP.NET Web application? Attach the aspnet_wp.exe process to the DbgClr debugger.

22. Can you change the value of a variable while debugging a C# application? Yes.

23. What’s the role of the DataReader class in ADO.NET connections? It returns a read-only dataset from the data source when the command is executed.

24. Which one is trusted and which one is untrusted? Windows Authentication is trusted because the username and password are checked with the Active Directory; the SQL Server authentication is untrusted, since SQL Server is the only verifier participating in the transaction.

25. Why would you use untrusted verificaion? Web Services might use it, as well as non-Windows applications.

26. What’s the data provider name to connect to Access database? Microsoft.Access.

27. What does dispose method do with the connection object? Deletes it from the memory.

28. What is a pre-requisite for connection pooling? Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.