Introduction
ThisArticle explains about why do we want to use C#,why not Java. C# has class members, such as properties, indexers, and events. A property is sometimes called a smart field; it has a get accessor to customize value retrieval and a set accessor to customize setting the value.
Sample Code for Properties and Indexers.
private short foo;
public short Foo { get { return foo; } set { foo = value; } }
The previous code could be used like this (assuming that the property is a member of MyClass):
MyClass.Foo = 25;
Short myField = MyClass.Foo;
And here's an example of an indexer, which enables a class to provide array-like semantics.
private ArrayList junk = new ArrayList();
public this[int index] { get { return junk[index]; } set { junk[index] = value; } }
The previous code could be used like this (assuming that the property is a member of MyClass):
MyClass.Foo = 25;
Short myField = MyClass.Foo;
And here's an example of an indexer, which enables a class to provide array-like semantics.
private ArrayList junk = new ArrayList();
public this[int index] { get { return junk[index]; } set { junk[index] = value; } }
The previous code could be used like this (assuming that the property is a member of MyClass):
MyClass[5] = "somestring";
string myString = MyClass[2];
Indexers and properties don't look like any of Java's syntactical constructs.
It would be difficult for C# to mimic Java with overloaded operators such as these:
public static MyClass operator+(MyClass bar1, MyClass bar2) { // some implementation }
That's especially true because Java doesn't have overloaded operators.
Object Semantics
One of the primary concepts of C# is that, in many places, it forces a programmer to explicitly specify his intent. This eliminates a class of errors associated with assumption of default behavior in languages such as Java. For example, for polymorphism to occur in C#, a normal base class must declare a method as virtual, and the derived class must declare the overriding method with the overrides keyword. Without this explicit declaration, all calls to a base class reference execute a base class method, even if the actual object type is a derived class. Here's a C# method declaration:
class Bar { public Bar() { MyMethod(); }
public void MyMethod() { Console.WriteLine("Bar.MyMethod()"); } }
class Foo : Bar { static void Main(string[] args) { Bar myFoo = new Foo(); }
public void MyMethod() { Console.WriteLine("Foo.MyMethod()"); } }
This produces "Bar.MyMethod()". This shows how C# produces a well-versioned implementation. Now here's the Java implementation:
class Bar {
public Bar() { MyMethod(); }
public void MyMethod() { System.out.println("Bar.MyMethod()"); } } public class Foo extends Bar { public static void main(String[] args) { Bar myFoo = new Foo();
}
public void MyMethod() { System.out.println("Foo.MyMethod()"); } }
This produces "Foo.MyMethod()". It also reveals potentially serious versioning problems. Say that class Bar belongs to a third-party library, which doesn't implement MyMethod() in version 1.0, and the only MyMethod() called is to an instance of class Foo. The problem occurs when the third-party library updates class Bar to version 2.0 and adds MyMethod(). Because of implicit polymorphism, any call in class Bar to MyMethod() will accidentally invoke Foo.MyMethod().
Summary
So,The above explained topics are specially designed for .net users. In c# everything is object. So,It has rich object orientation not like as Java.
|
| Author: Anil Rajan 12 Apr 2005 | Member Level: Gold Points : 0 |
dear Shaik Abdullah K
please check this code that you have given class Bar { public Bar() { MyMethod(); }
public void MyMethod() { Console.WriteLine("Bar.MyMethod()"); } }
class Foo : Bar { static void Main(string[] args) { Bar myFoo = new Foo(); }
public void MyMethod() { Console.WriteLine("Foo.MyMethod()"); } }
first thing is it won't work ,secondly ,the output won't be Bar.MyMethod()". after proper implementation like this
class Bar { public Bar() { MyMethod(); }
public virtual void MyMethod() { Console.WriteLine("Bar.MyMethod()"); } }
class Foo : Bar { static void Main(string[] args) { Bar myFoo = new Foo(); Console.ReadLine(); }
public override void MyMethod() { Console.WriteLine("Foo.MyMethod()"); } }
|