Tuesday, September 9, 2008

Mono

Since I have decided to re-install and migrate completely to Linux, I have put my C# learning on hold. Before making the move to Linux, I had to confirm that C# was properly supported on Linux. The primary reason behind having this doubt was that C# and .Net is primary a MS sponsored language and whether it was being ported properly to Linux was a big question in my mind.

So, after research I found out that Linux has a port of .Net called Mono. This comes pre-built into openSUSE. However, as I see, it is not supported out of the box on Ubuntu. As in, it doesn't come with the installation disk. But then, what the problem when I can just apt-get it. :)

So, now the next steps are to decide on my distribution of choice and then install Mono on it. Once I do that I can continue with learning C# and this time on linux. The only bad thing is that the .Net framework is released for Windows and it is then ported onto Linux. What this means is that the linux implementation could one or more versions behind the MS implementation in terms of features. Well, thats one thing that I have to live with.

Labels: ,

Properties of a Class

Properties in C# refers to a way of encapsulating the class attributes/variables. When I first read this, this was something new and a radically different concept. I have never come across such a concept in any other programming language before this. For that matter, I would have not come across any topics that I am/will be blogging about since this blog is going to be only about the different and distinguishing features of C#.

So what are these properties?
Properties are like get/set methods for an attribute like when you use beans in Java. The inherent advantage is not easily visible when you see this for the first time. Say you have a variable, x, then you can declare this variable as private and instead have a public Property for this variable so that it can be accessed. Now, one would wonder what is the necessity for making it private in the first place when you are anyway providing access to the variables. I was of this very same opinion initially till I understood the benefits of this approach.

Firstly, in the simplest of all cases, these get/set methods could be used to validate or control the values that would be set on the attributes. It is the same as creating a public method to set the value on the variable, just that the call way is different. In case of a method, one would have to call the methods and pass the value to be set as arguments. In case of properties, it is similar to declaring a public attribute and putting values on this attribute will set the value to the actual private attribute. At least this is what how the usage of properties looks like. Other benefits would be to allow only read access to the attribute by implementing only the get property and not implementing the set property. This way you are implicitly marking the attribute as read-only.

This feature is used a lot in the C# libraries, where you can used the property of a class to get information about that object. A case in point is the Length property of an array. When you want to see the size of the array, you use the command arrayName.Length in loops and other places. The member Length is in fact a property of the array and is a read only property which you can only read and not change. Quite a useful feature once you get to using it and it does explain a lot about the language constructs.

There is also an enhancement of the property called index. We’ll look at that in the next post.

Labels:

Wednesday, August 20, 2008

Got .Net 3.5 Installed

Ahhhh… Finally!!! I just managed to successfully install .Net Framework 3.5 on my laptop. I have been trying this since about a month now. I managed to install it on my home PC at the first shot, but it just refused to install on my lappy. I tried using the web installer, the complete installer, tried and retried both multiple times but it never installed and kept throwing some errors.

Somehow, today morning I finally decided that I have to install it in any case. I tried again and the installation failed as usual. Today I thought let me see what is the error and I checked the installation logs. The message stated that the .Net 2.0a libraries were not over-written properly. So, I re-installed .Net 2 and .Net 3 in the hope that 3.5 installation would work. But it did not. Then I uninstalled the existing 3.0, 2.0 and 1.1 installations. It did not ask for a reboot when I removed 3.0 and 2.5, but finally did so when I un-installed 1.1

At last I tried 3.5 installation and the installation went on successfully. Finally I have 3.5 installed on my lappy and I can code in C# even on the move. :)

Labels: ,

Thursday, August 14, 2008

Partial Classes

C# has an interesting concept of partial classes. I don't remember whether Java and C++ have such a feature, and so I am finding this interesting. The other languages might have developed such a functionality of late, but at the time when I had learnt it and was using it, partial classes didn't exist in Java and C++.

So, what are partial classes? As the name suggests, it means that the class is partial or incomplete. Now, what could be the use of these partial classes? The reason behind this could be a) Readability, b) Team-based development. Let see what I mean by these.

Supposing you have to develop a class with a lot of functionality and the length of the class spans to pages. Then you might like to modularize it into separate files for the sake of readability. You might like to declare, say, the types of the class in one file, the reading method of the class in one file, and the writing methods of the class in another file. This would improve readability considerably.

The other reason could be, again, if you are having a really huge class implementation, but this time you decide that more than 1 developer will code the implementation. So each developer is asked to develop a certain number of methods of the class and the developer can do so independently in their own files as partial classes. Finally all these files can be compiled together as one file and can put together a single whole class.

Partial classes are declared using the modifier partial.
public partial class Employee { ... }
To compile the complete class from 2 files, say, file1.cs and file2.cs, the following syntax will be used
csc/out:full_file.exe file1.cs file2.cs

Partial classes have other interesting properties like if one partial class inherits a class or an interface, the whole class will inherit the same class/interface. So if each of the partial classes implements 2 different iterfaces, then the whole class will also implement both the interfaces.
public partial class Emp: IPerson { ... }
public partial class Emp: ICompany { ... }
is equivalent to
public class Emp: IPerson, ICompany { ... }

Labels: ,

Nullable Types

C# has this interesting concept of Nullable types. The Nullable types are a property of the System.Nullable class. What is meant by Nullable types is that the variable which is declared as a Nullable type need not store any value when first used.

You might be thinking that anyway when you first declare a variable, it does not contain any value. Actually, a variable that is declared is automatically initialized to a default value. Different 'types' have different initial values. All the numeric types are initialized to '0' or '0.0' as the case may be.

So, if you declare an integer variable and don't give it a value then it automatically gets the value 0.

What the Nullable type means, is that the variable will not even get this initial value, i.e. the Nullable variable is never initialized. One has to explicitely give it a value whenever required. This is explicitely needed in cases such a database table entries. Here you might store an entry in the DB using the variable. If you want to store an empty row in the table, or if you are storing a row in the table with an empty field, then you would want to use Nullable types. If you use normal types, then that field will not be blank, but will instead have the initial value, i.e. zero '0'.

Normal Type:
int var1; //var1 = 0
Nullable Type:
int? var2; //var2 = null
The Nullable type has two methods HasValue() and Value() which returns whether the Nullable type has been initialized or not, and the actual value that is stores if it has been initialized, respectively.

Labels: ,

Monday, August 11, 2008

Variable Placeholder

Also, some deviations from the material that I am referring to. I am referring to C# 3.0, which I supposed is analogous to .Net 3.0, in which case the next version of .Net, i.e .Net 3.5 has some changes with respect to the outputting command.

In 3.0, the WriteLine() method uses paranthesis and a number, eg: (1), as a placeholder for variables, or at least so says the book.
System.Console.WriteLine("Print this (0)", myVar);

Since this did not work and just printed "Print this (0)" as it is, I suspected that the syntax might have changed. Sure enough, in 3.5 the paranthesis have been replaced by curly braces as the placeholder.
System.Console.WriteLine("Print this {0}", myVar);

Edit: I noticed that the rest of the book uses the curly braces -> {}, as the placeholder for the variables. So there has not been any change in the C# language. This was only a printing anomaly in the book that I am referring to.

Labels: ,

Compilation Speeds

In the last post I observed that compilation takes a lot of time. By lot of time, I mean something to the tune of 4-5 seconds, and that is huge when it comes to a simple 'hello world' program. I realize today that the subsequent compiles take a lot less time and is infact instantaneous for the good ol' 'Hello World' program. :)

Labels: