# Высокоуровневые методы информатики и программирования Лекция 3

• View
45

0

Embed Size (px)

DESCRIPTION

3. . . . . - ( ): , , . - PowerPoint PPT Presentation

Transcript

Variables&Operators

3

1 ( )

2 . - ( ): , , . (, ) (, ). + (). . : , . () . C#1. .2. .3. .4. .5. .6. .int n;float val;float r;(n + val) * 2r = (n + val) * 2;float calc(int m){ int n; f loat val; float r; r = (n + val) * 2; return r;}class Circle{ int r; float calc(int m) { }}1. 5 , : ( , ), ( , , ""). C# : ( ) ( ). ( ) ( public) () new ( public ) ( )

7 , , . . : () (_). (unicode) . ( ) , , . : ; [= ][] [= ]; [] = {, static, const}.

:

public int x = 5;public static const int n=10;

8 C# ( ) . :y = 7.7; "7.7" , . double. , ( ). : f float; d double; m decimal. . const, . :const float = 0.1f;

. : .. C# : , , "ssss"; @-, c @. . , , escape-. : "\n" - ;"\t" - ( );"\\" - ;"\"" - , , . , , : C:\\test.txt, . @-, . , :s1 = "c:\\c#book\\ch5\\chapter5.doc";s2 = @"c:\c#book\ch5\chapter5.doc";

() . () . . () (}). .

(variable scope) , . , , , , , . , (, for while), () . :public void ScopeTest() { int n = 0; for (int i = 0; i < 10; i++) { Console.WriteLine(i); } // i // i { // var i = ; // Console.WriteLine(i); } // i } // n

C# . , ( ) .

: ( ); , ; . (, , , , , ..) . . . , : ( ) ( , ) C# C# : , . ( ) , CLR, . (Common Type Standard CTS) .Net 15 (. . 3.1)., . , C#, () . : (class) (struct) (enum) (interface) (delegate) , : ( ); ; ( ) . , , , , .

2 new16 (, ) : ( ) ( ) (stack) (), (last-in, first-out LIFO). , . . . , , , . . , .

Last InFirst In (heap) (heap) , , . new. , "" . "", . CLR, Garbage Collector (GC), "", , ( ). , .

int v = 123;string s = Hello World!;

v

s 123254 688Hello World!254 68820 - (Stack) (heap, free memory) (Stack Top Pointer){ int n = 37; Point p; p = new Point(); ...}3 725642 5 6 4 21 CLR .Net Framework , .

Common Type System (CTS) .

Microsoft.:System.Int32System.SingleSystem.String.

22 C#intfloatstring Visual BasicIntegerSingleString. . . . . . System.Int32System.SingleSystem.String. . . CLRObjectValueTypeBooleanByteCharDecimalDoubleInt16Int32SingleInt64UInt16UInt32UInt64ClassStringArrayInterfaceDelegateReference TypesValue TypesEnum StructureOthersOthers CLRObjectStringArrayValueTypeExceptionDelegateClass1Class2Class3MulticastDelegateStructure1EnumEnum1 BooleanCharByteInt16Int32Int64SingleDoubleDecimalDateTime- , - , 25 System.ObjectEquals() - , true ( , ). GetHashCode() - , (-), . GetType() - Type, . ToString() - , ( , ).

26- Object. , , . , , , Object, , ; , , . C# :int x=11;int v = new Int32();v = 007;string s1 = "Agent";s1 = s1 + v.ToString() + x.ToString();

CLR - boolSystem.Booleantrue, false8 sbyteSystem.SByte-128 +127, 8 byteSystem.Byte0 255, 8 shortSystem.Short-32768 +32767, 16 ushortSystem.UShort0 65535, 16 intSystem.Int32-2,147,483,648 +2,147,483,647

, 32 uintSystem.UInt32(0 4*109), 32 longSystem.Int64(-9*1018 9*1018), 64 ulongSystem.UInt64(0 18*1018), 64 () CLR - - floatSystem.Single(1.5*10-45 3.4*10+38 )32 ( 7 )doubleSystem.Double(5.0*10-324 1.7*10+308 )64 ( 1516 ) decimalSystem.Decimal (1.0*10-28 7.9*10+28)2829 charSystem.CharU+0000 U+ffff 16 Unicode stringSystem.String Unicode bjectSystem.Object void - var bool System.BooleanM true false ( ) , :bool bc = (c > 64 && c < 123); 30 decimal128- ; (floating-point); 1.01028 7.91028 28 ; decimal m M: decimal myMoney = 300.5m; 31 - var (var) . . , :var name = " .."; :string name = " .."; . . , Point:var points = new[] { new Point(1, 2), new Point(3, 4) };

() ( ). double. :Float F: 0.23FDouble D: 2.7DDecimal M: 12.34M

33Nullable Nullable , null. , nullable System.Boolean {true, false, null}. , . nullable (?) . . :static void LocalNullableVariables(){ // nullable . int? nullableInt = 10; double? nullableDouble = 3.14; bool? nullableBool = null; char? nullableChar = 'a'; int?[] arrayOfNullableInts = new int?[10]; // Error! String are reference types! // string? s = "oops";}Nullable System.Nullable. nullable . ??: nullable = null, , ??. Nullable bool HasValue . Value ( , System.InvalidOperationException ) 1:int? n = null;int m = 5 + n.Value; //

2:int? n = null;// int m = 5 + (int)n; //

3:int? n = null;int? m = 5 + n; // m = null nullable static void Main(string[] args){ Console.WriteLine("***** Nullable Data *****\n"); DatabaseReader dr = new DatabaseReader(); // int . int? i = dr.GetIntFromDatabase(); if (i.HasValue) // Console.WriteLine("Value of 'i' is: {0}", i.Value); else Console.WriteLine("Value of 'i' is undefined."); // bool . bool? b = dr.GetBoolFromDatabase(); if (b != null) Console.WriteLine(" 'b' : {0}", b.Value); else Console.WriteLine(" 'b' ."); Console.ReadLine();}2. () . , ( ) ( , ), : a + ++a * pi, , (++) new, . , , , (+, , *, /) . (?:), C#.

x.y f(x) a[x] x++ x-- new T(...) new T(...) {...} new T[...] (. 3.5).typeof(T) System.Type Tdelegate {} ( ) x !x ~x ++x --x (T)x x T () *x + y, /x y % x > y x != y () x < yx > y x = y x is T true, x T, falsex as T x T , x T =x op= y : +=, -=, *=, /=, %=, &=, |=, !=, = , Null- ANDx & y AND, AND XORx ^ y XOR, XOR ORx | y OR, OR ANDx && y y x true ORx || y y x false x ?? y y, x = null, xx ? y:z y, x true, z x false C#0(expr) x.y f(x) a[x] x++ x-- new sizeof(t)typeof(t) checked(expr) unchecked(expr) 1+ - ! ~ ++x --x (T)x 2 ()* / % 3 ()+ - 4> 5, < > = is as 6== != 7 & 8 (XOR)^ 9 (OR)| 10 && 11 || 12 ? : 13 = *= /= %= += -= = &= ^= |= . : (expr), x.y, f(x), a[i]. . , 2 + 3 * 2 8, . (2 + 3) * 2 10, C# , (+) (*). , . . , x * y / z (x*y)/z. . (?:) . . . .

. . . byte, decimal.int a=5;double d=2.6;a *d // doublea / 2 // int (, ).int n;n = a*d // int bool.a > 5 // bool bool.bool b = true, = false;b && // bool

(implicit conversion) , . . A B , Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?) (explicit conversion) , (cast)int i = (int) f; // Convertint i = Convert.ToInt32(f); //

47Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

ulonglonguintintushortshortbytesbytefloatdoubledecimalchar48 ()longintshortfloatdoubledecimalcharbyte49 () , , . short float , . float int50 bool c1 = true;int d = c1; // Error!Cannot implicitly convert type 'bool' to 'intd = (int) c1; //Error! Cannot convert type 'bool' to 'int

int a = 5;float f = 1.9; // Error! Literal of type double cannot be implicitly //converted to type 'float'; use an 'F' //suffix to create a literal // of this type

float b = 1.9;a = (int)b; // a = 1 b = a; // b = 1.0

decimal d = 2;d = (decimal)b;d = a;51 Javachar c='X';int code=c;System.out.println(code);

: 88 (ASCII code of X)

// Error: no conversion from int to shortint x=5, y=6;short z = x + y;

int a = 5;float b = 1.5F;b = a;

// ()a = (int)b;

53 (), :(type) , , , . : int i = (int) 2.99; // i = 2; T P, T P, : T y; P x = new P(); y = (T) x; , , , , bool. :double a = 5.0;int p = (int)a;//bool b = (bool)a; // !!! double int , double bool , .

Convert , , System.Convert, ( ). Convert 15 To (ToBoolean(),...ToUInt64()); string s1 = Console.ReadLine(); int ux = Convert.ToUInt32(s1); To Convert , , . float b = 1.5;a = Convert.ToInt32(b); // a=2 bool b = true;a = Convert.ToInt32(b); // a=1

55 DateTime, System.DateTime dat = Convert.ToDateTime("15.03.2003");

System.Single f = 0.5F; float b = f; int a; a = (int)f; // a = Convert.ToInt32(f); // string s = "123"; // a = (int)s; a = Convert.ToInt32(s);56 C# , . , , . : x = y = z = w =(u+v+w)/(u-v-w); . .. , . ( T) ( T1) : T T1 ; T () T1 ( ); T1 T. C# Object, , Object .

C# . "x=x+1", , "++" "--". : X = X (expression); : x = x * 2; : X = expression