C# Versus Java
By Marc Eaddy, February 01, 2001
Microsoft describes C# ("C sharp") as a "simple, modern, object-oriented, and type-safe programming language derived from C and C++." That statement would apply equally well to Java. In fact, after comparing the two languages, it's obvious that prerelease descriptions of C# resemble Java more than C++. As Example 1 illustrates, the language features and syntax are similar. Example 1(a) is the canonical "Hello World" program in Java, while Example 1(b) is the program in C#.
Feb01: C# Versus Java
<b>(a)</b>
String name;
public String getName() { return name; }
public void setName(String value) { name = value; }
Ex:
obj.setName("Marc");
if (obj.getName() != "Marc")
throw new Exception("That ain't me!");
<b>(b)</b>
string name;
public string Name {
get { return name; }
set { name = value; } // 'value' is the new value
}
Ex:
obj.Name = "Marc";
if (obj.Name != "Marc")
throw new System.Exception("That ain't me!");
Example 3: Implementing the Name property using the name field: (a) in Java; (b) in C#.