Saturday 12 April 2014

Enums in C#

An enum is kind of construct that you can use to make your code easier to work with. The enum construct is useful when you want to create a method that has multiple modes. A good example is the constructor for the CryptoStream class. The CryptoStream constructor accepts CryptoStreamMode.Read or CryptoStreamMode.Write as one of its arguments. This is a simple enum.

Enum is short form enumerator. Enumerating something literally means assigning numbers to it. An enum takes meaningful names and assigns numbers to them.

An enum object doesn't take up any more memory than a standard int variable, so there is no good reason not to use them to make your code clearer and easier to understand. As well as making your code more readable, enums also make methods easier to use; this makes them particularly useful in class libraries.

Friday 11 April 2014

Garbage Collector

The Garbage Collector is a special feature of C# that automatically clears objects from memory when they are no longer needed. For example: once a method is finished executing, the garbage collector removes any variables it used from the memory. Thanks to the garbage collector it is very rare for C# applications to suffer from memory leak issues when the program uses up more and more memory until it overloads the system.

The garbage collector will work automatically without you needing to do anything but you can send commands to it by using the GC class. The GC class can also provide some useful information about memory usage.
It is important to note that using the GC class can potentially make your project less memory efficient. The garbage collector files your objects into one of three categories or generations.

Generation 0 contains objects that will be disposed off quickly such as variables within methods. Generation 1 contain objects that are expected to remain in memory for longer, a public property of a class might be a generation 1 object. Generation 2 contains objects that are expected to remain in memory for a very long time such as static classes.

When the garbage collector cleans up memory, it promotes any object that is still in use to the next generation. Misusing the GC class can lead to objects being promoted to inappropriate generations and remaining in memory for longer than they should.

Monday 7 April 2014

Difference between Class and Struct

A struct is a construct that is almost identical to a class.
The major difference is that structs are value type objects while classes are reference type objects. This means that when you pass a struct object to a method, the method will receive a copy of the object and wont be able to make changes to it outside the method.
It is usually best to use classes instead of structs as there are many features of classes that aren't supported by struct objects. However it is not possible to pass a class object by value so if there is s special need to do this you may find struct more appropriate.

The limitations of structs are that they cannot have constructor methods without any arguments and they cannot extend classes nor can classes extend structs.

Because structs are value type objects, this means that they are stored in the stack. Whereas class objects are stored in the heap. Because struct objects are stored in the stack they will generally be slightly faster than class objects. It is best to avoid using large structs as there is a limited amount of space in the stack, you should only use structs for small constructs that need to be accessed quickly.

Sunday 6 April 2014

C# Lazy Class

When you create a variable in C# it immediately reserves a space in memory even if you don't assign a value to it. For example: if you create an int variable it immediately reserves 32 bit of memory. It is very rare for this to be an issue but there may be cases when you need to work with a class that consumes enormous amount of memory.

In these cases you can use the Lazy class to prevent the object from being loaded into memory until it is needed. This is called Lazy instantiation. If you follow good coding practices, its extremely unlikely that you'll ever need to make use of the lazy class.

An important note when working with Lazy objects is that their constructor methods will not run until they are loaded into memory.

Friday 4 April 2014

C# Tuple Class

The Tuple class is a new feature of ASP .NET 4.0 that enables you to very quickly create custom data structures similar to classes. Tuple objects can't contain methods, they are only structures for data storage. It is best practice to avoid using Tuple as where possible as they don't have the flexibility of real classes and can make your code harder to understand.

Tuple objects offer an easy way to return multiple values from a method. It is important to note however that it is better to do this using either the out keyword or ideally returning a custom class.
Tulple objects have a limit of eight properties, although you can nest Tuple objects by using Tuple as a property type within another Tuple. You need to set the values of a Tuple object's properties when you create it. After the Tuple object has been created it's values will be read only.
Once a Tuple object is created, it will behave in exactly the same way as any other object that has read-only properties.

As well as creating a Tuple object using the standard method of instantiating a class, you can also use the Tuple.Create method. The only reason to use this method is to cut down on the amount of code you need to type as the Create method will automatically select the correct data types for the values you provide.

Thursday 3 April 2014

ASP .NET Master Pages

A Master Page is a way of storing your site layout in a single file which you can then assign to many pages. The site that is automatically generated when you create a new ASP.NET web application includes a master page. By using a master page you can avoid having to manually update each page when a minor change is made to your site layout.
The disadvantage of master pages is that you lose the ability to completely customize each page.

The ContentPlaceHolder is a special control that is only available on master pages. It is used to define areas that can be edited on pages that use the master page. The ContentPaceHolder appears in the standard category of the toolbox.

Tuesday 1 April 2014

Difference between Web Application Project and Website Project

Web Application projects are generally a better option then website projects for two reasons: Firstly because website projects are not compiled; the web server has to compile them as the site is viewed, this makes websites slightly slower then web applications.
Secondly because web application projects are compiled; so the code of the website they produce will not be easy to edit, this prevents the website from being tampered with and allows you to protect your source code.

In your professional career you might have to work on a website project that was created by another developer, in this case it is useful to convert it into a web application project.