Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Tuesday, May 15, 2012

What is a partial class and how to use in c sharp

It was introduced under 2.0 framework which allows you to split a class o n to multiple files
Advantages:
Splitting huge volumes of code into multiple files which makes managing easier.Allows multiple programmers to work on the same class at the same time.grouping related code of a class in separate files
Here i will show how to develop a partial class in csharp.For this add a class to the project naming it as File1.cs and write the following code
partial class parts
{
public void test1()
{
Console.Writeline("method1");
}
public void test2()
{
Console.Writeline("method2");
}
}
add one more class naming it as File2.cs
 partial class parts { public void test3() { Console.Writeline("method3"); } public void test4() { Console.Writeline("method4"); } } Now to test the class add one more class to the project naming it as test parts.cs and write the following class testparts { static void main() { parts p= new parts(); p.test1(); p.test2(); p.test3(); p.test4(); Console.ReadLine();
} }

Monday, May 14, 2012

What is Interface and how to use it in asp.net

In object oriented programming the code can be defined under another container known as interface.which can be defined with only abstract methods in it.Inheritance is organised as implementation and interface inheritance .Implementation inheritance is achieved through classes which is always single.
Interface inheritance is achieved through an interface which is multiple .Multiple inheritance is supported through interfaces under java and .net languages
[<modifiers>]interface<name>
{
<abstract members>;
}
-->An interface cannot contain variables in it
-->members of an interface by default public
-->method under interface are by default abstract
-->while implementing the methods of an interface under the class no need of using the override keyword
-->An interface can inherit another interface


Interface Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
class Program:bhaskar1,bhaskar2
{
static void Main(string[] args)
{
Console.WriteLine("Create my first interface");
Program p = new Program();
p.printbhaskar1();
p.printbhaskar2();
}
public void printbhaskar1()
{
Console.WriteLine("FirstMethod Implementation");
}
public void printbhaskar2()
{
Console.WriteLine("Second Method Implementation");
Console.ReadKey();
}
}
interface bhaskar1
{
void printbhaskar1();
}
interface bhaskar2
{
void printbhaskar2();
}
}

Thursday, April 19, 2012

Difference between constructor and destructor in c#

Constructor:
*It is a special type of method  which will be executed automatically while crating an object
*Constructor name must be same as class name with out return type
syntax:
public class_name()
{
}
*These are overlodabale
*These are used to initialize the variables to open files or database connection etc
Destructor:
*It will be executed automatically while destroying object
*The name of destructor  must be same as class name with a ~(tild) prefix and with out return type and  with out access specifiers
syntax:
~class_name()
{
}
*These are not over lodable
*It is used to de-allocate the memory

Saturday, February 4, 2012

difference between class and structure

Class:
1)It is a referece type
2)Memory is allocated on managed Heap
3)They are not faster in access
4)New is mandatory for creating an object of a class
5)default constructor is option in under a class
6)It can be declared with variables and assigned with values while declaration
7)class provides both implementation as well as interface inheritance
The clssses for implementaion of complex business logics

Structure:
1)It is a value type
2)Memory is allocated on stack
3)They provide faster in access
4)It is optional while creating an object of a class
5)default constructor is mandatory which will be idebtified by compiler
6)It can be always declared with variables but can not be initiallised while declaration
7)it provides only interface inheritance
The strucutres for implementaion of small business logics

Monday, November 28, 2011

Static methods in csharp

A method declaring using the static keyword is a static method rest of all instance only.While defining static methods,we should not refer to any instance member of class directly,but we can use them by creating the object of the class.Non static members of a class cannot be referred from static blocks with out object reference.
Example:
class example
{
 int x=10;
 static int y=20;
 static void add()
  {
    example e=new example();
    console.writeline(e.x+y);
  }
}


Extension Methods in c#

It is an approach which can be used for extending the functionalities of a class even if the source code of the class is not available.Extension methods can be used for adding new methods to a class using static class.After defining an extension method these methods can be accessed using in object of the actual class to which they were added.To define an extension method we need to fallow the below rules and regulations

1.An extension method should always per-defined under a static class
2.As a methods are defined under the static class they need to be defined as static methods.but when they are found to actual class they can converted to instance methods.Every extension methods should have one mandatory parameters.e The class to which the method should be bound.A single static class can add extension methods to any no of classes

Example:
class extension
{
  public int a=10;
  public void display1()
   {
    console.writeline("method1");
   }
  public void display2()
   { 
    console.writeline('method2");
   }
  static void main()
   {
    extension e=new extension();
    e.display1();
    e.display2();
    console.readline();
   }
}
Add one more class to project naming it as Staticclass.cs.In this
public static void display3(this extension e)
{
 console.writeline("method3");
}
public static void display(this extension e,int x)
{
console.writeline("method4+"x");
}
public static void display(this extension e)
{
console.writeline("methods5"+e.a);
}
After defining three extension methods now the class extension contains five methods in it .you can test it by adding a new class the project and naming it as text extension.cs and with fallowing
static void main()
{
extension e=new extension();
e.display1();
e.display2();
e.display3();
e.displa4(10);
e.display5();
}

Bel