Beruflich Dokumente
Kultur Dokumente
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, professional language, but learning it doesnt have to be boring
Basic C# concepts
Identifiers used to denote variables, constants, types, methods, objects... The same to java, c/c++ Note: @+keyword string @if = "@if";
abstract as base bool break byte case catch char checked class const continue decimal default delegate
do double else enum event explicit extern false finally fixed float for foreach goto if implicit
in int interface internal is lock long namespace new null object operator out override params private
protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw
true try typeof uint ulong unchecke unsafe d ushort using virtual void volatile while
Variables
A variable combines 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 object
Note: variables be assigned a value before they are used
Constants
const keyword followed by the constant's type const float PI = 3.141592; const string className = QL;
Enumeration Constants
enum toys {train, dinosaur, truck};
Enumerator toys.train toys.dinosaur toys.truck Value 0 1 2
Value 12 35 42
Types(1)
Keyword .NET Type byte char System.Byte System.Char Bytes 1 1 Description (0 to 255). Unicode character.
bool
System.Boolean 1
1 2 2
Types(2)
int uint float double decimal long ulong object string System.Int32 System.Uint32 System.Single System.Double System.Decimal System.Int64 System.Uint64 System.Object System.String 4 4 4 8 8 8 8 N/A N/A 2147483647 to 2147483647 (0 to 4,294,967,295). Single-precision floating point Double-precision floating. Fixed-precision number up to 28 digits Signed 64-bit integer. Unsigned 64-bit integer. System.Object class Unicode characters.
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
Commenting Code
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
Operator(2)
String Concatenation + Increment, Decrement ++ -Comparison == != < > <= >= Concatenates two strings. Increments operand by 1 Decrements operand by 1 Equality. Inequality. Less than. Greater than. Less than or equal. Greater than or equal.
Operator(3)
String Concatenation + Increment, Decrement ++ -Comparison == != < > <= >= Concatenates two strings. Increments operand by 1 Decrements operand by 1 Equality. Inequality. Less than. Greater than. Less than or equal. Greater than or equal.
Operator(4)
Assignment = += -= Assignment *= /= %= &= |= ^= Assigns the value of the right to the left Addition assignment. Subtraction assignment. Multiplication assignment. Division assignment. Modulus assignment. AND assignment. OR assignment. Exclusive OR assignment.
Operator(5)
Member Access . Indexing [] Casting () as Conditional ?: used to access members of a type.
Array indexing (square brackets are also used to specify attributes). Conversion The as Operator
Conditional operator
Structs
a simple user-defined type contain properties, methods, and fields do not support inheritance structs derive from System.Object, like all types in C# cannot inherit from any other class (or struct), and no class or struct can derive from a struct.
Structs
public struct Employee { public string fullName, rank; public Employee (string fullName, string rank){ this.fullName = fullName; this.rank = rank;
} }
Structs
// Declare an instance Employee A; // Initialize A.fullName = Nguyen Van A"; OR A.rank = Manager"; Employee B = new Employee(Nguyen Van B", Staff");
Exception
The C# Exception object is used to store information about errors and abnormal events. trycatchfinally Statements finally blocks to close open files and database connections, and generally to make sure that all resources used by a program are released
String
Study and present String and StringBuilder Properties and Methods Dynamic Strings System.Text.StringBuilder class allows you to create dynamic strings. may be modified directly Note: the difference between two class
myDateTime6 = 1/13/2004 11:10:30 PM myTimeSpan = 1.02:04:10 myDateTime6 + myTimeSpan = 1/15/2004 1:14:40 AM myDateTime6 - myTimeSpan = 1/12/2004 9:06:20 PM Property and methods
OOP
Declaring a Class [access-modifier] class class-name {class-body}
public class Car { // declare the fields public string make; public string model; public string color; public int yearBuilt; // define the methods public void Start() { System.Console.WriteLine(model + " started"); } }
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;
Defining Methods
Defining Properties
public class Car { // declare a private field private string make; // declare a property public string Make { get { return make; } set { make = value; } } }
protected internal
internal protected
private
only accessible within the class, a derived class, or class in the same program only accessible within the class or class in the same program only accessible within the class or derived classes. only accessible within the class. This is the default.
Using Constructors
Using Destructors
public class Car { // define the destructor ~Car() { // do any cleaning up here } }
Introducing Inheritance
public class MotorVehicle { public string model; public MotorVehicle(string model) { this.model = model; } public void Start() { Console.WriteLine(model + " started"); } }
Inheritance(cont)
public class Car : MotorVehicle { bool convertible; public Car(string model, bool convertible) : base(model) // calls the base class constructor { this.convertible = convertible; } }
Polymorphism
that a class can do the same thing in different ways. Methods with different parameter signatures offer one form of polymorphism. Polymorphism in classes(Override) means that several classes can have the same method, but that method happens differently in each class
Overload
In a class
public double getSquare(double theNumber){ return(theNumber * theNumber); } // end getSquare
Override
public class MotorVehicle { // define the Accelerate() method (may be overridden in a derived class) public virtual void Accelerate() { Console.WriteLine(model + " accelerating"); } }
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(); } }