C Sharp

From Bauman National Library
This page was last modified on 8 June 2016, at 21:17.
C#
Paradigm multi-paradigm: structured, imperative, object-oriented, event-driven, task-driven, functional, generic, reflective, concurrent
Family C
Designed by Microsoft
Developer Microsoft
Stable release 6.0 / July 20, 2015; 7 years ago (2015-07-20)
Typing discipline static, dynamic, strong, safe, nominative, partially inferred
Platform Common Language Infrastructure
License CLR is proprietary, Mono compiler is dual GPLv3, MIT/X11 and libraries are LGPLv2, DotGNU is dual GPL and LGPLv2
Website {{#property:P856}}
Major implementations
Visual C#, .NET Framework, Mono, DotGNU
Dialects
, Spec#, Polyphonic C#
Influenced
Chapel,[1] D, Dart,[2] F#, Hack, Java,[3] Kotlin, Monkey, Nemerle, Oxygene, Rust, Swift,[4] Vala

C# - object-oriented programming language. It was disgned in 1998-2001 by a group of engineers under the direction of Anders Hejlsberg in Microsoft company as a language for developing applications for the Microsoft platform .NET Framework and it was later standartizes as the ECMA-334 and ISO/IEC 23270.

C# refers to a family of languages with C-like syntax. C# syntax is the closet to C++ and Java. This language has static typing, supports polymorphism, operators overloading (including operators of explicit and implicit type), delegates, attributes, events, properties, generalized types and methods, iterators, anonymous functions with support for closures, LINQ, exceptions, comments in XML style.

C# got a lot from its precursors - C++, Pascal, Module, Smalltalk and Java languages. C# eliminates some models which have proven to be problematic in the development of software systems. For instance C# unlike C++ does not support multiple inheritance classes (meanwhile allowed multiple inheritance of interfaces).

History

Name

The name "C sharp" was inspired by musical notation where a sharp indicates that the written note should be made a semitone higher in pitch. This is similar to the language name of C++, where "++" indicates that a variable should be incremented by 1. The sharp symbol also resembles a ligature of four "+" symbols (in a two-by-two grid), further implying that the language is an increment of C++.

Due to technical limitations of display (standard fonts, browsers, etc.) and the fact that the sharp symbol is not present on the standard keyboard, the number sign was chosen to represent the sharp symbol in the written name of the programming language. This convention is reflected in the ECMA-334 C# Language Specification.

Versions

C# project was staretes in december 1998. It got a code name COOL (C-style Object Oriented Language).

Version 1.0

Version 1.0 was announced with .NET platform in june 2000. In the same time the first public beta version was appeared. C# was finally released with Microsoft Visual Studio .NET in february 2002.

The first C# version was similar in its capabilities with Java 1.4. C# had properties (looking in the code as a field of the object, but in fact cause when accessed class methods), indexers (like properties, but takes a parameter as an array index), event, delegates, foreach cycle, structures passed by value, automatic type conversion embedded in objects when necessary (boxing), attributes, built-in interaction tools to deal with unmanaged code (DLL, COM) etc.

Besides was decided to transfer some features to C# from C++ which are absent in Java: unsigned types, operators overloading, passing parameters to a method by reference, methods with a variable number of parameters, goto operator. Also C# left a limited ability to work with pointers in places of code wich specially marked unsafe and which provides a special compiler options.

Version 2.0

The final 2.0 C# version was released in 2005. It strengthened this programming language possition, and finally convinced of its effectiveness and further development.

New C# 2.0 features:

Version 3.0

The final 3.0 version of this programming language was included in Visual Studio 2008 и .NET 3.5.

New C# 3.0 features:

Version 4.0

The final version was released in 2010.

New C# 4.0 features:

Language overview

Typing

C# supports strongly typed implicit variable declarations with the keyword var.

C# supports a strict Boolean data type, bool. Statements that take conditions, such as while and if, require an expression of a type that implements the true operator, such as the Boolean type.

C# is more type safe than C++. The only implicit conversions by default are those that are considered safe, such as widening of integers.

Any user-defined conversion must be explicitly marked as explicit or implicit.

The C# language does not allow for 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++.

Methods and functions

Like C++, and unlike Java, C# programmers must use the keyword virtual to allow methods to be overridden by subclasses.

Extension methods in C# allow programmers to use static methods as if they were methods from a class's method table, allowing programmers to add methods to an object that they feel should exist on that object and its derivatives.

C# has support for strongly-typed function pointers via the keyword delegate. Delegate is a type that represents references to methods with a particular parameter list and return type. When instance of the delegate is created that can be associated with any method with a compatible signature and return type.

Propety

C# provides properties as syntactic sugar for a common pattern in which a pair of methods, accessor (getter) and mutator (setter) encapsulate operations on a single attribute of a class. No redundant method signatures for the getter/setter implementations need be written, and the property may be accessed using attribute syntax rather than more verbose method calls.

Namespace

Namespaces are allow to organize any type of present programs in C#. It can be compared with a folder in a computer file system. Like folders, namespaces define the classes unique full names. C# program contains one or more namespaces, each of which is defined by a programmer, or defined as part of the previously written class library.

Exeptions

The C# language's exception handling features help you deal with any unexpected or exceptional situations that occur when a program is running. Exception handling uses the try, catch, and finally keywords to try actions that may not succeed, to handle failures when you decide that it is reasonable to do so, and to clean up resources afterward.

Polymorphism

Virtual methods allow to work with groups of related objects universal way.

If a derived class inherits from the base, it gets all the methods, fields, properties, and events of the base klassa. The developer who derives a class can choose the following:

  • override virtual members
  • inheritance the nearest base class method without redefinition
  • definition of a new, non-virtual realization of those members that hide the base class implementation

Value and reference type

Unlike some programming languages, C# has two varieties of data types: value type and reference type. If the application performance is essential and there is an interest how C# manages data memory, it is important to know the differences between these types. If in variable declaration we are using one of the built-in data types or user-defined data structure, then we are dealing with the type of values. String data type is an exception. String is a reference type. Value type stores its contents in the memory allocated on the stack.

Boxing and unboxing

Boxing is the operation of converting a value-type object into a value of a corresponding reference type. Boxing in C# is implicit.

Unboxing is the operation of converting a value of a reference type (previously boxed) into a value of a value type. Unboxing in C# requires an explicit type cast. A boxed object of type T can only be unboxed to a T (or a nullable T).

Examples

The console program. "Hello, World!" print:

using System;

namespace Example
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
    }
}

Shaker sort:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortLab
{
    class Program
    {

        static void Main()
        {
            Sort(); 
        }

        static void Sort()
        {
            
            int[] myint = { 99,88,77,66,55,44,33,22,11,8,5,3,1};

            WriteArray(myint);
            ShakerSort(myint);
            WriteArray(myint);

            Console.ReadLine();
        }

        static void ShakerSort(int[] myint)
        {
            int right = 0,
				left = myint.Length-1,
				count = 0;

			while (right <= left)
			{
				for (int i = right; i < left; i++)
				{
					count++;
					if (myint[i] > myint[i + 1]) 
						Swap(myint, i, i+1);
					else if (right == i)
						right++;
				}
				left--;

				for (int i = left; i > right; i--)
				{
					count++;
					if (myint[i-1] > myint[i]) 
						Swap(myint, i-1, i);
					else if (i == left)
						left--;
				}
				right++;
			}
        }
        static void Swap(int[] myint, int i, int j)
        {
            int glass;
            glass = myint[i];
            myint[i] = myint[j];
            myint[j] = glass;
        }
        static void WriteArray(int[] a)
        {
            foreach (int i in a)
                Console.Write("{0}|", i);
            Console.WriteLine("\n\n\n");
        }
    }
}

Further reading

External links

  1. C# Language Specification (from MSDN)
  2. C# Programming Guide (MSDN)
  3. Microsoft Visual Studio
    1. "Chapel spec (Acknowlegements)" (PDF). Cray Inc. 2015-10-01. Retrieved 2016-01-14. 
    2. "Web Languages and VMs: Fast Code is Always in Fashion. (V8, Dart) - Google I/O 2013". Google. Retrieved 22 December 2013. 
    3. Java 5.0 added several new language features (the enhanced for loop, autoboxing, varargs and annotations), after they were introduced in the similar (and competing) C# language [1] [2]
    4. Lattner, Chris (2014-06-03). "Chris Lattner's Homepage". Chris Lattner. Retrieved 2014-06-03. The Swift language is the product of tireless effort from a team of language experts, documentation gurus, compiler optimization ninjas, and an incredibly important internal dogfooding group who provided feedback to help refine and battle-test ideas. Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.