C# .NET - explain abstract class,interface,delegates,procedures

Asked By karthi keyan on 24-Jul-08 08:43 AM
please brief about abstract class,interface,delegates,procedures with some real time examples? and explain where it is used in programing?
            

Reply

alice johnson replied to karthi keyan on 24-Jul-08 09:52 AM
A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

using System;

namespace Akadia.BasicDelegate
{
    // Declaration
    public delegate void SimpleDelegate();

    class TestDelegate
    {
        public static void MyFunc()
        {
            Console.WriteLine("I was called by delegate ...");
        }

        public static void Main()
        {
            // Instantiation
            SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

            // Invocation
            simpleDelegate();
        }
    }
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe

Go through these links for delegates
http://www.akadia.com/services/dotnet_delegates_and_events.html

go thru this link

Web Star replied to karthi keyan on 24-Jul-08 11:34 PM

http://www.aspspider.com/qa/Question3421.aspx

msdn.microsoft.com/en-us/library/aa288436.aspx

 

See this

Sagar P replied to karthi keyan on 25-Jul-08 12:34 AM

Abstract Class

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
E.g.:

using System;
abstract class
MyAbs
{
public abstract void
AbMethod1();
public abstract void
AbMethod2();
}
//not necessary to implement all abstract methods
//partial implementation is possible
abstract class
MyClass1 : MyAbs
{
public override void
AbMethod1()
{
Console.WriteLine("Abstarct method #1");
}
}
class
MyClass : MyClass1
{
public override void
AbMethod2()
{
Console.WriteLine("Abstarct method #2");
}
}
class
MyClient
{
public static void
Main()
{
MyClass mc =
new
MyClass();
mc.AbMethod1();
mc.AbMethod2();
}
}

Interface

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

See these links to know more abt abstract class n interface;

http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx

http://www.c-sharpcorner.com/UploadFile/rajeshvs/AbstractClassesNMethods11122005014357AM/AbstractClassesNMethods.aspx

Delegates

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.

A delegate declaration defines a type that encapsulates a method with a particular set of arguments and return type. For static methods, a delegate object encapsulates the method to be called. For instance methods, a delegate object encapsulates both an instance and a method on the instance. If you have a delegate object and an appropriate set of arguments, you can invoke the delegate with the arguments.

An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.

See this link to know abt delegates;

http://msdn.microsoft.com/en-us/library/aa288459(VS.71).aspx

See this link for procedures;

http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.apdv.sql.doc/doc/t0011399.htm

Best Luck!!!!!!!!!!!!!!
Sujit.

Abstract class, interface, delegates, procedures
Kalit Sikka replied to karthi keyan on 25-Jul-08 01:01 AM

Abstract Class

  • Abstract class defines functionality that is implemented by one or more subclass.
  • Abstract class states “what” to do, rather than “how” to do.
  • Abstract class cannot be instantiated.
  • Abstract method “must” be overridden in derived class.
  • Abstract classes can provide all, some, or none of the actual implementation of a class. It provides a default code or stub for their child class.
  • It is useful while creating Component.
Example:
abstract class shape
{
        void draw() { }
}
 
class circle : shape
{
        void draw()
        {
               Console.WriteLine(“Circle”);
        }
}
 
class rectangle : shape
{
        void draw()
        {
               Console.WriteLine(“rectangle”);
        }
}
 
class triangle : shape
{
        void draw()
        {
               Console.WriteLine(“triangle”);
        }
}
 
static void main()
{
        shape s;
        s = new circle();
        s.draw();
        s = new rectangle();
        s.draw();
        s = new triangle();
        s.draw();
}
Interface
  • An Interface is a group of constants and method declaration.
  • .Net supports multiple inheritance through Interface.
  • Interface states “what” to do, rather than “how” to do.
  • An interface defines only the members that will be made available by an implementing object. The definition of the interface states nothing about the implementation of the members, only the parameters they take and the types of values they will return. Implementation of an interface is left entirely to the implementing class. It is possible, therefore, for different objects to provide dramatically different implementations of the same members.
  • Example1, the Car object might implement the IDrivable interface (by convention, interfaces usually begin with I), which specifies the GoForward, GoBackward, and Halt methods. Other classes, such as Truck, Aircraft, Train or Boat might implement this interface and thus are able to interact with the Driver object. The Driver object is unaware of which interface implementation it is interacting with; it is only aware of the interface itself.
  • Example2, an interface named IShape, which defines a single method CalculateArea. A Circle class implementing this interface will calculate its area differently than a Square class implementing the same interface. However, an object that needs to interact with an IShape can call the CalculateArea method in either a Circle or a Square and obtain a valid result.
Practical Example
public interface IDrivable
{
   void GoForward(int Speed);
}
 
public class Truck : IDrivable
{
   public void GoForward(int Speed)
   {
      // Implementation omitted
   }
}
 
public class Aircraft : IDrivable
{
   public void GoForward(int Speed)
   {
      // Implementation omitted
   }
}
 
public class Train : IDrivable
{
   public void GoForward(int Speed)
   {
      // Implementation omitted
   }
}
Extra 
  • Each variable declared in interface must be assigned a constant value.
  • Every interface variable is implicitly public, static and final.
  • Every interface method is implicitly public and abstract.
  • Interfaces are allowed to extends other interfaces, but sub interface cannot define the methods declared in the super interface, as sub interface is still interface and not class.
  • If a class that implements an interface does not implements all the methods of the interface, then the class becomes an abstract class and cannot be instantiated.
Both classes and structures can implement interfaces, including multiple interfaces.
 
A delegate in C# is similar to a function pointer in C or C++ but they are managed and type safe. Using a delegate allows the programmer to encapsulate a reference of one or more methods that have identical signatures. 
The delegate object can then be passed to Event or Method which can call the referenced method, without having to know at compile time which method will be invoked.
 
There are four steps in defining and using delegates: 
Delegate has 4 steps:
1- Definition : public delegate void TestCallback();
2- Declaration: Testcallback tc;
3- Instantiation: tc= new Testcallback(Test.f);
4- Call: tc();
  1. A delegate represents a class.
  2. A delegate is type-safe.
  3. You can combine multiple delegates into a single delegate.
  4. You can use delegates both for static and instance methods.
  5. You can define delegates inside or outside of classes.
  6. You can use delegates in asynchronous-style programming.
  7. Delegates are often used in event-based programming, such as publish/subscribe.
Procedures:
A procedure or function is an object stored in the database, and run as a unit to solve a specific problem or perform a set of related tasks. Procedures and functions permit the caller to provide parameters that can be input only, output only, or input and output values. Procedures and functions let you combine the ease and flexibility of SQL with the procedural functionality of a structured programming language.

Procedures and functions are identical except that functions always return a single value to the caller, while procedures do not but could return value
help
Interface and Abstract Class C# .NET 10-Aug-13 06:25 PM Hai, When do we go for Interface. . When do we go for Abstract Class. . .Give one example Today i faced one interview . . He asked this question.I want answer with Example Thanks in Advance Abstract Class :: - It cannot defines all the methods - It has subclass. - Here
normal user point of view, what’s the Difference between Abstract class and Interface class? hi, Abstract Class: • Abstract Class Can contain Abstract Methods and Non- Abstract Methods. • When a
What is Interface & Abstract? C# .NET 10-Aug-13 06:21 PM What is Interface & Abstract? please explain with simple definition. Thanks & Regards H.Karthikeyan There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison: Feature Interface Abstract class Multiple inheritance A class may inherit several interfaces
what is difference between abstract class and interface class C# .NET 10-Aug-13 06:42 PM end of post See these articles for difference between abstract class and interface :: http: / / www.geekinterview.com / question_details / 15817 http: / / www.codeproject.com KB / cs / abstractsvsinterfaces.aspx Hope it helps. Abstract Class. . 1.It cannot defines all the methods 2.It
ASP.NET 10-Aug-13 06:28 PM What is abstract class & interface?when we use abstract class and interface? Hi Abstract classes are one of the essential behaviors provided by .NET and don't want anyone to create objects of these class types. You can make use of abstract classes to implement
abstract class and interface ASP.NET 10-Aug-13 06:21 PM What is abstract class and interface ? What is diffrence between them ? And in what instance we use abstract class and interaface . Please give some live exmple Hi, The most
interface, abstract class C# .NET 10-Aug-13 06:32 PM what is difference between abstract class and interface , please give one realtime example Hi, Interfaces are closely related to abstract classes that have all members abstract.  For an abstract class, at least one method of
abstract class C# .NET 10-Aug-13 06:27 PM what is the difference between abstract class and interface hi There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison
abstract , interface C# .NET 10-Aug-13 06:32 PM what is main difference between interface and abstract class please give me one example Abstract : = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 1. Abstract class cannot be instantiated. 2. Abstract class may contain abstract methods