Last Updated:

C# pronounced "C Sharp"

Among the new technologies announced by Microsoft in June and scheduled for presentation at the Microsoft Professional Developers Conference (PDC) is a programming language called C#. C# (billed as "Sharp") will be included in the next release of the Microsoft Visual Studio.NET programming environment. Modules written in C# will be compatible with modules written in Visual C++ and Visual Basic, thereby supporting cross-language development on the Microsoft .NET platform for the first time. Just as Visual Basic met the needs of Windows developers in the '90s, C# must meet the performance needs of .NET web applications and service developers. Modern programming languages are created from the experience and knowledge of their designers. And, the more people involved in the project, the wider the core of languages. Microsoft says that the definition of the C# language was derived from C and C++ and many elements of the language reflect this. C# is broader than Java because its designers used inheritance from C++ (of the structs type). Additionally, new features (such as source text versioning) have been added to C#. To understand all this more precisely, you can analyze the features of C# that clearly coincide with Java, which are derived from standard C and C ++. As you'll see later, the features that C# has borrowed from these languages will help you understand its structure.


Features of C# borrowed from Java

Classes

There are a lot of classes in C#, as in Java. Note the following example, which shows the use of classes:

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 C. Namespace contains the Console class, which is used in this example to output a string.

Classes can be abstract and finite: a class that is declared as abstract can only be used as a base class. The lock keyword (analogous to the final in Java) means that the class will not be abstract, but it also cannot be used as the basis of another class.

Interfaces

As in Java, the interface is an abstract definition of a collection of methods. When a class or structure executes an interface, it must execute all the methods defined in that interface. A single class can execute a number of interfaces.

Boolean operations

There is no direct conversion between a Boolean type and any other data type. The key words are: Boolean truth and falsehood.

Errors

As in Java, you can control error handling by capturing exception objects.

Memory Management

there is an automatic garbage collection that is provided by .NET.


C# Features borrowed from C and C++

Compilation

Programs compile directly into a standard binary executable form. If the previous Hello World program was saved in a Hello.cs text file, it will be compiled into a executable Hello.exe file.

Structure

C# structures are similar to those in C++ and should contain data definitions and methods. However, unlike C++, structures in C# are different from classes and do not support inheritance. However, like Java, structures can execute interfaces.

Preprocessor

There are preprocessor directives for conditional compilation, warnings, errors, and control. Pre-processing directives:

#define 
#undef 
#if 
#elif 
#else
#endif
#warning 
#error 
#line []

Operator overload

Some operators may be overloaded and some may not. In particular, none of the destination operators can be overloaded.

Overloaded unary operators

+ -! ~ ++ - true false

Overloaded binary operators

+ - * / % и | ^  > ==! = >  =

Features unique to C#


Definitions in namespace



When you create a program, you create one or more classes in namespace. In it (outside the class) it is possible to declare interfaces, enums and structs. Using keywords you can address the contents of another namespace.


Fundamental data types



C# has a wider variety of data types than C, C++, or Java. The types are bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double, and decimal. Like Java, all types have a fixed size. Like C and C++, all types can be signed or unsigned. Like Java, char contains a 16-bit unicode character. In C#, a new data type is the decimal type, which can contain up to 28 decimal digits.


Two fundamental classes



class object is the base class of all classes. The string class is also the base class. As part of the language, it is used by the compiler when you create a string in your program by enclosing it in quotes.


Assembly



Assembly - a collection of compilable classes and the ability to execute other elements of the language, which are combined in a separate file. If it is a program, the file has an .exe extension. If it is a library - DLL.



signs



Each class member has attributes: public, protected, internal, protected internal, or private.

  • public: available for all code.
  • protected: Available only from received classes.
  • internal: available only when assembling;
  • protected internal: Available only from retrieved classes within assembly.
  • private: only available from class.

Passing an argument

Methods can be declared to accept a number of arguments. By default, values are passed to fundamental data types. The ref keyword can be used to pass a value through a specific reference that allows the value to be returned. The out keyword also causes you to click the link without passing in a value.

Virtual Methods

Before a method in a base class can be rewritten, it must be declared virtual. The method in the subclass that will be rewritten must be declared using the override keyword. This will prevent the method from being accidentally overwritten. This feature improves the readability and ease of maintenance of C# modules.

Properties

A COM object has properties and therefore each C# class can be used as a COM object. C# allows you to define properties within any class. Inside a C# class, each property is given a name and data type. The set accessor and get accessor keywords are used to declare executable code when reading or updating a property. As an example, consider a class that has a Caption property:

public class Button: Control {
private string caption;
public string Caption {
get {
return caption;
}
set {
caption = value;
Repaint();
}
}
} 

The property name can be addressed externally in the destination claim:

Button b = new Button();
b.Caption = "ABC";
string s = b.Caption;
b.Caption += "DEF" 

Assigning b.Caption calls the set method. Assigning a value from b.Caption calls the get method. The + = operation calls both of these methods. The property addresses the contents of an individual field in the class.

Indexer

the indexer is similar to a property except that instead of a name, an indexed value inside square brackets (as an array) is used to address a class member.

public class ListBox: Control {
private string[] items;
public string this[int index] {
get {
return items[index];
}
set {
items[index] = value;
Repaint();
}
}
} 

Iterator can be used to address members of internal arrays:

ListBox listBox = ...;
listBox[0] = "hello";
Console.WriteLine(listBox[0]);

Delegate and callback

The delegate object contains the information needed to call a specific method. You can access the delegate object to query the represented method securely. The callback method is an example of a delegate. The event keyword is used in defining methods that are called when an event occurs.

Identifying Versions

C# allows developers to support multiple versions of classes in binary form by placing them in different namespaces. This allows both old and new versions of the software to run simultaneously. Along with this, C# will have the ability to maintain and manage multiple versions of source code.

Validated and Unverified Assessment

validated expression - an expression that throws an exception when it goes beyond its limits. An unverified expression is an expression that does not throw an exception. The keywords checked and unchecked are used to explicitly define how the assessment was performed:

int j = checked(a * b);
int k = unchecked(a * b);

Explicit and implicit transformations

Like Java, C# takes into account implicit conversion of fundamental data types as long as there is no probability of data loss (byte type conversion to int), but if there is a probability of data loss (int to byte conversion), an explicit conversion is performed. C# extends this ability to other elements of the program by allowing the programmer to define both explicit and implicit transformations. For example, the following Digit structure can be implicitly assigned to a byte type, but must be explicitly defined to assign another Digit:

public struct Digit {
byte value;
public Digit(byte value) {
if(value  9)
throw new ArgumentException();
this.value = value;
}
public static implicit operator byte(Digit d) {
return d.value;
}
public static explicit operator Digit(byte b) {
return new Digit(b);
}
}

Externally executable methods

Methods in a class can be executed externally. In the following example, the static RemoveDirectory method runs in a library named kernel32.dll:

class Path {
[DllImport("kernel32", setLastError=true)]
static extern bool RemoveDirectory(string name);
}

Iterate through collection members

The foreach claim can be used to execute a block of code once for each member of a list or array. The following example loops once in the WriteList() method for each ArrayList member:

using System;
using System.Collections;
class Test {
static void WriteList(ArrayList list) {
foreach(object o in list)
Console.WriteLine(o);
}
static void Main() {
ArrayList list = new ArrayList();
for(int i = 0; i

Conclusion

Any experienced Windows programmer will certainly find something of interest in the list of C# language features. I especially like the language's indexing features and abilities. There are some new features compared to Java. The fixed-size data types (32-bit int and 64-bit long) are not only highly mobile, but also make programming easier because you always know exactly what you're dealing with. The automatic "collection" of garbage is also very convenient. While all of these language features seem very attractive, it's still pretty early to compare C# to C++ or Visual Basic. However, I like C# and if a good implementation of it follows, then I think .NET developers will gravitate towards this new tool.