Showing posts with label Nov 18. Show all posts
Showing posts with label Nov 18. Show all posts

Tuesday, November 18, 2008

Partial Class Example and Definition

Program.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
}
public partial class sub
{
int a,b,c;
public void method()
{

a=10;
b=20;
c=a+b;
}
}
public partial class sub
{
public void display()
{
method();
Console.WriteLine("C Value is :"+ c);
Console.ReadLine();
}
static void Main(string[] args)
{
sub sd=new sub();
sd.display();
sd.meth();
}
}
}

Class1.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Class1
{
}
public partial class sub
{
public void meth()
{
method();
Console.WriteLine("C value is:" + c);
Console.ReadLine();
}
}
}

  • Partial Class name should be same in both Program.cs and Class1.cs file.
  • We can use the method from one class file to another class file.
  • Easily Debugging.
  • Sharing the class is easy.
  • Multiple developers can work at a time.
  • But the compiler merges these 2 classes and it will compile.
  • The assembly should unique for these 2 class files.

Monday, November 17, 2008

Partial Classes in Asp.Net

The newly introduced “partial” keyword can be used to split a single class, interface, or struct into multiple, separate files in ASP.NET (using C# as the language). This permits multiple developers to works on these files, which can later be treated as a single unit by the compiler at the time of compilation and compiled into MSIL code. Partial classes can improve code readability and maintainability by providing a powerful way to extend the behavior of a class and attach functionality to it. This article discusses partial classes and how to use them in our ASP.NET applications in a lucid language (with code examples, wherever necessary).

Partial Classes in ASP.NET

Using partial classes helps to split your class definition into multiple physical files. However, this separation does not make any difference to the C# compiler, as it treats all these partial classes as a single entity at the time of compilation and compiles them into a single type in Microsoft Intermediate Language (MSIL). ASP.NET 2.0 uses partial classes as the code-beside class (a new evolution of the code behind model). It is generated by ASP.NET 2.0 to declare all the server side controls you have declared in your ASPX file. The following is an example of a partial class definition:

using System;

using System.Web.UI;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

}

Following are the major benefits of using partial classes:

  • Isolation of the business logic of an application from its user interface
  • Facilitates easier debugging

Implementing Partial Classes

We can split one part of a class in one file and the other part in some other file using the partial keyword. The compiler merges these partial classes spread across one or more source files into a single compiled class at the time of compilation — provided all these partial types are available. The following code examples illustrate this better:

Employee1.cs

public partial class Employee

{

public void ReadData()

{

//Some code

}

}

Employee2.cs

public partial class Employee

{

public void SaveData()

{

//Some code

}

}

class Test

{

public static void Main(string[] args)

{

Employee employee = new Employee();

//Some Code

employee.ReadData();

//Some code

employee.SaveData();

}

}

Points to Be Noted

This section discusses some of the most important points that we should keep in mind when working with partial classes or types. Note that all partial types are defined using the keyword “partial”. Further, the “partial” keyword should precede the type signature definition. Note that the partial keyword applies to classes, structs, and interfaces, but not enums. It also does not apply to classes that extend the system classes or types. According to MSDN, “It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled”. The partial types for a specific type should be located in the same module or assembly.


Saturday, November 1, 2008

Data Caching:

Caches the objects programmatically.
For Data caching asp.net provides a cache object for Ex:cache["States"]=dsStates;

Data Caching:

Caching:

Caching is a technique widely used in computing to increase performance by keeping frequently accessed or expensive data in memory.
In context of web application, caching is used to retain the pages or data across HTTP requests and reuse them without the expense of recreating them.
Asp.net has 3 kinds of caching strategies Output Caching, Fragment Caching and Data Caching