Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
using System;

interface IPerl
{
    void Read();
}

class Test : IPerl
{
    public void Read()
    {
	Console.WriteLine("Read");
    }
}

class Program
{
    static void Main()
    {
	IPerl perl = new Test(); // Create instance.
	perl.Read(); // Call method on interface.
    }
}

----------
in above code the line ......
C#
IPerl perl = new Test(); // Create instance.

what does this mean

I can access the Read method in interface just by creating a local variable for IPerl as below
C#
Iperl perl;
perl.Read();

so what does new keyword mean or what does it do

normally we use new keyword like below
C#
Class1 MyClass  = new Class1();

but here
C#
IPerl perl = new Test();    // only Test() is class name , but on other side Iperl perl which is given which is not a class but interface.

can some explain what new keyword does.
Posted
Updated 26-Sep-12 19:58pm
v3

C#
IPerl perl = new Test();

Does just the same as
C#
Class1 MyClass = new Class1();

But the variable you assign the new instance to can hold any class which implements the IPerl interface, instead of just the specific Class1 type.

If you derived a class from Class1:
C#
public class Class2 : Class1 {}

You could happily do the same thing:
C#
Class1 myClassInstance = new Class2()

A reference to a class can contain any class derived from the specified type.
 
Share this answer
 
It looks like you are very new in such things, so you better need to read a good book on OOP. It's not so easy to explain from scratch, but I'll try.

With inheritance, we face such thing as run-time type vs. compile-time type. First, let's look at it without interfaces:

C#
class Base {
   internal A() { } 
}

class Derived : Base {
   internal B() { }
}

//...
Base instance = new Derived();

//can call:
instance.A();
//cannot call:
instance.B(); // but if you could, it would work, because actually during run time Base instance is Derived

//but work around is the typical abuse of OOP:
((Derived)instance).B(); // will certainly work in this simple example
//but it depends on previous run time, could cause "invalid typecast" exception


What is the type of instance? Compile-time type is Base; this is how compiler sees it. Run-time type is Derived, could be checked during run-time using operators is or as (find them by yourself in C# reference).

Now, interfaces. The interface to be implemented has the syntax of base class list, and indeed, interface is "inherited" by the implementing class as it was an abstract class, which is similar to interfaces in many aspects.
The interface can only be a compile-time type. During runtime, you can have objects which has the interface compile-time types, but their run-time type is always some class or structure implementing this interface. Interface is some type which is, it I may say so, "always abstract". In the context of your question, the situation is the same as with base and derived class.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900