Showing posts with label .Net. Show all posts
Showing posts with label .Net. Show all posts

Sunday 22 July 2018

What is Extension Methods in .Net?

​​
In this post, I will explain about What is Extension Methods in .net?.  An extension method is a method which used to extend a type, such as string or integer, without recompiling or modifying the original type. In essence, they are a type of static method, but they are called as if the method is native to the type. Extension methods are available from the 3.5 version of the .NET Framework and can be implemented on any type in the .NET Framework or any custom type that you define.

An extension method is a static method of a static class that can be invoked using the instance method syntax. Extension methods are used to add new behaviours to an existing type without altering. In the extension method, "this" keyword is used with the first parameter and the type of the first parameter will be the type that is extended by the extension method.

An extension method having the same name and signature like as an instance method will never be called since it has low priority than an instance method. The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in the same class file using directives. The compiler will cause an error if you will try to call one of them extension method. An extension method is only called when there is no native method found.
​​
Definition of extension method

Public Static class <class_name>
{
​​
        Public static <return_type>  <method_name>(this <type> <type_obj>)
        {
        }
}

Wednesday 24 May 2017

Asp.Net Web Configuration File (Web.Config)

In this post, I will explain about Asp.Net Web Configuration File (Web.Config).

Web.Config is the main settings and configuration file used for an Asp.Net web application. Web.Config file is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way, you can configure settings independently from your code. Asp.Net web application contains a single Web.Config file in the root directory. An application can have more than one web.config file in a different directory. Asp.Net Configuration file is used to describe the properties and behaviours of Asp.Net applications. It is an XML document.
 
Benefits of XML-based Configuration files 
  • Asp.Net Web.Config file is extensible and application-specific information can be stored and retrieved easily. It is human readable.
  • No need to restart web server when settings are changed in a configuration file. Asp.Net automatically detects the changes and applies them to the running Asp.Net application.
  • Any standard text editor or XML parser can be used to create and edit Asp.Net configuration files.

What web.config file contains?
There is a number of important settings that can be stored in the Web.Config file. Some of the most frequently used configurations inside Web.Config file is:
  • Database connections
  • Caching settings
  • Session States
  • Error Handling
  • Security

Example of web.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation   defaultLanguage="c#"  debug="true"   />
    <customErrors   mode="RemoteOnly"  />
    <authentication mode="Windows" />
    <authorization>
      <allow users="*" />
    </authorization>
    <trace enabled="false" requestLimit="10" pageOutput="false"
        traceMode="SortByTime" localOnly="true" />
    <sessionState  mode="InProc" cookieless="false"  timeout="20"  />
    <globalization  requestEncoding="utf-8"  responseEncoding="utf-8"  />
  </system.web>
</configuration>