<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.Net Developement : Programming</title><link>http://msmvps.com/blogs/pauldomag/archive/tags/Programming/default.aspx</link><description>Tags: Programming</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Serialization</title><link>http://msmvps.com/blogs/pauldomag/archive/2006/03/17/86719.aspx</link><pubDate>Fri, 17 Mar 2006 19:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:86719</guid><dc:creator>Paul June</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/pauldomag/rsscomments.aspx?PostID=86719</wfw:commentRss><comments>http://msmvps.com/blogs/pauldomag/archive/2006/03/17/86719.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I know that this is an old age technology in .Net. But I was surprised that some C# programmers still don’t know about serialization. So I’m creating this blog for the ones who still hasn’t got any idea on serialization. But if you already know this, then just skip this blog entry.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here’s a FAQ about serialization.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;What is serialization?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;Well, according to MSDN, “as the process of storing the state of an object instance to a storage medium”. So why do we need to store a class in an external storage? The reason for this is that, sometimes we need to communicate across between applications. And sometimes, it’s a lot easier if we can pass objects between two separate application domains.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;Why use serialization?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;Just like as said earlier, sometimes when you need to pass objects between two applications, you can’t pass it through memory coz two different applications resides in two different domains. So you must find a way to store it externally so that the other application could read the external file and recreate your class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;How do you serialize a class?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;All you have to do is mark your class as serializable. To do this just place the serializable keyword enclosed in angle bars before the declaration of your class:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;[Serializable]&lt;BR&gt;public class MyObject {&lt;BR&gt;&amp;nbsp;&amp;nbsp; public int n1 = 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public int n2 = 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public String str = null;&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This is the basic serializable class. When you serialize the class. All of its private and public properties would also be saved. Now, to serialize a class, you must have a serializer. To explain it better, Here’s an example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;MyObject obj = new MyObject();&lt;BR&gt;obj.n1 = 1;&lt;BR&gt;obj.n2 = 24;&lt;BR&gt;obj.str = "Some String";&lt;BR&gt;IFormatter formatter = new BinaryFormatter();&lt;BR&gt;Stream stream = new FileStream("MyFile.bin", &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileMode.Create, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileAccess.Write, FileShare.None);&lt;BR&gt;formatter.Serialize(stream, obj);&lt;BR&gt;stream.Close();&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This sample writes your class into a File names “MyFile.bin”.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;After serializing, what should I do?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;After serializing the class, you should deserialize it for you to restore the state of the saved class. Here’s a sample:&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;IFormatter formatter = new BinaryFormatter();&lt;BR&gt;Stream stream = new FileStream("MyFile.bin", &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileMode.Open, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileAccess.Read, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileShare.Read);&lt;BR&gt;MyObject obj = (MyObject) formatter.Deserialize(fromStream);&lt;BR&gt;stream.Close();&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;// Here's the proof&lt;BR&gt;Console.WriteLine("n1: {0}", obj.n1);&lt;BR&gt;Console.WriteLine("n2: {0}", obj.n2);&lt;BR&gt;Console.WriteLine("str: {0}", obj.str);&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In the previous example, we are using the BinaryFormatter as the serializer for our class. The BinaryFormatter class serializes our class into a file. But its pretty useful to serialize our class into an xml stream. This is the case of Webservices. To serialize a class into an xml stream, you’ll have to use the SoapFormatter instead of BinaryFormatter. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;What if I don’t like to include some properties during serialization?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;To exclude some properties, just mark them as NonSerialized. Here’s a sample:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&lt;STRONG&gt;[Serializable]&lt;BR&gt;public class MyObject &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; public int n1;&lt;BR&gt;&amp;nbsp;&amp;nbsp; [NonSerialized] public int n2;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public String str;&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This process is called Selective Serialization.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;What about the ISerializable Interface?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;The ISerializable interface is used when your doing Custom Serialization. Wondering what difference it is compared to Selective Serialization? Well, when your class is implementing the ISerializable interface, you will have the power to set values to your variables during serialization. This is pretty much useful when a value in your class would be invalid after deserialization, but you need to initialize its value. You must be also wondering if why not just place the initialization code in your constructor? The answer is simple, during deserialization your constructor would not be called. So how to implement ISerializable? Here’s a sample:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;Serializable]&lt;BR&gt;public class MyObject : ISerializable &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; public int n1;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public int n2;&lt;BR&gt;&amp;nbsp;&amp;nbsp; public String str;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; public MyObject()&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected MyObject(SerializationInfo info, StreamingContext context)&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n1 = info.GetInt32("i");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n2 = info.GetInt32("j");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; str = info.GetString("k");&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp; public virtual void GetObjectData(SerializationInfo info, &lt;BR&gt;StreamingContext context)&lt;BR&gt;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info.AddValue("i", n1);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info.AddValue("j", n2);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info.AddValue("k", str);&lt;BR&gt;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;There are two things to consider when implementing the ISerializable interface. You should implement a special constructor which happens during deserialization. And also the GetObjectData method, this method is called during the serialization process.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;EM&gt;When should I use Serialization?&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR&gt;A straightforward answer, always. You should mark your classes serializable as often as possible. Coz you might not know that the user of your class might design to inherit your class and serialize it. Hmmm. I kinda forgot to say that you cannot serialize an object if the base class is not serializable. Sorry for that.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;So during your design phase, you should identify the classes that you think its users would serialize. To be safe, you can mark all of your class into serializables.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;I hope this clarifies any issue about serialization. Cheers!&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=86719" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/pauldomag/archive/tags/Programming/default.aspx">Programming</category></item><item><title>C# GetObject and CreateObject</title><link>http://msmvps.com/blogs/pauldomag/archive/2006/03/15/86417.aspx</link><pubDate>Wed, 15 Mar 2006 16:08:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:86417</guid><dc:creator>Paul June</dc:creator><slash:comments>769</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/pauldomag/rsscomments.aspx?PostID=86417</wfw:commentRss><comments>http://msmvps.com/blogs/pauldomag/archive/2006/03/15/86417.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;When&amp;nbsp;interoperating with COM applicaitons, sometimes you need to get the running instance of the application object of the app. In VB you would be doing this by using the GetObject method.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Basically the GetObject method returns a reference to an object provided by a COM component.&lt;BR&gt;For example you want to get a reference on the currently running excel program.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;Dim excelObj As Object&lt;BR&gt;&lt;BR&gt;excelObj = GetObject(, "Excel.Application")&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;If the above statement causes an error, then no instance of the excel application is running.&lt;BR&gt;This routine is very basic for VB programmers.&lt;BR&gt;But what about C#? How could you do this in C#?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;There is no direct&amp;nbsp;equivalent of the GetObject method in C#. I have this problem lately, and I have produced a simple routine that&amp;nbsp;"could be" the equivalent of&amp;nbsp;GetObject method (I've used this to get a reference on a running Autodesk AutoCAD application:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;acadObj&amp;nbsp;= (AcadApplication) Marshal.GetActiveObject("AutoCAD.Application.16");&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Note: you'll have to add the namespace System.Runtime.InteropServices to be able to use the Marshal object.&lt;BR&gt;In VB you can also create a reference to a COM applicaiton by using the CreateObject method if the GetObject method fails:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;excelObj = CreateObject("Excel.Application")&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In C# its quite very simple. You just need to allocate the application object of the COM component:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;AcadApplication acadObj = new AcadApplication();&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This sample routine would then launch a new AutoCAD application.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=86417" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/pauldomag/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Generics 101</title><link>http://msmvps.com/blogs/pauldomag/archive/2006/03/05/85526.aspx</link><pubDate>Sun, 05 Mar 2006 19:08:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:85526</guid><dc:creator>Paul June</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/pauldomag/rsscomments.aspx?PostID=85526</wfw:commentRss><comments>http://msmvps.com/blogs/pauldomag/archive/2006/03/05/85526.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Another new feature of C# 2.0 is the ability to create classes that can handle different types while eradicating type boxing on the underlying type. This new feature is called Generic Classes.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This type of class enable its user to create a somewhat “general class” that could accommodate any types. By using this class there is no need for type casting thus reducing the possibility of runtime errors.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Generic classes are most widely used on user-defined collections. In the previous version (framework 1.1) you are forced to implement collections using object as its underlying type so that it could store any types. This implementation would result into runtime type checking and type casting. To avoid this you can implement a class for each type that you are planning to support, thus creating an overhead of multiple implementations of a class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here’s a scenario. You are creating a user defined collection which has the capability to convert its stored values into a csv file. The first step is to inherit from a collectionbase class. Second is to implement your add/remove/get methods. Here then comes the problem, what type would my get function return? Or what type would my add function receive?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Generic class is the solution for the problem above.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Here’s the proper syntax for declaring a Generic Class:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;public class GenericClass&amp;lt;T&amp;gt; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;T _value;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public GenericClass(){}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void setValue(T data) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_value = data;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public T getValue() {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return _value;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Let’s examine this class. The first odd thing that you would notice is the angle bars located right after the class name. These angle bars encapsulates the type that this class uses. After specifying the type, you can then use it like a normal type. By default the &amp;lt;T&amp;gt; would act as an object type within your class.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;To use the above class in your program, you can initialize it this way:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;GenericClass&amp;lt;int&amp;gt; generic = new GenericClass&amp;lt;int&amp;gt;();&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Try to check if your setValue and getValue functions return an int which was the specified type within the angle bars.&lt;/FONT&gt;&lt;/P&gt;&lt;IMG height=40 src="/photos/pauldomag/images/85525/original.aspx" width=393&gt;&lt;IMG src="/photos/pauldomag/images/85523/original.aspx"&gt;&lt;BR&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;As you can see, both of the methods of the class now returns int.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In C# 2.0 there are already Collection based Generics that’s readily available. Most likely, you’ll going to use these classes rather than create one yourself.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;•&amp;nbsp;List is the generic class corresponding to ArrayList.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;•&amp;nbsp;Dictionary is the generic class corresponding to Hashtable.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;•&amp;nbsp;Collection is the generic class corresponding to CollectionBase.&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=85526" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/pauldomag/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Nullable Types</title><link>http://msmvps.com/blogs/pauldomag/archive/2006/02/28/85068.aspx</link><pubDate>Tue, 28 Feb 2006 19:08:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:85068</guid><dc:creator>Paul June</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/pauldomag/rsscomments.aspx?PostID=85068</wfw:commentRss><comments>http://msmvps.com/blogs/pauldomag/archive/2006/02/28/85068.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Well, this is the official opening of my blog.&lt;BR&gt;I’ll be blogging on the new features of Visual C# 2005 on my first few posts. The newest feats that I love on C# is on its new Nullable Types.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Nullable types gives you the ability to create types that can hold null values. You might be wondering on how would this be useful. Well nullable types can be very useful when dealing with database values. Such as you don’t need to convert null values in your database to a blank string or 0 if numeric when passing it directly to a variable.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;By default, types cannot contain null values. Here’s an example that demonstrates this:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;int x = null;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The code above generates an error (Cannot convert null to 'int' because it is a value type).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;A nullable type can represent the normal range of values for its underlying value type, plus an additional null value. For example, a Nullable&amp;lt;Int32&amp;gt;, can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable&amp;lt;bool&amp;gt; can be assigned the values true or false, or null.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;To create Nullable Types you must declare your variable this way:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;System.Nullable&amp;lt;int&amp;gt; x;&lt;BR&gt;// Then you can assign it to a null value&lt;BR&gt;x = null;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;But there is a short way of doing this just add a “?” operator after your type declaration:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;int? x = null;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Both two types of the declaration are interchangeable.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;By declaring your type as nullable, you have additional properties on your object. These are HasValue and Value read-only properties. Both of these properties is being used to check the nullability of your data. The HasValue property returns true if the variable contains a value, or false if it is null. The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown. Here’s an example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;int? x = null;&lt;BR&gt;if (x.HasValue) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int j = x.Value;&lt;BR&gt;}&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Another useful additional function is the GetValueOrDefault. This function returns the current value or the default value of the type if its current value is null.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;int? x = null;&lt;BR&gt;int j = x.GetValueOrDefault();&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;The statement would give the variable j a 0 value instead of a null.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This might be the coolest feature of the nullable types. If your variable is of nullable, then you can use the new “??” operator. This is the shortcut of “?:” operator (iif in VB) on detecting null values.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;// This is the old declaration&lt;BR&gt;int? x = null;&lt;BR&gt;int j = x == null ? 0 : x;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;STRONG&gt;// This is the new declaration&lt;BR&gt;int? x = null;&lt;BR&gt;int j = x ?? 0;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Both statements are interchangeable. It’s just in your choice on which looks easier.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;This wraps up my first blog. Hope to see you on my next!&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=85068" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/pauldomag/archive/tags/Programming/default.aspx">Programming</category></item></channel></rss>