2. C SHARP It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270). C# is one of the programming languages designed for the Common Language Infrastructure. "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch. C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, who was previously involved with the design of Turbo Pascal, Embarcadero Delphi.
3. Design Goals of C# C# language is intended to be a simple, modern, general-purpose, object-oriented programming language. The language, and implementations thereof, should provide support for software engineering principles such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection. The language is intended for use in developing software components suitable for deployment in distributed environments. Source code portability is very important, as is programmer portability, especially for those programmers already familiar with C and C++.
4. Design Goals of C# Support for internationalization is very important. C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging from the very large that use sophisticated operating systems, down to the very small having dedicated functions. Although C# applications are intended to be economical with regard to memory and processing power requirements, the language was not intended to compete directly on performance and size with C or assembly language.
6. Distinguishing Features of C# It has no global variables or functions. All methods and members must be declared within classes. Static members of public classes can substitute for global variables and functions. Local variables cannot shadow variables of the enclosing block, unlike C and C++. Variable shadowing is often considered confusing by C++ texts. C# supports a strict Boolean data type. In C#, memory address pointers can only be used within blocks specifically marked as unsafe, and programs with unsafe code need appropriate permissions to run. Managed memory cannot be explicitly freed.
7. Distinguishing Features of C# Multiple inheritance is not supported, although a class can implement any number of interfaces. C# is more type safe than C++. C# currently (as of version 4.0) has 77 reserved words.
8.
9.
10.
11. Preprocessor C# features "preprocessor directives" (though it does not have an actual preprocessor) based on the C preprocessor that allow programmers to define symbols but not macros. Conditionals such as #if, #endif, and #else are also provided. Directives such as #region give hints to editors for code folding. public class Foo { #region Procedures public void IntBar(intfirstParam) {} public void StrBar(string firstParam) {} public void BoolBar(bool firstParam) {} #endregion #region Constructors public Foo() {} public Foo(intfirstParam) {} #endregion }
12. "Hello world" example using System; class Program { static void Main() { Console.WriteLine("Hello world!"); } }
25. Variables Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value. Declare intMyInt; // Declaring an uninitialized variable called 'MyInt', of type 'int' Initialize intMyInt; // Declaring an uninitialized variable MyInt = 35; // Initializing the variable Declare & initialize intMyInt = 35; // Declaring and initializing the variable at the same time
29. Jump statements The goto statement can be used in switch statements to jump from one case to another or to fall through from one case to the next. switch(n) { case 1: Console.WriteLine("Case 1"); break; case 2: Console.WriteLine("Case 2"); goto case 1; case 3: Console.WriteLine("Case 3"); case 4: // Compilation will fail here as cases cannot fall through in C#. Console.WriteLine("Case 4"); goto default; // This is the correct way to fall through to the next case. default: Console.WriteLine("Default"); }
30.
31. break statement The break statement breaks out of the closest loop or switch statement. Execution continues in the statement after the terminated statement, if any. int e = 10; for (int i=0; i < e; i++) { while (true) { break; } // Will break to this point. }
32. continue statement The continue statement discontinues the current iteration of the current control statement and begins the next iteration. intch; while ((ch = GetChar()) >= 0) { if (ch == ' ') continue; // Skips the rest of the while-loop // Rest of the while-loop ... }
33.
34. const - Specifies that a variable is a constant value that has to be initialized when it gets declared.
42. virtual - Specifies that a method or property declaration can be overridden by a derived class.
43.
44. F SHARP F# uses pattern matching to resolve names into values. It is also used when accessing discriminated unions. F# comes with a Microsoft Visual Studio language service that integrates it with the IDE. All functions in F# are instances of the function type, and are immutable as well. The F# extended type system is implemented as generic .NET types.
45. Examples A few small samples follow: (* This is a comment *) (* Sample hello world program *) printfn "Hello World!"