Download presentation
Presentation is loading. Please wait.
Published byVirginia Stevenson Modified over 9 years ago
1
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications which can be built using C# include traditional Windows client applications, XML web services, distributed components, client-server applications, database applications, and much more. - Visual C# 2010 provides an advanced code editor, convenient user interface designers, integrated debugger based on version 4.0 of C# language, and version 4.0 of.NET Framework.
2
Introduction to C# - II C# syntax is highly expressive, yet easy and simple to learn. Familiarity with Java, C, C++ → familiarity with C# C# syntax simplifies many of the complexities of C++ C# provides powerful features such as nullable value types, enumerations, delegates, lambda expressions and direct memory access, which are not found in Java
3
Introduction to C# - III C# supports generic methods and types, which provide increased type safety and performance Language-Integrated Query (LINQ) expressions make the strongly-typed query a first-class language construct C# supports encapsulation, inheritance, and polymorphism C# class may inherit from only one class, however implement more than one interface.
4
Introduction to C# - IV Struct in C# - stack-allocated type, can implement interfaces, not support inheritance Innovative software constructs in C# include: Delegates – encapsulated method signatures Properties – accessors for private member variables Attributes - provide declarative metadata about types at run time Inline XML documentation comments Language-Integrated Query (LINQ)
5
Introduction to C# - V Interop – allows C# programs to use native C++ code, including pointers, and “unsafe” code C# got simple build process compared to C and C++ and more flexible than in Java. no separate header files. no requirement that methods and types be declared in a particular order. A C# source file may define any number of classes, structs, interfaces, and events.
6
Structure of a C# program // A Hello World! program in C#. using System; namespace HelloWorld { class Hello { static void Main() { Console.WriteLine("Hello World!"); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
7
Structure of a C# program - II Comments: //single line comments /*a block of comments*/ Main method: Where control starts and ends Can take arguments (string []) and can return void or int. Compiling & Executing: “csc Hello.cs”->Hello.exe, then “Hello.exe” on command prompt
8
Structure of a C# program - III // A skeleton of a C# program using System; namespace YourNamespace { class YourClass { } struct YourStruct { } interface IYourInterface { } delegate int YourDelegate(); enum YourEnum { }
9
Structure of a C# program - IV namespace YourNestedNamespace { struct YourStruct { } } class YourMainClass { static void Main(string[] args) { //Your program starts here... }
10
Structure of a C# program - V On start of application, Main method the first method to be invoked. Can only be one entry point in a C# program. Main must be static, and it should not be public. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument.
11
Structure of a C# program - VI class CommandLine2 { static void Main(string[] args) { System.Console.WriteLine("Number of command line parameters = {0}", args.Length); foreach (string s in args) { System.Console.WriteLine(s); }
12
Structure of a C# program - VII /* Output: Number of command line parameters = 3 Muhammad Eesa Musa */
13
Structure of a C# program - VIII To compile the code from a command prompt, use the Visual Studio Command Prompt, available from the Start menu under Visual Studio Tools.
14
Types C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method signature specifies a type for each input parameter and for the return value. The.NET Framework class library defines a set of built-in numeric types as well as more complex types that represent a wide variety of logical constructs, such as the file system, network connections, collections and arrays of objects, and dates.
15
Types – II information stored in a type includes The storage space that a variable of the type requires. The maximum and minimum values that it can represent The base type it inherits from. The location where the memory for variables will be allocated at run time. The kinds of operations that are permitted.
16
Types - III Compiler uses information stored in the type to ensure type safety. The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory. Type of a variable or constant must be declared, or use var keyword to let the compiler infer its type.
17
Types - IV After a variable is declared, it cannot be re- declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. Types can be converted to other types. A conversion in which there is no data loss is done automatically by the compiler. A conversion in which there is data loss requires explicit cast.
18
Types - V C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in string and object types. struct, class, interface, and enum constructs can be used to create custom types.
19
Types - VI Common Type System supports the principle of inheritance Types can derive from other types, called base types. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. All types derive ultimately from a single base type (System.Object (C# keyword: object)). This unified type hierarchy is called the Common Type System (CTS).
20
Types - VII Each type in the CTS is defined as either a value type or a reference type. Types that you define by using the struct keyword are value types; all the built-in numeric types are structs. Types that you define by using the class keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior.
21
Types - VIII Value Types: Value types derive from System.ValueType, which derives from System.Object. Types that derive from System.ValueType have special behavior in the CLR. Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There is no separate heap allocation or garbage collection overhead for value-type variables.
22
Types - IX Categories of Value types: Struct Enum The built-in numeric types are structs. Value types are sealed, which means, for example, that you cannot derive a type from System.Int32, and you cannot define a struct to inherit from any user-defined class or struct because a struct can only inherit from System.ValueType. However, a struct can implement one or more interfaces.
23
Types - X You can cast a struct type to an interface type; this causes a boxing operation to wrap the struct inside a reference type object on the managed heap. Boxing operations occur when you pass a value type to a method that takes a System.Object as an input parameter. All enums inherit from System.Enum, which inherits from System.ValueType. All the rules that apply to structs also apply to enums. Reference Types: A type that is defined as a class, delegate, array, or interface is a reference type.
24
Types - XI When the object is created, the memory is allocated on the managed heap, and the variable holds only a reference to the location of the object. Reference types fully support inheritance. Generic Types: A type can be declared with one or more type parameters that serve as a placeholder for the actual type (the concrete type) that client code will provide when it creates an instance of the type. Such types are called generic types.
25
Types - XII e.g. the.NET Framework type System.Collections.Generic.List(Of T) has one type parameter. List strings = new List (); The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to object. Generic collection classes are called strongly-typed collections because the compiler knows the specific type of the collection's elements. Implicit Types
26
Types - XIII you can implicitly type a local variable (but not class members) by using the var keyword. The variable still receives a type at compile time, but the type is provided by the compiler. Anonymous types: In some cases, it is inconvenient to create a named type for simple sets of related values that you do not intend to store or pass outside method boundaries. You can create anonymous types for this purpose. var v = new { Amount = 108, Message = "Hello" };
27
Types - XIV Nullable types: Ordinary value types cannot have a value of null. However, you can create nullable value types by affixing a ? after the type. In the CTS, nullable types are instances of the generic struct type System.Nullable(Of T).
28
References Visual Studio 2010 help
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.