Tuesday, September 16, 2014

Singleton in ASP.NET web application

Question: One part of the web application is designed using singleton pattern. So across all users this object will be one. Is it correct?

Answer: Yes. There will be only one instance.

Each request to an asp.net site comes in and is processed on it's own thread. But each of those threads belong to the same application. That means anything you mark as static is shared across all requests, and therefore also all sessions and users.


Tuesday, August 12, 2014

Static Class

  1. Cannot derive from Static Class
  2. Cannot create instance of Static class using new keyword
  3. Static class can be public or internal
  4. Static class can NOT be private or protected or protected internal
  5. Static class can NOT have non-static members

1) 
static class ThisClassIsStatic
{
    static string comment;
    static int count = 0;
}
class ThisIsDervied : ThisClassIsStatic   //NOT POSSIBLE
{
}

2)
ThisClassIsStatic obj = new ThisClassIsStatic(); //NOT POSSIBLE

3) 
public static class ThisClassIsStatic  //POSSIBLE
{
}

4)
private static class ThisClassIsStatic  //NOT POSSIBLE

5)
    static class ThisClassIsStatic
    {
        static string comment;
        static int count = 0;
        int index = 0; //NOT POSSIBLE

    }

C# Base

    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            Console.Read();
        }
    }

    class A
    {
        internal int x = 10;
        public int MyProperty { get; set; }
        public A()
        {          
            Console.WriteLine("A Constructor");
        }
    }

    class B:A
    {
        public B(): base()
        {                
            Console.WriteLine("B Constructor ");
        }
    }

What's the output of the above program?

Note: base keyword can be used ONLY from derived class's constructor to access base class members (non-private members)

Saturday, August 9, 2014

C# Extension Method - Sample

class Program
{
    static void Main(string[] args)
    {
        string s = "hello please tell me the word count";
        Console.Write(s.WordCount());
        Console.Read();
    }
}

public static class StringExtension
{
    public static int WordCount(this string value)
    {
        return value.Split(' ').Length;
    }
}

Wednesday, July 23, 2014

Cyclomatic Complexity

Cyclomatic Complexity:
  • Measures structural complexity of code
  • It is created by calculating number of different code paths in program
  • In visual Studio 2012, use “Calculate Code Metrics” to identify cyclomatic complexity
  • Cyclomatic Complexity values from

    • 1 to 10 is Simple program – Less risk
    • 11 to 20 is medium complex program – Moderate Risk
    • 21 to 50 is complex program – High Risk
    • >51 untestable program – Very high Risk


References:





Saturday, June 21, 2014

Application Level Exception Handling in ASP.NET

This can be done in Global.asax within Application_Error method.

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
 
    if (ex.GetType() == typeof(HttpUnhandledException))
    {
        if (ex.Message.Contains("NoCatch") ||
ex.Message.Contains("maxUrlLength") ||
ex.Message.Contains("File does not exist."))
            return;
           
     
        Response.Write("~/Error.aspx");
    }

    Server.ClearError();
}

Reference: http://msdn.microsoft.com/en-us/library/24395wz3.aspx

Tuesday, June 17, 2014

Difference Between Sql Server VARCHAR and NVARCHAR Data Type


  • varchar : Non-Unicode variable length character type
  • nvarchar : Unicode variable length character type

Note: nvarchar can store both non-Unicode and Unicode characters(Example: Japanese, Korean)

Monday, May 12, 2014

Relationships in SQL Server



One-To-Many
  • One DeviceClass can have many DeviceTypes
  • Key symbol represent one, infinity symbol represent many
  • Note: You don’t have to have many. 


Many-To-Many
  • Not as common as One-To-Many
  • Many-To-Many relationship is expressed with help of junction tables
  • A table name with a joint name of two tables


One-To-One
  • Least commonly used relationship
  • One row in one tables is related to, One row in other table

Wednesday, May 7, 2014

Is C# a 100% Object Oriented Programming Language?

C# is NOT a pure object oriented programming language. 

Object oriented language should support:
  1. Encapsulation
  2. Polymorphism
  3. Inheritance
  4. All predefined type are objects
  5. All operations performed by sending messages to objects
  6. All user-defined types are objects
C# does not support point#4 & 5. 

In C#, predefined type can be struct. Example: integer > public struct Int32

So C# does not qualify as pure object oriented language.




Reference:

Wednesday, February 19, 2014

.NET Framework 4 Features

Background garbage collection:
  • Provides background garbage collection 
  • Provide better performance.
  • It replaces concurrent garbage collection.
ETW (event tracing for windows):
  • This determines processor usage and memory usage estimates per app domain.

DLR (dynamic language run-time):
  • Support with System.Dynamic namespace. 
  • DLR built on top of CLR
  • DLR supports dynamic languages (IronPython, IronRuby)

Parallel programming:
  • Parallel programming is introduced
  • PLINQ
  • AsParallel 

Tuple:
  • Data structure consists of multiple parts
  • System.Tuple
  • Maximum of 8 items supported

BigInteger: is introduced with 8 byte storage size. int is 4 byte.


Reference: core new features and improvements


ASP.NET:


New Chart control
:


Routing:
  • Built-in support for routing. 
  • No need to specify physical file names.
Session:
  • Introduced compression option for out-process session state (state server/ SQL Server)

JQuery:
  • is included in script folder