Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ТП лекція (змінні, типи,..

.).pdf
Скачиваний:
5
Добавлен:
17.05.2015
Размер:
3.4 Mб
Скачать

 

Implicit conversion

 

 

From

To

 

 

sbyte

short, int, long, float, double, decimal

 

 

byte

short, ushort, int, uint, long, ulong, float, double, decimal

 

 

short

int, long, float, double, decimal

 

 

ushort

int, uint, long, ulong, float, double, decimal

 

 

int

long, float, double, decimal

 

 

uint

long, ulong, float, double, decimal

 

 

long, ulong

float, double, decimal

 

 

float

double

 

 

char

ushort, int, uint, long, ulong, float, double, decimal

 

 

Interception narrowing conversions of data

Key words:

checked

unchecked

byte var1 = 150; byte var2 = 150; try {

byte sum = checked((byte)(var1+var2)); Console.WriteLine("Сумма: {0}", sum);

} catch (OverflowException ex){}

Read-Only Variables and Constants

Read-only variables

Declared with the readonly keyword

Initialized at run time

readonly string currentDateTime = DateTime.Now.ToString();

Constants

Declared with the const keyword

Initialized at compile time

const double PI = 3.14159; int radius = 5;

double area = PI * radius * radius; double circumference = 2 * PI * radius;

Lesson 2: Using Expressions and

Operators

What Is an Expression?

What Are Operators?

Specifying Operator Precedence

Best Practices for Performing String Concatenation

What Is an Expression?

Expressions are the fundamental constructs that you use to evaluate and manipulate data

a + 1

(a + b) / 2

"Answer: " + c.ToString()

b * System.Math.Tan(theta)

What Are Operators?

Operators are character sequences that you use to define operations that are to be performed on operands

Arithmetic +, -, *, /, %

Increment, decrement ++, --

Comparison ==, !=, <, >, <=, >=, is

String concatenation +

Logical/bitwise &, |, ^, !, ~, &&, ||

Indexing [ ]

Casting ( ), as

Assignment =, +=, -=, *=, /=

Bit shift <<, >>

Type information sizeof, typeof

Delegate concatenation +, -

Overflow checked, unchecked

Indirection, Address *, ->, [ ], &

Conditional (ternary operator) ?:

Specifying Operator Precedence

Highest

Lowest

++, -- (prefixes), +, - (unary), !

*, /, %

+, -

<<, >>

<, >, <=, >=

==, !=

&

^

|

&&

||

Assignment operators

++, -- (suffixes)

Strings

string address = "23";

char[] chararray = {'e', 'x', 'a', 'm', 'p', 'l', 'e'}; string str = new string(chararray);

Compare()

СоруТо()

Insert()

PadLeft()

PadRight()

 

 

 

 

 

 

 

CompareOrdinal()

Equals()

Join()

Remove()

Replace()

 

 

 

 

 

 

 

Concat()

Format()

LastlndexOf()

Split()

 

 

 

 

Contains()

IndexOf()

LastlndexOfAny()

Substring()

 

 

 

 

 

Trim()

ToUpper ()

 

IndexOfAny()

ToLower()

 

 

 

 

 

 

 

Best Practices for Performing String

Concatenation

Concatenating multiple strings in C# is simple to achieve by using the + operator

address = address + ", Oxford Street"; address = address + ", Thornbury";

This is considered bad practice because strings are immutable

A better approach is to use the StringBuilder class

StringBuilder address = new StringBuilder();

address.Append("23"); address.Append(", Oxford Street"); address.Append(", Thornbury");

string concatenatedAddress = address.ToString();

Lesson 3: Creating and Using

Arrays

What Is an Array?

Creating and Initializing Arrays

Common Properties and Methods Exposed by Arrays

Accessing Data in an Array