Tuesday, 22 April 2014

What you need to know about Static class

Leave a Comment

Static Class Properties

  • A static class cannot be instantiated. That means you cannot create an instance of a static class using new operator. 
  • A static class is a sealed class. That means you cannot inherit any class from a static class. 
  • A static class can have static members only. Having non-static member will generate a compiler error.
  • A static class is less resource consuming and faster to compile and execute. 

Static Class Example

Public static class MyStaticClass 
{ 
    Private static int myStaticVariable; 
    Public static int StaticVariable; 
    { 
       Get 
       { 
          return myStaticVariable; 
       }  
       Set 
       {  
         myStaticVariable = value; 
       } 
   } 
    Public static void Function() 
    { 
    } 
} 

Note : The static class can directly called out from the other class for example you create class ClassA, and you can directly type in ClassA to call Function method in class MyStaticClass like this.
MyStaticClass.Function();


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
Read More...

Nullable Types in .Net C#

Leave a Comment
Since .NET version 2.0, the new feature introduce which is nullable types.  In the basic programming, how do you assign null to the promitive type such as int, long, and bool. Its is impossible.

The following code shows how to define an integer as nullable type and checks if the value of the integer is null or not.

Example 

/// <summary>
/// Nullable types
/// </summary>
public static void TestNullableTypes()
{
   // Syntax to define nullable types
   int? counter;
   counter = 100;

   if (counter != null)
   {

      // Assign null to an integer type
      counter = null;
      Console.WriteLine("Counter assigned null.");
   }
   else
   {

      Console.WriteLine("Counter cannot be assigned null.");
      Console.ReadLine();
   }
}



If you remove the "?" from int? counter, you will see a warning as following:



By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
Read More...

Friday, 4 April 2014

How to enable GZip or Deflate on ASP.NET(HTTP Compression)

Leave a Comment
HTTP compression is a capability that can be built into web servers and web clients to make better use of available bandwidth, and provide greater transmission speeds between both.

HTTP data is compressed before it is sent from the server: compliant browsers will announce what methods are supported to the server before downloading the correct format; browsers that do not support compliant compression method will download uncompressed data. Compression as defined in RFC 2616 can be applied to the response data from the server (but not to the request data from the client)

The most common compression schemas include gzip and deflate.

So how do we gonna achieve this  HTTP Compression on asp.net application.?

Here is the Step :
  1. Open Global.asax file.
  2. Add the method IsCompressionAllowed() to check if compression is allowed.
  3. Add code in Application_Start event.

Global.asax File

void Application_Start(object sender, EventArgs e)
        {
            //check if request is aspx page.. if javascript etc ...that file cannot be compressed
            string sTheUrl = HttpContext.Current.Request.Url.LocalPath;
            string[] siTheUrl = sTheUrl.Split('?');
            string sUrl = siTheUrl[0].ToLower();
            if (sUrl.IndexOf(".aspx") <= 0)
            {
                goto End;
            }

            if (IsCompressionAllowed()) // If client browser supports HTTP compression   
            {
                //Browser supported encoding format      
                string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
                //use deflate first if deflate not available use gunzip since deflate more efficient (41% more faster than gzip)

                if (!string.IsNullOrEmpty(AcceptEncoding))
                {
                    if (AcceptEncoding.Contains("deflate"))
                    {
                        HttpContext.Current.Response.Filter = new DeflateStream(HttpContext.Current.Response.Filter, CompressionMode.Compress);
                        HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
                    }
                    else
                    {
                        HttpContext.Current.Response.Filter = new GZipStream(HttpContext.Current.Response.Filter, CompressionMode.Compress);
                        HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
                    }
                }
            }

            // Allow proxy servers to cache encoded and unencoded versions separately  
            //Response.AppendHeader("Vary", "Content-Encoding");
            HttpContext.Current.Response.AppendHeader("Vary", "Content-Encoding");

        End:
            char hello;

        }

        private bool IsCompressionAllowed()
        {
            //Browser supported encoding format   
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(AcceptEncoding))
            {
                if (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate"))
                    return true; ////If browser supports encoding, is either one
            } 
          return false;
        }


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
Read More...

Tuesday, 1 April 2014

Basics of .NET Programs and Memory Management

Leave a Comment
.NET has made a lot of improvements in its core to enhance the performance of an application. It is the first solid-core environment that really connects to all parts of the technology for developers. Inorder to make solid footsteps in the .NET environment, it would be great to know about the basic advantages and also the architecture of the .NET environment that you are going to start working on.

The most important infrastructural component of the .NET framework is Common Language Runtime (CLR). The code that you write inside the framework is called the managed code, which benefits from cross-language integration, cross-language exception handling, enhanced security,  versioning, and easy deployment support. Every component in a managed environment conveys some specific rule sets. When you write your code in a managed environment, the code gets compiled into the common intermediate language (CIL).
This language remains constant or equivalent to a code that might be compiled from another language. The commonality of languages is maintained in a managed environment by the use of an intermediate language. The languages that are built on top of CIL follows rules defined on the Common Language Specification (CLS). The Diagram below demonstrate the fact :


In the diagram, the source code from any .NET language is converted to CIL code with help from the respective supported compilers. To compile a C# source code, we have CSC compiler and similarly we have a VBC compiler for VB .NET code. The CIL is then again converted to native code using the JIT compiler. The JIT (just-in-time) compiler converts the intermediate language to a machine-dependent code when it is executed during runtime.

Thus, you can copy an assembly to any platform that has the runtime installed and the assembly will work in the same way. The introduction of a multicore JIT implementation with CLR makes applications run almost at the same pace as running native code.
The CLR is supported by Common Type System (CTS). When you consider the languages in .NET, each of them is backed up with a wide set of libraries available in every language. This library is a set of assemblies installed when the .NET framework is loaded in a system, and when it forms a major portion of any program, we built on top of it. Even though the framework allows you to write code in different languages, the internal type system remains constant for every language.

For example, Integer of VB .NET is mapped to the same type in CLR as to int of C#. Both of them point to the System.Int32 type in a framework.

The language that exists in the .NET environment is built on top of CLR such that the compiler that compiles the source code should produce the CIL which follows the ECMA standardization specified. There are a number of languages that are not built by Microsoft and are not shipped with the .NET framework which include Lsharp, Boo, A Sharp, Fantom, and so on, which are built on top of the .NET framework and which follows the ECMA standards. The internal bits of an assembly built by these language compilers are exactly same and can be identified by the JIT compiler easily.

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)
Read More...

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.