Search Forum
(57229 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

C# - A Hype Or Reality?
By syzack@yahoo.com

C#, (pronounced "C sharp") is a new programming language announced by Microsoft in June, and scheduled to debut at the Microsoft Professional Develper’s Conference(PDC). C# is Microsoft researcher Anders Hejlsberg's latest accomplishment. C# looks astonishingly like Java; it includes language features like single inheritance, interfaces, nearly identical syntax, and compilation to an intermediate format. But C# distinguishes itself from Java with language design features borrowed from Delphi, direct integration with COM (Component Object Model), and its key role in Microsoft's .Net Windows networking framework.

In this article, I will examine common motivations for creating a new computer language, and speculate on which might have led to C#. Next I will introduce C# with regard to its similarities to Java, c, c++. Then I will discuss a couple of high-level, fundamental differences in scope between Java and C#. I close the article by evaluating the wisdom (or lack thereof) in developing large applications in multiple languages, a key strategy for .Net and C#. Currently, C# and .Net are available only as a C# language specification (not yet in final form), a "d Preview" for Windows 2000, and a quickly growing corpus of articles on MSDN.

Microsoft says that the c# language definition has been primarily derived from C and C++, and many elements of the language reflect that. C# is broader than Java in what its designers chose to inherit from C++ (such as structs), and it also adds new features of its own (such as source code versioning). But it's too premature to write off Java. C# is yet to evolve into a language that is adopted and taken up by the developers' community. But the hype that Microsoft is creating around its new language is worth taking note of. But the response at the moment is: "It is a riposte to Java."

C# is more like Java than anything else, in spite of Microsoft's silence on the subject. This is to be expected, I suppose, given the recent success of the latter and the gains in productivity over C++ that have been reported by the companies that use it.

The kind of impact and the acceptance levels that Java has been able to build is clearly evident by the number of programmers (estimates say 2.5 million developers are using Java worldwide) working on this language and platform. The number of applications that have emerged through this language is mind-boggling and has penetrated to every level of computing, including wireless and mobile phones (remember the launch of Java phones in Japan). Can C# create this level of acceptance among the user community?. We have to wait and watch as rightly pointed out by Kalpathi S. Suresh, chairman and CEO of SSI Ltd, "I find all these incremental. If C# isn't there, we could always get (back) to Java or C and C++. These are not exactly new technologies; they are more of marketing hype created by big companies. We'll have to give them time to settle and see if these will really have any impact on the IT industry."

C# Features Derived From Java

Classes: Classes in C# are declared very much as they are in Java. This makes sense because experience has taught us that the Java model works very well. The Java keyword import has been replaced by using, which performs the same basic function. The point at which a class begins execution is the static method Main(). The following Hello World program demonstrates the basic form:

using System;
class Hello {
 static void Main() {
  Console.WriteLine("Hello, world");
 }
}
In this example, the name System refers to a namespace that contains a set of basic C# utility classes. The namespace contains the Console class, which is used in this example to output a string.
· Classes can be abstract and final: A class that is declared as abstract cannot be instantiated; it can only be used as a base class. The C# keyword lock is like the Java keyword final, which declares a class to be non-abstract, but it also cannot be used as the base of another class.
· Interfaces: Just as in Java, an interface is an abstract definition of a collection of methods. When class or struct implements an interface, it must implement all of the methods defined in the interface. A single class can implement a number of interfaces. There may be some subtle differences that surface later, but this feature appears to be unchanged from the one in Java.
· Boolean operations: Conditional expressions result in the bool data type, which is a distinct type in the language. There is no direct conversion between bool and any other data type. The Boolean constants true and false are keywords in the language.
· Errors: As in Java, throwing and catching exception objects manage error handling.
· Memory management: There is automatic garbage collection, which is provided by the underlying .NET framework.

C# Features Derived From C and C++

· Compilation: Programs compile directly into the standard binary executable form. If the previous Hello World program were to be stored in a text file named Hello.cs, it could be compiled into an executable named Hello.exe.
· Structs: A C# struct is like a C++ struct in that it can contain both data declarations and methods. However, unlike C++, structs are distinct from classes and do not support inheritance. However, like Java, a struct can implement interfaces.
· Preprocessor: There are preprocessor directives for conditional compilation, warnings, errors, and line control. The preprocessing directives available are:

#define 
#undef 
#if 
#elif 
#else
#endif
#warning 
#error 
#line [] 
There is no #include directive. There is no assigning of values to symbols on the #define statement, so there is no source code substitution--the symbols are only for use on the #if and #elif directives. The number (and optional name) on the #line directive modifies the line number and source file name reported by #warning and #error.
· Operator overloading: Some operators can be overloaded, and some cannot. In particular, none of the assignment operators can be overloaded. The overloadable unary operators are:
+ - ! ~ ++ -- true false
And the overloadable binary operators are:
+ - * / % & | ^ << >> == != > < >= <=

Features Unique to C#

C#'s most intriguing facets are its differences from Java, not its similarities. This section (and much of Part 2 of this series) covers features of C# that Java implements differently or entirely lacks.
· Intermediate language : Microsoft is very flexible about choosing when MSIL is compiled to the native machine code. The company takes care to say that MSIL is not interpreted, but compiled to machine code. It also understands that many -- if not most -- programmers accept the idea that Java programs are inherently slower than anything written in C. The implication is that MSIL-based programs (written in C#, Visual Basic, "Managed C++" -- a version of C++ that conforms to the CLS -- and so on) will outperform "interpreted" Java byte code. Of course, this has yet to be demonstrated, since C# and other MSIL-producing compilers have not yet been released. But the ubiquity of JIT compilers for Java make Java and C# relatively equal in terms of performance. Statements like, "C# is compiled and Java is interpreted," are simply marketing spin. Java byte code and MSIL are both intermediate assembly-like languages that are compiled to machine code for execution, at runtime or otherwise.
· Declarations in the namespace: When you create a program, you create one or more classes within a namespace. In this same namespace (outside of a class) it is also possible to declare interfaces, enums, and structs. The using keyword must be employed to address the contents of other namespaces.
· The fundamental data types: There are a wider variety of data types than found in C, C++, or Java. The types are bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double, and decimal. Like Java, all of the types are of a fixed size. Like C and C++, there are both signed and unsigned versions of each. Like Java, a char contains a 16-bit Unicode character. New with C# is the decimal data type, for monetary data, that can contain up to 28 significant base 10 digits.
· The two fundamental classes: A class named object is the base class of all other classes. A class named string is as much a part of the language as object. Being a part of the language makes is possible for the compiler to use it--whenever you code a quoted string in your program, the compiler creates a string object to hold it.
· Argument passing: Methods can be declared to accept a variable number of arguments. The default is to pass fundamental data types by value. The ref keyword can be used to force an argument to be passed by reference, which allows the argument to accept a return value. The out keyword also causes a pass by reference, except no initial value is expected.
· COM integration: The biggest win for Windows programmers with C# may be its painless integration of COM, Microsoft's Win32 component technology. In fact, it will eventually be possible to write COM clients and servers in any .Net language. Classes written in C# can subclass an existing COM component; the resulting class can be used as a COM component too, and can then be subclassed in, for example, JScript, to provide yet a third COM component. The result is an environment in which components are network services, subclassable in any .Net language.
· Indexer: An indexer is similar to a property except that, instead of a property name, an indexing value inside square brackets (like an array subscript) is used to anonymously address a member of a class.

public class ListBox: Control {
 private string[] items;
 public string this[int index] {
  get {
   return items[index];
  }
  set {
   items[index] = value;
   Repaint();
  }
 }
} 
The iterator can be used to anonymously address members of the internal arrays of strings like this:
ListBox listBox = ...;
listBox[0] = "hello";
Console.WriteLine(listBox[0]);
· Delegates and callbacks: A delegate object contains information required to call a specific method of a specific object. Think of it as a smart method pointer. The delegate can be passed to another location, and accessed to make a type-safe call to the represented method. A callback method is an example of a delegate. The event keyword is used in the declaration of methods that are to be the delegates called when an event occurs.

Conclusion

The similarity between C++ and C# is a great benefit to any organization with an existing training investment in C and C++. Programmers accustomed to C++ will have no trouble understanding C#. Moreover, Windows programmers who invested time learning Java (often in the form of Visual J++) will come up to speed on C# even more quickly than C++ programmers. The common the theme in the programming world is "If you learned Java, and you know C++, you've basically got 90% of what you need for C#."
I hope this has given you a feel for where C# stands in relation to Java and C++. Overall, I believe C# provides greater expressiveness and is more suited to writing performance-critical code than Java, while sharing Java's elegance and simplicity, which makes both much more appealing than C++.