Presentation is loading. Please wait.

Presentation is loading. Please wait.

C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++

Similar presentations


Presentation on theme: "C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++"— Presentation transcript:

1 C#.NET C# language

2 C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++

3 .NET Multiple languages which can interoperate Languages compile to a common intermediate language Common Language Runtime runs programs from all the.NET languages

4 C# versus Java Both were designed to be improvements on C and C++ –Java aims to be one language for many platforms portable (platform-independent) safe –C# aims to provide many languages for a single platform power unsafe code is allowed platform-independent not

5 Language Features Strong type checking Array bounds checking Detection of uninitialized variables Automatic garbage collection Designed for use in distributed environments Supports internationalization

6 First Program namespace FirstProgram { class First { static void Main() { System.Console.WriteLine( "Welcome to C#!"); }

7 C# on onyx mono is an open-source project that provides facilities for running C# programs under Linux http://www.mono-project.com Workstations have a Windows virtual machine with C# Express installed

8 mono mono is an open-source project that provides facilities for running C# programs under Linux http://www.mono-project.com Compile a program by typing mcs First.cs Run a program by typing mono First.exe

9 C# Express VMWare is installed on all the workstations in the lab –Not on the server –Virtual machine is frozen

10 Getting your own copy C# Express –Free from Microsoft –http://www.microsoft.com/express/vcsharp/http://www.microsoft.com/express/vcsharp/.NET Development Environment –Available to you through MSDN Alliance –details later

11 C# Program Structure A program consists of one or more files A file can contain one or more classes and/or namespaces –name of file is not tied to name of class At least one class must contain Main –There are several allowed signatures return type is either int or void either no parameter or String array

12 C# Types Value types –simple types: primitive types from Java plus unsigned types and decimal Reference types - like object references in Java Pointer types - used only in unsafe code Everything can be treated as an object

13 Operators Java operators with similar precedence Extra operators –typeof - returns class name (getClass()) –checked/unchecked (overflow) –is (like instance of) –as (returns cast value or null) Unsafe operators –*, &, sizeof

14 Operators: differences from Java C# allows you to overload operators == compares values for strings and simple types, addresses for all other objects

15 Console I/O System.Console.WriteLine( arg) –argument can be any type –for objects, ToString is invoked System.Console.ReadLine() –returns a String System is a namespace –using System; allows you to omit the namespace when calling the method

16 Namespaces Sort of like a package –in Java, package statement applies to everything in the file –can import all or part of a package A single C# file can contain classes from different namespaces –using imports entire namespace

17 C# Class Members Function Members –Instance constructor –Static constructor –Method –Property (accessor/mutator) –Indexer –Operator –Destructor (finalizer) –Nested types (inner classes) Data Members –Field instance static –Constant –Read-only –Delegate –Event

18 C# Class Modifiers new abstract sealed (final) access modifiers –public –protected –internal –private

19 Inheritance Single inheritance (System.Object) class : {… } Interfaces provide restricted form of multiple inheritance interface {… } –Syntax is same as for inheritance –Multiple interfaces separated by comma –Super class, if any, must be first in list

20 Methods Syntax same as for Java Default access is private (rather than package) Value types are passed by value usually –ref keyword used to pass by reference –out keyword provides for return values Reference types behave as in Java

21 Properties C# properties provide the same service as Java's accessors and mutators public string Color { get {return MyColor;} set { MyColor = value; } } Use the name Color to get value of or assign value to private instance variable MyColor

22 Control Statements do, while, for, if-else same as in Java foreach provides loop for arrays switch-case like Java except –string case values are allowed –no fall-through behavior use goto case instead C# has goto statement

23 Arrays Arrays are objects –Subclasses of System.Array –Reverse, Sort, IndexOf methods Declaration, instantiation, initialization, element access as in Java for 1D arrays int []MyArray = new int[5]; –Length property gives the number of elements –Only this syntax is allowed

24 Arrays For rectangular 2D arrays int [,] Array2D = new int[2,5]; –Length property gives total number of elements –Element access is the same as in Java For jagged arrays int [][] JaggedArray2D = new int[2][]; JaggedArray[0] = new int[3];

25 Structs A struct is very similar to a class except –structs are value types, classes are reference types –no inheritance –struct has a default constructor always –all constructors must initialize all data –can be "instantiated” without new

26 Exceptions System.Exception try-catch-finally has same syntax as Java –Don't need a variable name in catch if you aren't going to use it –Within catch block, throw throws the exception that was caught All exceptions are unchecked

27 Delegates Delegate is a special type that encapsulates one or more methods public delegate int MyDelegate( string s, int i); Instantiate with a method that has the same signature and return type MyDelegate d2 =new MyDelegate(obj.ObjectMethod);

28 Events C# event model is similar to Java - delegation based An Event is a special type of Delegate Event is created by event source when an event occurs. The Event is passed to the event consumer's handler Handlers must be registered with the event source

29 etc. Operator overloading Reflection - asking a class for information about it Attributes –Used to add annotation to a class that can be read using reflection Preprocessor Unsafe code


Download ppt "C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++"

Similar presentations


Ads by Google