C# is a multi-paradigm programming language developed by Microsoft for its .NET Framework. It supports imperative, functional, generic, object-oriented and component-oriented programming. C# is intended to be a simple, modern, general-purpose language and its most recent version is 4.0. It was influenced by languages like Java, C++ and Object Pascal.
C# is a multi-paradigm programming language developed by Microsoft for its .NET Framework. It supports imperative, functional, generic, object-oriented and component-oriented programming. C# is intended to be a simple, modern, general-purpose language and its most recent version is 4.0. It was influenced by languages like Java, C++ and Object Pascal.
C# is a multi-paradigm programming language developed by Microsoft for its .NET Framework. It supports imperative, functional, generic, object-oriented and component-oriented programming. C# is intended to be a simple, modern, general-purpose language and its most recent version is 4.0. It was influenced by languages like Java, C++ and Object Pascal.
C# is a multi-paradigm programming language developed by Microsoft for its .NET Framework. It supports imperative, functional, generic, object-oriented and component-oriented programming. C# is intended to be a simple, modern, general-purpose language and its most recent version is 4.0. It was influenced by languages like Java, C++ and Object Pascal.
Download as PPT, PDF, TXT or read online from Scribd
Download as ppt, pdf, or txt
You are on page 1/ 23
C# Introduction
By Prof. Vasanti Dutta
C# • C# (pronounced "see sharp") is a multi-paradigm programming language encompassing imperative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within the .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# is intended to be a simple, modern, general-purpose, object-oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 4.0, which was released in April 12, 2010. C# (programming language) • C# Usual file extensions : . Cs • Paradigm multi-paradigm: structured, imperative, object-oriented, event-driven, functional • Appeared in 2001 • Designed & Developer by Microsoft Stable release 4.0 (April 12, 2010) • Typing discipline static, dynamic,strong, safe, nominative • Major implementations.NET Framework, Mono, DotGNU • Influenced by Java, C++, Eiffel, Modula-3, Object Pascal • Influenced D, F#, Java 5, Nemerle, Vala
• C# (pronounced "see sharp") is a multi-paradigm programming
language encompassing imperative, functional, generic, object- oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within the .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# is intended to be a simple, modern, general-purpose, object- oriented programming language. Its development team is led by Anders Hejlsberg. The most recent version is C# 4.0, which was released in April 12, 2010. Features of C# 1/4 • There are 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 datatype, 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. While C++ also has a boolean type, it can be freely converted to and from integers, and expressions such as if(a) require only that a is convertible to bool, allowing a to be an int, or a pointer. C# disallows this "integer meaning true or false" approach on the grounds that forcing programmers to use expressions that return exactly bool can prevent certain types of common programming mistakes in C or C++ such as if (a = b) (use of assignment = instead of equality ==). Features of C# 2/4 • 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. Most object access is done through safe object references, which always either point to a "live" object or have the well-defined null value; it is impossible to obtain a reference to a "dead" object (one which has been garbage collected), or to a random block of memory. An unsafe pointer can point to an instance of a value-type, array, string, or a block of memory allocated on a stack. Code that is not marked as unsafe can still store and manipulate pointers through the System. IntPtr type, but it cannot dereference them. • Managed memory cannot be explicitly freed; instead, it is automatically garbage collected. Garbage collection addresses the problem of memory leaks by freeing the programmer of responsibility for releasing memory which is no longer needed. • In addition to the try...catch construct to handle exceptions, C# has a try...finally construct to guarantee execution of the code in the finally block. Features of C# 3/4 • Multiple inheritance is not supported, although a class can implement any number of interfaces. This was a design decision by the language's lead architect to avoid complication and simplify architectural requirements throughout CLI. • C# is more type safe than C++. The only implicit conversions by default are those which are considered safe, such as widening of integers. This is enforced at compile-time, during JIT, and, in some cases, at runtime. There are no implicit conversions between booleans and integers, nor between enumeration members and integers (except for literal 0, which can be implicitly converted to any enumerated type). Any user-defined conversion must be explicitly marked as explicit or implicit, unlike C++ copy constructors and conversion operators, which are both implicit by default. Features of C# 4/4 • Enumeration members are placed in their own scope. • 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. • Full type reflection and discovery is available. • C# currently (as of 3 June 2008) has 77 reserved words. • Checked exceptions are not present in C# (in contrast to Java). This has been a conscious decision based on the issues of scalability and version ability C# Basics • Basic syntax • Describes the basics in how the applications you write will be interpreted. • Variables • The entities used to store data of various shapes. • Operators • Summarizes the operators, such as the '+' in addition, available in C#. • Data structures • Enumerations, structs, and more. • Control statements • Loops, conditions, and more. How the program flow is controlled. Data Types: Value types • Value types : Value types are plain aggregations of data. Instances of value types do not have referential identity nor a referential comparison semantics - equality and inequality comparisons for value types compare the actual data values within the instances, unless the corresponding operators are overloaded. Value types are derived from System.ValueType, always have a default value, and can always be created and copied. Some other limitations on value types are that they cannot derive from each other (but can implement interfaces) and cannot have an explicit default (parameterless) constructor. Examples of value types are some primitive types, such as int (a signed 32-bit integer), float (a 32-bit IEEE floating-point number), char (a 16-bit Unicode code unit), and System.DateTime (identifies a specific point in time with nanosecond precision). Other examples are enum (enumerations) and struct(user defined structures). Data Type: Reference types • Reference types have the notion of referential identity - each instance of a reference type is inherently distinct from every other instance, even if the data within both instances is the same. This is reflected in default equality and inequality comparisons for reference types, which test for referential rather than structural equality, unless the corresponding operators are overloaded (such as the case for System.String). In general, it is not always possible to create an instance of a reference type, nor to copy an existing instance, or perform a value comparison on two existing instances, though specific reference types can provide such services by exposing a public constructor or implementing a corresponding interface (such as ICloneable or IComparable). Examples of reference types are object (the ultimate base class for all other C# classes), System.String (a string of Unicode characters), and System.Array (a base class for all C# arrays). • Both type categories are extensible with user-defined types. Boxing and Unboxing • Boxing is the operation of converting a value of a value type 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. Example int num = 42; // Value type. object obj1 = num ; // num is boxed to obj1 . int num2 = (int) obj1 ; // Unboxed back to value type. "Hello, world" example using System; class ExampleClass { static void Main() { Console.WriteLine("Hello, world!"); } } C# Syntax • Identifier : An identifier is the name of an element in the code. There are certain standard naming conventions to follow when selecting names for elements. • An identifier can: – Start with ‘_’. – contain both upper case and lower case Unicode letters. Case is significant • An identifier cannot: – start with a numeral. – start with a symbol, unless it is a keyword (check Keywords). – have more than 511 chars. Literals Integers • octal 0365, 0[0..7]* • hexadecimal 0xF5, 0x[0..9, A..F, a..f]* • decimal 245, [1..9][0..9]* Floating-point values • float 23.5F, 23.5f; 1.72E3F, 1.72E3f, 1.72e3F, 1.72e3f • double 23.5, 23.5D, 23.5d; 1.72E3, 1.72E3D, ... Character literals • char 'a', 'Z', '\u0231' • String literals • String "Hello, world“ "C:\\Windows\\", @"C:\Windows\" Character escape sequence Escape Sequence Meaning \' Single Quote \" Double Quote \\ Backslash \0 Null, not the same as the C# null value \a Bell \b Backspace \f form Feed \n Newline \r Carriage Return \t Horizontal Tab \v Vertical Tab Category (by Operator(s) Associativity precedence) x.y f(x) a[x] x++ x-- new Primary typeof default checked unchecked left delegate Unary + - ! ~ ++x --x (T)x left Multiplicative * / % left Additive + - left Shift << >> left Relational < > <= >= is as left Equality == != right Logical AND & left Logical XOR ^ left Logical OR | left Conditional AND && left Conditional OR || left Null Coalescing ?? left Ternary ?: right = *= /= %= += -= <<= Assignment right >>= &= ^= |= => Size and Range: Integral type Size (in Type Range bits) sbyte 8 -128 to 127 byte 8 0 to 255 short 16 -32768 to 32767 ushort 16 0 to 65535 int 32 -2147483648 to 2147483647 uint 32 0 to 4294967295 -9223372036854775808 to long 64 9223372036854775807 ulong 64 0 to 18446744073709551615 char 16 0 to 65535 Advanced Object-Orientation Concepts • Inheritance • Re-using existing code to improve or specialise the functionality of an object. • Interfaces • Define a template, in which to base sub-classes from. • Delegates and Events • Be informed about when an event happens and choose what method to call when it happens • with delegates. • Abstract classes • Build partially implemented classes. Advanced Object-Orientation Concepts • Partial classes • Split a class over several files to allow multiple users to develop, but also to stop code • generators interfering with source code. • Collections • Effectively manage (add, remove, find, iterate, etc.) large sets of data. Advanced Object-Orientation Concepts • Generics • Allow commonly used collections and classes to appear to have specialisation for your • custom class. • Object Lifetime • Learn about the lifetime of objects, where they are allocated and learn about garbage collection. • Design Patterns • Learn commonly used design methodologies to simplify and/or improve your development • framework.