Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, December 6, 2009

System.Transaction.IsolationLevel

A little amusing observation,
IsolationLevel enumeration defined in the System.Transactions namespace looks something like this:
public enum IsolationLevel
{
Unspecified,
ReadUncommitted,

RepeatableRead,
ReadCommitted,
Serializable,
Chaos,
Snapshot
}
By definition, chaos means -"a state of extreme confusion and disporder. " Initially I was not able to understand what is the whole purpose of having this option (that too with a funny name)
After doing some reading (on MSDN) and talking (with peer devs), this is what I understood.

Chaos Isolation Level - Behaves the same way as Read Uncommited, with additional features as stated below:

  • It permits viewing uncommitted changes by other transactions
  • It checks any other uncompleted update transactions with higher restrictive isolation levels to ensure not to raise any conflics i.e. any oending changes from more highly isolated transactions cannot be overwritten
  • Rollback is not supported in this isolation level

If you want to perform read operations over once per transaction, then go for the Chaos isolation level

Chaos isolation level is present in SSIS as well. Select the task or container on which you want to set the isolation level. Then go to the properties and set the property named IsolationLevel to Chaos.

//Cheers!
//Currently listening to: Put your hands up (Radio Edit) by Wet Fingers

Sunday, April 26, 2009

New definition to C#

If anyone asksus , what is C#, what will be our answer? All of us will say, C# is an Object Oriented Language targeting .NET run time. Today this definition of C# is only 10% correct or you can say 10% complete. Surprised! read ahead.

Few days back, I was listening to Anders Hejlsberg's PDC 2008 presentation on C# 4.0. There he formulated a new definition for C#.
C# is a multi-paradigm language that covers functional, imperative, generic, object oriented and component oriented disciplines.

C# 2.0 introduced some concepts of functional languages like anonymous methods and then came LINQ. F#, a pure functional language, makes extensive use of anonymous methods to achieve its goal. C# 4.0 will introduce new features that makes it interoperable with domain specific languages targeting .Net run time.

Some important links:

Currently Listening - Feel the Rush by Shaggy
~eNjOy CoDiNg~

Thursday, February 7, 2008

C# 3.5 - Properties Shorthand

I must tell you if today your C# knowledge is not up to date then you might face tough time with future versions of the language. Microsoft developers will keep on adding new things ranging from entirely new features like LINQ to something like Implicit types or Object initializations. Last two features that I've mentioned are due to LINQ and secondly they provide some kind of syntactical sugar for developers. In future, VB.NET and C# will take entirely different paths. C# will become more performance oriented and VB.NET more developer friendly. VB.NET will target UI where as C# will mostly be used as class libraries. I am not sure when will this happen but it will happen soon [trust my Microsoft contacts :) ]

Let me show you how property shorthand feature of C# works from inside. We will start of with a simple console application and add code as needed.

Adding property shorthand to your code
Type prop in code window and let intellisense jump in. Then select "prop" and press Tab 2 times. A property shorthand will appear
.

Complete your class definition
Add 3 shorthand properties and one normal property. Your final class definition will look like this



















Create a class that will use Customer object.
Main() method in Program.cs would be enough for us
,













And the output is as expected,




Let us examine the IL that is generated for this managed assembly. Below are two images one with IL showing members of Customer class and next one with one method expanded.


For the properties that we created with shorthand, C# compiler added a backing field for each shorthand property in this format,
k_BackingField: private TypeOfShrtHandProperty

get_FirstName method expanded
Image shown below looks at the IL generated for get_FirstName method. Second IL instruction 'ldflf' i.e. Load Field, works with backing field created by the compiler. set_FirstName() also uses this backing field.

Don't expect that these shorthands will support validations etc. These are simple properties and just adds thread safety over using class variables directly. If we don't want any thread safely and no validations, then we should not provide properties. I guess this is a cool new feature but unless you understand how it works inside you will not able to fully utilize it.

Next I'll add a small post related to introduction of partial methods in C#. I am down with Viral from last few days but today I am feeling quite well. I hope! to get well soon.

~ILdasm rOcKs~

Tuesday, December 4, 2007

Crazy Enums

Few days back I got a great opportunity to talk with a senior thoughtworker, Chris. He told me few things related to Agile and NUnit. Apart from this there was one intresting thing that I want to share. Almost all of us have worked with .Net Enums. Let me show you something,
Create a project and add one Enum to it,

enum MyEnum
{
Orange=0,
Red=1,
Green=2
}


Now create another class where we will use this Enum,

class Program
{
static void Main(string[] args)
{
MyEnum obj;
obj = (MyEnum)5;
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}


Did you noticed something? Our Enum only had 0,1 and 2 as valid values. I went on and assigned 5 to it. Now try compiling this code. Aha! no compilation errors. Now try running this. Everything goes well and 5 gets printed on console. Did you expected this?
I never expected this. Did Microsoft forget about type safety and type checking issues while copying from Java Enums? I think Java enums works fine.
If you ever worked with Bit Flags you will some how realize that all this is due to them. Bit flags can actually take up values which are a combination of values that are defined in Bit Flag Enum. Here is an example:

[Flags]
internal enum Actions
{
None = 0,
Read = 0X0001,
Write = 0X0002,
ReadWrite = Actions.Read Actions.Write
...
}

There are number of threads going on in forums related to this. Just keep this in mind before using Enums.
I am exited about playing EA Game's CRYSIS. I have the DVD and will be installing the game tomorrow. Actually I need to clean up my system a bit before I get on with the game. CRYSIS CD says, "Game needs 20 Gb of free hard drive space."

Currently listening to: Black or White - MJ
~nJoY CoDiNg~

Saturday, November 10, 2007

C# 3.0 - What's new?

Hello! everyone. This is festive time here in India. Yesterday we all celebrated Diwali, festival of light. I must tell you that lot of things have changed, things like playing with crackers, going out with friends and watching the way people decorated their houses using lights and ribbons. Yesterday, after having our Diwali prayer I just watched a movie (CHUCK) @ home and slept at around 11. Nothing new for me and it turned out to be yet another holiday. I wish things remained the same way as they were before.

Now let's talk technology. Microsoft have another a new set of features to C# in its third avatar. There is complete list available at MSDN, but I just wanted to add my comments to it as I study new features one by one. What I am planning is, I'll study one new feature everyday, post it here with my comments.
  1. Implicitly Typed Local Variables
    • Type is inferred by the expression used to initialize the variable
    • Only applicable to local variable
    • Variable declared using keyword 'var'
    • There should be no Type with name 'var' defined in scope. Only then type inferring will work
    • We should initialize the variable where we declaring it
    • Initialized must be an expression.
    • Local variable declaration should not include multiple variables
    • Initializer should not refer to variable itself
    • Some Examples: [ <=> is Same as ]
      var i = 5; <=> int i = 5;
      var s = "Hello";
      <=> string s = "Hello";
      var d = 1.0;
      <=> double d = 1.0;
      var numbers = new int[] {1, 2, 3};
      <=> int[] number = new int[]{1,2,3};
    • Some incorrect use of var:
      var x; // Error, no initializer to infer type from
      var y = {1, 2, 3}; // Error, collection initializer not permitted
      var z = null; // Error, null type not permitted
      var u = x => x + 1; // Error, lambda expressions do not have a type
      var v = v++; // Error, initializer cannot refer to variable itself
    • for, foreach and using (resource acquisition) can also use var. Like,
      int[] numbers = { 1, 2, 3, 4, 5 };
      foreach (var n in numbers) { //Do some thing }
    • COMMENTS
      Why do we need to use var? Everything should be Type Strict. I think this will impact performance as compiler will use some kind of intelligence to find out variable type and it will surely take up some time. This is no scripting language...man this is C#. It will just complicate things even more and add to huge list of keywords that we already have like sealed, virtual, override, abstract, new, internal etc. I don't see any use of this so called added feature.
      Please let me know your opinions.
Currently Listening To: Shadow of the day-LINKIN PARK
~eNjOy cOdinG~

Wednesday, October 31, 2007

C# Optimizing Compiler

Let me share nice thing that I noticed about C# compilation model. What I did was, I created a small program to print an Int32 array to console. Then I went into IL and checked out various function calls that C# compiler has made. There was one nice thing that I noticed. C# compiler sometimes optimizes your code by itself. Let me show you,

using System;

namespace Optimization

{

class Program

{
static void Main(string[] args)
{
Int32[] arr ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,11 };
for (Int32 i = 0; i<=arr.Length-1;i++) {
Console.WriteLine(arr[i]);
}
Console.ReadLine();

}

}

}

This is the code that I wrote to print a simple Int32 array. Below is the IL generated by this code snippet.


Compiler stores the array length in line IL_0000. The you can see there is no call to Array.Length property [internally it will call the method get_Length()] which I have used in For loop. Only calls that C# compiler added were to Initialize array, WriteLine() and ReadLine(). C# compiler have optimized our code internally so that there is no need to call Length with every loop iteration. It uses the length that is stored in the beginning to provide a performance boost. So, if you are doing such thing in your code, don't complicate things by declaring a variable just before the for loop just to store array length and use that variable in loop condition. You will be doing that to improve your application's performance but doing such heroics is not needed in this scenario.

I have just started to read about ASP.net internals and currently I am dating HTTPSys driver :). I hope to get this done by November 2nd week.
Currently listening To: SHAGGY Feat. AKON - What's Love
...nJoy CoDiNg...