Posts Tagged ‘C#’

nUrlRewriter - IIS7 Url Rewriter

Sunday, October 26th, 2008

nUrlRewriter - IIS7 Url Rewriter

nUrlRewriter is a ASP.NET Http Module written in managed C# code which examines incoming Http requests and applies user defined criteria which may result in a Http request being redirected or rewritten. Web pages within existing web sites are often archived or retired, however many Internet based hyperlinks may exist for such web pages. nUrlRewriter solves this problem by providing a facility which can easily redirect or rewrite such Http requests to other web site web pages or web applications. For example, a discontinued product web page may be redirected to a general product category web page. nUrlRewriter differentiates itself from other redirectors/rewriters in that nUrlRewriter also supports the IIS7 Integrated ASP.NET Pipeline, enabling nUrlRewriter to redirect/rewrite any incoming web application URL supported by the IIS7 web server, such as but not limited to native HTML applications (htm, html), classic ASP applications (asp), PHP applications (php) as well as ASP.NET (aspx) applications.

Incoming Http requests which are redirected are returned to the originating browser with a status code of either 301 (permanent) or 302 (temporary) to indicate that the requested web page has been moved to a new target URL provided to the browser. the browser will then issue a new Http request for the new URL. Http status code 301 indicates that the URL has been permanently moved and the browser should use the new URL in any new Http requests. Http status code 302 indicates that the URL has been temporarily moved and the browser should use the new URL only for the outstanding Http request.

Incoming Http requests which are rewritten, are rewritten to a different URL location within IIS. Since the originating browser is not informed of the URL rewrite, the browser URL address bar will continue to display the originating URL before the URL rewrite.

nUrlRewriter works equally as well with IIS5 and IIS6.

Home Page

I was thinking of reading some source code of open source projects, I thought I will start with this as this is a simple and small little utility and in my imagination, the source code could be under 1000 lines. Do you have any good suggestions or what are your favourite open source projects ?

Comments and Suggestions are Welcome.

nUrlRewriter - IIS7 Url Rewriter

.Net performance Strut vs Class

Monday, June 9th, 2008

.Net performance Strut vs Class

In this article I am writing a simple test to compare the performance of ‘Struct’ and a ‘Class’. Yes, everyone knows thats a ‘Struct’ wins because it is a value type. No, Absolutely No. Use a ‘Struct’ wisely. It can be of huge benefit to performance if used properly. If it is not used properly, it can a bottleneck also. Lets see 2 simple examples which illustrate this.

The following code snippet is going to show the benefits of Struct, creating a value type, reading it and removing it on the stack is much easier than creating an object on the heap. So ‘Struct’ is faster.



using System;

namespace StructVsClass
{
public struct TempStruct
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}

public class TempClass
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
}

class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
for (int i = 0; i < 5000000; i++)
{
TempStruct ts = new TempStruct();
ts.I = i;
}

DateTime endOfStruct = DateTime.Now;
for (int i = 0; i < 5000000; i++)
{
TempClass tc = new TempClass();
tc.I = i;
}

DateTime endOfClass = DateTime.Now;

Console.WriteLine(endOfStruct.Subtract(start).Ticks);
Console.WriteLine(endOfClass.Subtract(endOfStruct).Ticks);
}
}
}

When I ran this code, I got the following result:

Struct vs Class Performance .Net

After that I made small changes to the code and within the for loop, I casted the struct to and object and casted the object back to a struct. Now as you guessed, the structs for loop ran slower because an value type is crested on stack and when we casted it an object was created on heap also, so it lost all of its performance benefits.

Here are the results.

The lesson is: use struct but use it wisely.

You can download the code used for this simple test here.
Uncomment the commented lines for the second test program i.e with the casting stuff.

Related Posts:
Profiling your .Net Applications Part - 1
Types in .Net - Part 1
Type in .Net - Part 2

PS:
Thanks to ‘Bart Czernicki’ for his valuable comment and for pointing my spelling mistake. And man its all over the post. As he suggested I will try to put in more useful information in a future article. Once again thank you very much Mr. Bart Czernicki.

.Net performance Strut vs Class

kick it on DotNetKicks.com

My Favourite Links (06/07/2008)

Sunday, June 8th, 2008

My Favourite Links (06/07/2008)

Health Monitoring In ASP.Net 3.5

C# And Partial Classes

Give way to the yield keyword

My Favourite Links (06/07/2008)

C# Interview Questions Part - 1

Friday, May 16th, 2008

C# Interview Questions Part - 1

Hello All,
I will be posting interview questions on various programming topics and all will be under interview questions tag. Prepare well for your interviews.

1) What is Boxing And UnBoxing ???
A) Boxing is the process of casting a value type as a reference type and storing the object on the heap. Unboxing is the process of casting a reference type as a value type and storing the value on the stack.

2) Can the web service’s methods be overloaded?
A. Yes, but the overloaded methods should be given a different name in the [WebMethod] methods attribute.

3) What is the difference between Debug and Trace class ?
A. Debug class is enabled only in debug builds. Trace class is available in both Debug and Release builds.

4) What is Method Overloading ?
A. Method Overloading is having the same method name and implementing different code by having different datatypes for parameters or different order of parameters of different datatypes or different number of parameters.
NOTE: Method overloading cannot be implemented based on return type.

5) What is method overriding ?
A. Method overriding is having the same signature as a method but implementing different functionality.

6) Can you inherit multiple interfaces for a class ?
A) You cant inherit, but you can implement multiple interfaces for a class.

C# Interview Questions Part - 1

Types in .Net - Part 2

Wednesday, April 23rd, 2008

Types in .Net - Part 2

In a previous article “Types in .Net - Part 1“, we looked at Built In Value Types. In this part we will look at User Defined Value Types.

User Defined Value Types:
User Defined Value Types are called structs. ’struct’ is the keyword we use in C# to create them. You can define them just like a class as in:

public struct MyStruct
{
}

You cant inherit a struct. Structs are Value Types not reference types like classes ie they are created on the stack not on heap.

The other type of Value Type is enum. The following code shows how to define enum’s.


public enum MyEnum
{
Value1,
Value2,
Value3
}

Enum’s are actually Integers, but when you do a .ToString() on them you get the representation like “Value1″. Enums are useful when programming and when you have to force to pick up from a certain list of values.

You use enum as follows:

MyEnum myEnum = MyEnum.Value1;

In a later article we will take a look at Reference types, which would be the concluding article to this series.

Types in .Net - Part 2