Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 39 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
39
Dung lượng
601 KB
Nội dung
Chapter Objectives Explain the need for C# Discuss flow control of a C# program Explain the fundamental data types in C# Discuss the concept of Boxing and UnBoxing Discuss Structures in C# Discuss Enumeration types Programming in C# / Compile and run a C# program Chapter 1/ of 39 Microsoft net Represents a new platform for developing windows & web applications Supports more than 20 languages Revolutionizes facilities available for Windows programming Programming in C# / Chapter 1/ of 39 Introduction to C# Takes full advantage of the net Platform The most efficient compiler in the net family A modern replacement for C++ Enhances developer productivity and increases safety, by enforcing script type checking Allows restricted use of native pointers Programming in C# / Chapter 1/ of 39 C# Program Flow A simple C# program: Programming in C# / Chapter 1/ of 39 C# Constructs (1) Variables in C# are declared as follows: Programming in C# / Chapter 1/ of 39 C# Constructs (2) If you want to use any keyword as the variable name, you have to prefix the variable name with @ Programming in C# / Chapter 1/ of 39 Default Values (1) Variables in C# are automatically assigned a default value upon creation Output: Programming in C# / Chapter 1/ of 39 Default Values (2) The default values of the common data types: Programming in C# / Chapter 1/ of 39 Input / Output In C# (1) Uses methods of the Console class in the System namespace The most widely used methods are: Programming in C# / Chapter 1/ 10 of 39 Fundamental Types Of C# (1) C# divides data types into two fundamental categories: - int, char and structures - classes, interfaces, arrays and strings Programming in C# / Chapter 1/ 25 of 39 Fundamental Types Of C# (2) Holds a value in the memory Stored in a stack Contains the address of the object in the heap = null means that no object has been in C# / Programming Chapter 1/ 26 of 39 referenced Value Types Example: Output: Programming in C# / Chapter 1/ 27 of 39 Reference Types Example: Output: Programming in C# / Chapter 1/ 28 of 39 Value types vs Reference types Programming in C# / Chapter 1/ 29 of 39 Boxing & Unboxing [1] Boxing is the conversion of a value type into a reference type Unboxing is the conversion of a reference type into a value type Programming in C# / Chapter 1/ 30 of 39 Boxing & Unboxing (2) Programming in C# / Chapter 1/ 31 of 39 Data Types In C# C# provides a Unified Type System All data types in C#, are derived from just one class, the object class Programming in C# / Chapter 1/ 32 of 39 Static Members Members are not associated with any particular object other than the class itself Only one instance is possible Programming in C# / Chapter 1/ 33 of 39 Arrays A group of values of similar data type It belongs to the reference type and hence, are stored on the heap The declaration of arrays in C#, follow the syntax given below: DataType[number of elements] ArrayName; Programming in C# / int[6] arr1;Chapter 1/ 34 of 39 Structures Custom data Types Can have methods Can have constructors Cannot implement inheritance Programming in C# / Chapter 1/ 35 of 39 Enumerators (1) They are a set of named constants Programming in C# / Chapter 1/ 36 of 39 Enumerators (2) Enumerators in C# have numbers associated with the values By default, the first element of enum is assigned a value of and is incremented for each subsequent enum element Programming in C# / Chapter 1/ 37 of 39 Enumerators (3) The default value can be overridden during initialization Programming in C# / Chapter 1/ 38 of 39 Compiling & Running Step – Type your code in Notepad Step – Save the file with a cs extension Step – Switch to DOS prompt and type the following command: csc csc First.cs Step - To run the C# file, Programming type the name in C# / of Chapter 1/ 39 of 39 the file (First) without the