2. c language review
TRANSCRIPT
-
8/2/2019 2. C Language Review
1/37
C# language
-
8/2/2019 2. C Language Review
2/37
Introduction C# is the programming language for
.Net developer
C# is much safer and simpler It has a visual interface and powerful
editor that provide tons of help
C# is a powerful, professionallanguage, but learning it doesnt haveto be boring
-
8/2/2019 2. C Language Review
3/37
Basic C# concepts
-
8/2/2019 2. C Language Review
4/37
Identifiers
used to denote variables, constants,types, methods, objects...
The same to java, c/c++
Note: @+keyword
string @if = "@if";
-
8/2/2019 2. C Language Review
5/37
Table 2.1: C# Keywords (Reserved Identifiers)
abstract do in protected true
as double int public try
base else interface readonly typeof
bool enum internal ref uint
break event is return ulong
byte explicit lock sbyte unchecke
dcase extern long sealed unsafecatch false namespace short ushort
char finally new sizeof using
checked fixed null stackalloc virtual
class float object static void
const for operator string volatile
continue foreach out struct while
decimal goto override switch
default if params this
delegate implicit private throw
-
8/2/2019 2. C Language Review
6/37
VariablesAvariablecombines a type with a way to
store a value of the specified type
Example
int theInt;
string deeFlat = "This is a string!";
int i = new int();//int inherit from objectNote: variables be assigned a value before they are used
-
8/2/2019 2. C Language Review
7/37
Constants
Constantsare declared in C# using theconst keyword followed by the
constant's typeconst float PI = 3.141592;
const string className = QL;
-
8/2/2019 2. C Language Review
8/37
Enumeration Constants
enum toys {train, dinosaur, truck};
Enumerator Value
toys.train 0
toys.dinosaur 1toys.truck 2
enum toys {train = 12, dinosaur = 35, truck = 42};
Enumerator Valuetoys.train 12
toys.dinosaur 35
toys.truck 42
int x = (int) toys.truck;
-
8/2/2019 2. C Language Review
9/37
Keyword .NET Type Bytes Description
byte System.Byte 1 (0 to 255).
char System.Char 1 Unicode character.
bool System.Boolean 1 either true or false.
sbyte System.Sbyte 1 (
128 to 127).
short System.Int16 2 (32,768 to 32,767).
ushort System.Uint16 2 (0 to 65,535).
Types(1)
-
8/2/2019 2. C Language Review
10/37
Types(2)
int System.Int32 4 2147483647 to 2147483647
uint System.Uint32 4 (0 to 4,294,967,295).
float System.Single 4 Single-precision floating point
double System.Double 8 Double-precision floating.
decimal System.Decimal 8 Fixed-precision number up to
28 digits
long System.Int64 8 Signed 64-bit integer.ulong System.Uint64 8 Unsigned 64-bit integer.
object System.Object N/A System.Object class
string System.String N/A Unicode characters.
-
8/2/2019 2. C Language Review
11/37
Types(3) C# Is a Strongly Typed Language
("type safety").1. Dim theFloat As Double = 3.5
Dim X As Integer = 2
X = X + theFloat =>OK in VB
2. double theFloat = 3.5;
int X = 2;
X = X + theFloat; =>ERROR in C#
3. double theFloat = 3.5;
int X = 2;
X = X + (int) theFloat; => OK
-
8/2/2019 2. C Language Review
12/37
Commenting Code
// I am a comment!
double theNumber = 3.1415; // other
comment...
/* I am a comment! */
/* I
am another
comment! */
-
8/2/2019 2. C Language Review
13/37
Operator(1)Operator Arithmetic Meaning
+ Addition.- Subtraction.
* Multiplication.
/ Division.
% Modulus.Logical (Boolean and Bitwise)
& AND.
| OR.
^ Exclusive OR.
! NOT.
~ Bitwise complement.
&& Conditional AND
|| Conditional OR
-
8/2/2019 2. C Language Review
14/37
Operator(2)String Concatenation
+ Concatenates two strings.
Increment, Decrement
++ Increments operand by 1-- Decrements operand by 1
Comparison
== Equality.
!= Inequality.< Less than.
> Greater than.
= Greater than or equal.
-
8/2/2019 2. C Language Review
15/37
Operator(3)String Concatenation
+ Concatenates two strings.
Increment, Decrement
++ Increments operand by 1-- Decrements operand by 1
Comparison
== Equality.
!= Inequality.< Less than.
> Greater than.
= Greater than or equal.
-
8/2/2019 2. C Language Review
16/37
Operator(4)
Assignment
= Assigns the value of the right to the left
+= Addition assignment.-= Subtraction assignment.
Assignment
*= Multiplication assignment.
/= Division assignment.%= Modulus assignment.
&= AND assignment.
|= OR assignment.
^= Exclusive OR assignment.
-
8/2/2019 2. C Language Review
17/37
Operator(5)
Member Access
. used to access members of a type.
Indexing
[ ] Array indexing (square brackets are also
used to specify attributes).
Casting
( ) Conversion
as The as Operator
Conditional
?: Conditional operator
-
8/2/2019 2. C Language Review
18/37
Flow Control Statements
The same to java, c/c++
string className = "";
int year;
switch (className) {// string type
case QL04": year =1;break;
case QL03": year = 2;break;
}
-
8/2/2019 2. C Language Review
19/37
Structs
a simple user-defined type
contain properties, methods, and fields
do not support inheritance
structs derive from System.Object, like alltypes in C#
cannot inherit from any other class (orstruct), and no class or struct can derivefrom a struct.
-
8/2/2019 2. C Language Review
20/37
Structs
public struct Employee {
public string fullName, rank;
public Employee (string fullName, string rank){this.fullName = fullName;
this.rank = rank;
}}
-
8/2/2019 2. C Language Review
21/37
Structs
// Declare an instance
Employee A;
// InitializeA.fullName = Nguyen Van A";
OR
A.rank = Manager";
Employee B = new Employee(Nguyen Van B",Staff");
-
8/2/2019 2. C Language Review
22/37
Exception
The C# Exception object is used tostore information about errors and
abnormal events. trycatchfinallyStatements
finally blocks to close open files and
database connections, and generally tomake sure that all resources used by aprogram are released
-
8/2/2019 2. C Language Review
23/37
String
Study and present String andStringBuilderProperties and Methods
Dynamic Strings
System.Text.StringBuilder class allows youto create dynamic strings.
may be modified directly
Note: the difference between two class
http://string_property.doc/http://string_property.doc/ -
8/2/2019 2. C Language Review
24/37
-
8/2/2019 2. C Language Review
25/37
OOP
Declaring a Class
[access-modifier] class class-name{class-body}
public class Car { // declare the fieldspublic string make; public string model;
public string color; public int yearBuilt;
// define the methods
public void Start() {System.Console.WriteLine(model + " started");
}
}
-
8/2/2019 2. C Language Review
26/37
Creating Objects
The following statements create a Car object:
Car myCar;myCar = new Car();
myCar.make = "Porsche";
myCar .model = "Boxster";
myCar .color = "red";
myCar .yearBuilt = 2000;
-
8/2/2019 2. C Language Review
27/37
Defining Methods
[access-modifier] return-type method-name
( [parameter-typeparameter-name[, ...]] )
{method-body}
-
8/2/2019 2. C Language Review
28/37
Defining Properties
public class Car {// declare a private fieldprivate string make;
// declare a propertypublic string Make {get { return make; }
set { make = value; }}}
-
8/2/2019 2. C Language Review
29/37
Access Modifiers Access
Access Modifier Accessibility
public without restriction.
protected internal only accessible within the class,
a derived class, or class in the
same program
internal only accessible within the class
or class in the same program
protected only accessible within the class
or derived classes.
private only accessible within the class.
This is the default.
-
8/2/2019 2. C Language Review
30/37
Using Constructors
public class Car {
public() { System.Console.WriteLine("In
Car() constructor");
}
}
-
8/2/2019 2. C Language Review
31/37
Using Destructors
public class Car {
// define the destructor
~Car() {
// do any cleaning up here
}}
-
8/2/2019 2. C Language Review
32/37
Introducing Inheritance
public class MotorVehicle {public string model;public MotorVehicle(string model) {
this.model = model;}public void Start() {Console.WriteLine(model + " started");
}}
-
8/2/2019 2. C Language Review
33/37
Inheritance(cont)
public class Car : MotorVehicle {
bool convertible;
public Car(string model, bool convertible) : base(model)// calls the base class constructor
{
this.convertible = convertible;
}}
-
8/2/2019 2. C Language Review
34/37
Polymorphism
Polymorphismin a class(Overload) meansthat a class can do the same thing in different
ways. Methods with different parametersignatures offer one form of polymorphism.
Polymorphism in classes(Override) meansthat several classes can have the same
method, but that method happens differentlyin each class
-
8/2/2019 2. C Language Review
35/37
Overload
In a class
public double getSquare(double theNumber){
return(theNumber * theNumber);} // end getSquare
public double getSquare(int theNumber){
return (int)(theNumber * theNumber);
} // end getSquare
-
8/2/2019 2. C Language Review
36/37
Override
public class MotorVehicle {
// define the Accelerate() method (may
be overridden in a derived class)public virtual void Accelerate() {Console.WriteLine(model + "
accelerating");}
}
-
8/2/2019 2. C Language Review
37/37
public class Car : MotorVehicle {
// override Accelerate() method
public override void Accelerate() {Console.WriteLine("Pushing gas pedal of " +model);
// calls the base class Accelerate() method
base.Accelerate();}
}