Chương 2: Core C# Programming Construct pot

65 561 0
Chương 2: Core C# Programming Construct pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter Core C# Programming Constructs Hoang Anh Viet VietHA@it-hut.edu.vn HaNoi University of Technology Objectives “This chapter surveys the C# language syntax I introduce you to the two fundamental kinds of types within the CLR: value types and reference types This chapter also describes namespaces and how you can use them to logically partition types and functionality within your applications.” Microsoft Roadmap 2.1 C# Is a strongly Typed Language 2.2 Expression 2.3 Statements and Expressions 2.4 Types and Variabless 2.5 NameSpaces 2.6 Control Flows Microsoft 2.1 C# Is a strongly Typed Language  Every variable and object instance in the system is of a well-defined type  This enables the compiler to check that the operations to perform on variables and object instance are valid  It is always best to find bugs at compile time rather than run time  Example: • Method ComputeAvg(): computes the average of two integers and returns the result Microsoft double ComputeAvg( int param1, int param2 ) { return (param1 + param2) / 2.0; } ComputeAvg accepts two integers and returns a double If passing an instance of Apple type, the compiler will complain and stop object ComputeAvg( object param1, object param2 ) { return ((int) param1 + (int) param2) / 2.0; } Convert objects into integers Passing an instance of Apple type causes an exception( the instance can’t be convert into integer Microsoft object keyword: an alias of System.Object class Object is not a numeric type Roadmap  2.1 C# Is a strongly Typed Language  2.2 Expression  2.3 Statements and Expressions  2.4 Types and Variabless  2.5 NameSpaces  2.6 Control Flows Microsoft 2.2 Expressions     Expressions in C# are identical to expressions in C++ and Java Are built using operands, eg variables or types within an application, and operators Operators can be overloaded Operators can have different meaning in different contexts • Eg: the + operator can mean string concatenation using with  string operands C# operator precedence: • When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated • Entries at the top of the table have higher precedence • Operators within the same category have equal precedence Microsoft Category Primary Microsoft Expression x.m x( ) x[ ] x++ x-new T( ) new T( ){ } new { } new T[ ] typeof(T) checked(x) unchecked(x) default(T) delegate { } Description Member access Method and delegate invocation Array and indexer access Post-increment Post-decrement Object and delegate creation Object creation with initializer Anonymous object initializer Array creation Obtain System.Type object for T Evaluate expression in checked context Evaluate expression in unchecked context Obtain default value of type T Anonymous function (anonymous method) Category Unary Multiplicative Additive Shift Microsoft Expression +x -x !x ~x ++x x (T)x x * y x / y x % y x + y x – y x > y Description Identity Negation Logical negation Bitwise negation Pre-increment Pre-decrement Explicitly convert x to type T Multiplication Division Remainder Addition, string concatenation, delegate combination Subtraction, delegate removal Shift left Shift right Category Relational and type testing Equality Logical AND Logical XOR Logical OR Microsoft Expression x < y x > y x = y x is T x as T x == y x != y x & y x ^ y x | y Description Less than Greater than Less than or equal Greater than or equal Return true if x is a T, false otherwise Return x typed as T, or null if x is not a T Equal Not equal Integer bitwise AND, boolean logical AND Integer bitwise XOR, boolean logical XOR Integer bitwise OR, boolean logical OR 10 Standard Namespaces in NET Fundamentals System System.IO System.AddIn System.Linq System.Collections System.Reflection System.ComponentModel System.Resources System.Configuration System.Runtime System.Diagnostics System.Security System.DirectoryServices System.ServiceProcess System.EnterpriseServices System.Text System.Globalization System.Threading System.IdentityModel.Claims System.Transactions Microsoft 51 Standard Namespaces in NET(2)  Windows Presentation Foundation   System.Windows Windows Forms  System.Drawing  System.Media  System.Windows.Forms  ASP.NET  Microsoft System.Web 52 Standard Namespaces in NET(3)  Communications and Workflow        System.Messaging System.Net System.Net.Sockets System.ServiceModel System.Web.Services System.Workflow DATA, XML and LINQ  System.Data  System.Xml Microsoft 53 Roadmap  2.1 C# Is a strongly Typed Language  2.2 Expression  2.3 Statements and Expressions  2.4 Types and Variabless  2.5 NameSpaces  2.6 Control Flows Microsoft 54 2.6 Control Flow  if-else, switch  while, do-while, and for  foreach  break, continue, goto, return, and throw Microsoft 55 if-else construct  if1 (condition) (salary if > 2000) statement(s)_1 //The original result Console.Write("Salary is greater than 2k"); [else statement(s)_2] //The    alternative result if (salary > 2000) condition is a relational or logical expression Console.Write("Salary is greater than 2k"); // The original result statement(s)_1 is a statement (or a block of else statements) that is executed if the condition is Console.Write("Salary is less than or equal true to 2k"); // The alternative result statement(s)_2 is a statement (or a block of statements) that is executed if the condition is false Microsoft 56 Nested if-else Statements if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else statement_n; Microsoft 57 switch Construct expression represents a value that corresponds to the associated switch choice using System; switch (expression) class Switch statement(s) is a { { statement or 4.case constant-1: static void main() statement(s); block of statements that is 5.jump-statement { executed if the int n = 2; case constant-2: statement(s); corresponding switch (n) condition is 8.jump-statement { evaluated true 9.case constant-3: case 1: Console.WriteLine("n=1"); 10 break; jump-statement is 11 default: Console.WriteLine("n=2"); a branching [default: statement(s); 12 break; statement to jump-statement] 13 } transfer control default}deals 14 } outside the specific with all the 15 } case, such as other cases break or goto (explained later) 58 Microsoft while Loop using System; while (control_expression) statement(s); class WhileLoop  control_expression is a condition be satisfied during the loop { execution static void Main()  statement(s) is a statement (or the block of statements) to be executed { int counter=0; while(counter++

Ngày đăng: 02/08/2014, 09:20

Mục lục

  • Chapter 2. Core C# Programming Constructs

  • 2.1. C# Is a strongly Typed Language

  • Value Types and Reference Types

  • Value Types contain Reference Types

  • Implicitly Typed Local Variables

  • Implicitly Typed Local Variables(2)

  • Implicitly Typed Local Variables(3)

  • as and is Operators

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan