Practically there are no differences between int and System.Int32 (except while using enums). C# allows for certain aliases and it is one of those. Both int and System.Int32 are compiled to System.Int32 in the Intermediate Language (IL). C# contains a number of aliases that allows for quick typing of the variable types. Here is a full list of the aliases.
June 19, 2015
May 18, 2015
Strings, in C#, represents text as a series of Unicode characters. A string can be referenced as both "string" and "String" - string is basically an alias of System.String. It can be created in a number of ways. In the simplest form, it can just be assigned or alternatively it can be created using the String's constructors. String, like other objects in .NET, can be assigned to null. Strings might look like and can be used like other variables like int or double but one important characteristics is that it is immutable - that is, once a String is created, it cannot be changed (so, when a string variable is re-assigned, behind the scenes, a new string is created and assigned to the variable).
May 15, 2015
All these methods Int32.Parse(), Convert.ToInt32(), and Int32.TryParse() ultimately does the same thing - convert a string to an integer. The main difference lies in what happens if the conversion does not succeed. With Int32.Parse(), exceptions are thrown if conversion does not work. With Convert.ToInt32() methods 0 is returned while parsing null, otherwise exceptions are thrown. With Int32.TryParse() method, no exception is thrown - it sets the correct value to an integer variable if conversion succeeds and at the same time returns a boolean value suggesting if conversion has succeeded.
July 18, 2014
In this example, I will show how to serialize an object in xml. In this post, I will use an Employee object, create a List of Employees and then serialize the object to xml. Serialization can data from objects needs to be saved in the filesystem or other storage and then used later for retrieval. Here is the code on how this can be achieved.
Once the code is run, the xml produced looks like below.
May 30, 2014
In this post, I will quickly show how to get system related information like machine name, user name, domain name, or how long ago the system started, etc. Most of these information can be retrieved from System.Environment class and I will take advantage of this class to display this data.
Below is an example of how most of these information can be obtained from the System.Environment class.
May 29, 2014
A List can contain simple types like int, string or other objects. Other day I was working and had to remove duplicates from a List collection. In this post, I will talk about how I remove the duplicates using minimum code. To start with a clue, I used a HashSet to remove the duplicates.
HashSet has the feature to only contain distinct items within it. Therefore, I could convert the List to HashSet and back to a list and this would remove all the duplicates for me. For HashSets to only contain unique items, the object needs to override the Equals and GetHashCode methods. Below is an example of how this can be done. I have created an Employee class with few members and overridden Equals and GetHashCode methods. Then I have another class EmployeeCollection where I have removed the duplicates.
April 29, 2014
In one of the applications I was working, I noticed that as we have developed the application we have used both System.Xml and System.Xml.Linq namespaces. Now this can happen for a number of reasons including that as the application got developed, the new namespaces were used but the old code has not been refactored and it's not a priority as well as the latest .NET framework supports it. However, sometimes we might need to convert files from old to new version and vice versa. In my situation, I had to convert XDocument to XmlDocument and vice versa. To cater for this requirement I wrote a class with static methods that perform the conversion taking advantage of the XmlNodeReader and XmlReader classes.
Here's the sample code of the class.
April 16, 2014
Enum in C# is a quick way to store constants. We can also assign value like int or chars to the constants. In other words, we can have a basic key - value sort of scenario with enums. Okay, so, how can I use a enum and bind it to a dropdownlist in asp.net.
To analyse what we need to do, we basically need to pull the names and the values from the enum and say a particular key has a particular value. Enums do not have a way to export the data to a list but it can export the list of names and list of values separately. The lists produced are in the same order and so we can say list[value] belongs to list[key].
March 25, 2014
MemberwiseClone method from the Object class can be used to perform a shallow copy of an object. The method creates a new Object, copies all the instance fields. For value type fields, it copies bit by bit. For reference types fields, the reference is copied. That is, if the reference field / object is changed by the initial object, then the cloned object will also get updated. I have written sample code to illustrate. I have used a web project - but you can use a console application if you like it.
March 6, 2014
To solve this problem, I can use the System.Drawing.Image class and System.IO.Stream class to get height and width of the image. To do so, let’s add the FileUpload control on designer.
Next, on the code-behind, I have a method that retrieves the uploaded file stream, loads an image from the stream and then obtains the dimension of the image. Note that I have skipped validation for the image on the FileUpload control and have assumed that a valid image is uploaded.
September 18, 2013
August 22, 2013
Client IP address might be sometimes useful depending on the use case. In different projects, the company might decide to release products based on IP address range, etc and it will be useful in those scenarios to get the ip address. However, if the product is a web application and it needs to get location of the user - then, HTML5's geolocation might be a better solution provided. I will discuss about HTML5 geolocation in a different post.
In asp.net, the Request object has access to various server variables including HTTP_X_FORWARDED_FOR and REMOTE_ADDR which are two important ones for retrieving client IP address. There is a method in the Request object as well - Request.UserHostAddress() - that can return the client IP.
The problem with Request.UserHostAddress() is that this method only works if the user is not behind a proxy server. If the user is behind a proxy server, this method returns the IP address of the proxy server. In this situation, the IP address is stored in the server variable - HTTP_X_FORWARDED_FOR. This variable contains a list of comma separated IP addresses in the format client IP, proxy1 IP, proxy2 IP, ... It is important to note that if the original client IP is needed, then we need to get first IP address returned.
If the above variable - HTTP_X_FORWARDED_FOR - returns null, then a different server variable - REMOTE_ADDR - needs to be used to get the IP address. There is no difference between using the method - Request.UserHostAddress() - and the variable - REMOTE_ADDR. Here is a code snippet on how to get the IP address of the client.
July 28, 2011
Using Linq, it’s easily possible to query various types of data like sql database, xml, objects like DataTable, ArrayList. It’s also possible to transform data into other formats like xml using linq. In this article, I will demonstrate how to query an System.Collections.ArrayList collection and then to save the data in xml format.
At first, I will create an ArrayList object and add Person objects to it. The code to do is below.
The Person class has 4 public properties of different data types – string, DateTime and int as shown below.
Person objects are added to the ArrayList list which can be queried using linq as shown in my previous post on query ArrayList using Linq. While querying the list, System.Xml.Linq.XElement objects can be created which can be saved as xml using System.Xml.Linq.XDocument’s Save() method. The code to show this is below.
As can be seen, the ArrayList object is queried for Person objects. This is not really needed for the code to work but is an example on how objects can be queried from ArrayList.
Using another query, “query”, XElement objects are created. The names of the XElement – that is, the names of the xml elements are arbitrary and the value of the elements are obtained from the Person object. The XElement objects are then added to XDocument object and saved as xml using XDocument.Save() method. The resultant xml looks like below.
One thing to note is that the xml declaration is automatically added. DateTime field – DateOfBirth is automatically converted to its string representation.
The xml produced previously contains elements only. The Linq query could be easily updated to change the data to be the attributes of xml elements. This is shown in another query – “query2”. System.Xml.Linq.XAttribute objects are created similarly instead of XElement objects and added to XElement object which is then added to XDocument object and saved as xml. The resultant xml file looks like below.
The data looks similar to the previous xml except that they are now attributes of “Person” element instead of child elements.