
Một số điểm khác của C# so với C++
lượt xem 20
download

C# là một ngôn ngữ lập trình hướng đối tượng được phát triển bởi Microsoft, là phần khởi đầu cho kế hoạch .NET của họ. Tên của ngôn ngữ bao gồm ký tự thăng theo Microsoft nhưng theo ECMA là C#, chỉ bao gồm dấu số thường. Microsoft phát triển C# dựa trên C++ và Java. C# được miêu tả là ngôn ngữ có được sự cân bằng giữa C++, Visual Basic, Delphi và Java
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Một số điểm khác của C# so với C++
- B tr 1 M t s ñi m khác c a C# so v i C++ Chương trình C# ñơn gi n /* This is the Hello world Program in C# */ using System; class HelloWorldDemo { public static void Main() { Console.WriteLine (“This is the Hello World program”); } } C# Simplified / Session 1 / 2 of 45 1
- Khai báo bi n AccessModifier DataType Variable int Public string Private float Protected C# Simplified / Session 1 / 3 of 45 Bi n trùng tên v i t khóa, s d ng @ using System; class VariableDemo { public static void Main() { string @string; @string = ”string is a keyword but used as a variable name in this example”; Console.WriteLine (@string); } } C# Simplified / Session 1 / 4 of 45 2
- C# cung c p nhi u ki u d li u, c ki u s r t l n Ki u d li u Mô t Ví d trong C# object Ki u d li u cơ s object o = null; string Xâu string s = “hello”; int Nguyên 32 bit int val = 12; byte byte byte val = 12; float Th c 64 bit float val = 1.23F; bool Logic bool val1 = true; bool val2 = false; char Ký t char val = 'h'; C# Simplified / Session 1 / 5 of 45 Khai báo m ng Ki u[] ten = new Ki u[s _ph n_t ] Ví d : int[] a = new int[200]; String[] s = new String[20]; C# Simplified / Session 1 / 6 of 45 3
- Vào / Ra trong C# V i ng d ng bàn giao ti p (console) S d ng ñ i tư ng System.Console v i các phương th c ñ c/ghi hay ñư c s d ng nh t là Console.ReadLine() Console.WriteLine() C# Simplified / Session 1 / 7 of 45 Vào / Ra trong C# - Ví d using System; class TestDefaultValues { static void Main() { int number, result; number=5; result=100 * number; Console.WriteLine (“Result is {0} when 100 is multiplied by number {1}”, result, number); } } {i}, i = 0, 1, … trong xâu ký t tham s th nh t ñư c g i là giành ch . Ký t giành ch {i} s ñư c thay th b i giá tr c a tham s th i trong danh sách khi in. C# Simplified / Session 1 / 8 of 45 4
- C u trúc ñi u khi n switch cho phép s d ng b t kỳ giá tr gì ñ làm giá tr so sánh switch(thXuly) { case “NGHI_HOC”: Console.Write(“Ban da nghi hoc”); break; case “DI_HOC_MUON”: Console.Write(“Ban di hoc muon”); break; default: Console.Write(“Ban di hoc rat tot”); break; } Giá tr so sánh v i các case trong ví d này là xâu ký t C# Simplified / Session 1 / 9 of 45 L nh l p foreach (1) S d ng ñ l p và duy t các ph n t trong collection ho c array Cú pháp - C# Simplified / Session 1 / 10 of 45 5
- L nh l p foreach – Ví d using System; public class ForEachDemo { static void Main (String[] args) { int index; String[] array1 = new String[3]; for (index=0; index
- Hai ki u d li u trong C# Dùng ñ lưu giá tr Value Types Are stored in a stack Dùng ñ lưu ñ a ch (tham chi u) Reference Types c a ñ i tư ng = null nghĩa là không tham chi u ñ n ñ i tư ng nào c C# Simplified / Session 1 / 13 of 45 Value Types using System; class DataTypeTest { public static void Main() { int variableVal = 100; funcTest(variableVal); Console.WriteLine(“This value of the variable is {0}",variableVal); } static void funcTest (int variableVal) { int tempVar = 10; variableVal = tempVar*20; } } C# Simplified / Session 1 / 14 of 45 7
- Reference Types using System; class DataTypeTest { public int variableVal; } class DataTypeTestRef { static void Main() { DataTypeTest dataTest = new DataTypeTest(); dataTest.variableVal = 100; funcDataTypeTest(dataTest); Console.WriteLine (dataTest.variableVal); } C# Simplified / Session 1 / 15 of 45 Reference Types - Contd static void funcDataTypeTest(DataTypeTest dataTest) { int tempVar = 10; dataTest.variableVal = tempVar*20; } } C# Simplified / Session 1 / 16 of 45 8
- C# s d ng bi n tham chi u ref ho c bi n ra out thay cho con tr ñ th ng nh t cách x lý và an toàn khi s d ng public void ham1(int vao, ref int vaora, out int ra) { ra = vao + vaora; vaora = vao – vaora; } K t qu : int x = 10, y = 6, z; x = 10 ham1(x, ref y, out z); y=4 z = 16 C# Simplified / Session 1 / 17 of 45 T t c các ki u d li u trong C# ñ u k th a t ki u cơ s object using System; class ObjectProff { public static void Main() { string objectVal; objectVal = 7.ToString(); Console.WriteLine (“The value now is ”+objectVal); } } C# Simplified / Session 1 / 18 of 45 9
- C u trúc có c phương th c struct SINHVIEN { public string hoten; public byte tuoi; public void datTen(string ht) { hoten = ht; } } C# Simplified / Session 1 / 19 of 45 Ki u li t kê (Enumerators) public enum WeekDays { Monday, Tuesday, Wednesday, Thursday, Friday } Theo m c ñ nh, các giá tr li t kê l n lư t là 0, 1, 2, … Ví d , v i ki u li t kê trên, Monday = 0, Friday = 4 C# Simplified / Session 1 / 20 of 45 10
- Ki u li t kê Có th ñ nh l i giá tr cho các thành ph n li t kê public enum WeekDays { Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5 } C# Simplified / Session 1 / 21 of 45 Ch ng phương th c Hai cách ch ng phương th c – S lư ng tham s khác nhau Ki u c a tham s khác nhau C# Simplified / Session 1 / 22 of 45 11
- Ch ng phương th c – S lư ng tham s khác nhau using System; public class Area { private int areaVal; public void AreaCal(int radius) { areaVal = (22/7)* radius*radius; } public void AreaCal(int length, int breadth) { areaVal = length*breadth; } public void AreaCal(int length, int breadth, int height) { areaVal = length*breadth*height; } … } C# Simplified / Session 1 / 23 of 45 Ch ng phương th c – Tham s khác ki u ... public void Add(int number1, int number2) { sum = number1 + number2; } public void Add(string value1, string value2) { int sum; sum = Int32.Parse(value1) + Int32.Parse(value2); Console.WriteLine ("Sum is {0}",sum); Console.WriteLine ("Strings are converted to integers to add”); } ... C# Simplified / Session 1 / 24 of 45 12
- Ch ng toán t Các toán t sau ñư c phép ñ nh nghĩa ch ng C# Simplified / Session 1 / 25 of 45 Ch ng toán t public static void Main() using System; { public class Distance Distance start = new Distance(); { Distance objDistance = new Distance(); int longitude, latitude; Distance finish = new Distance(); public Distance() start.longitude = 12; This statement does not { start.latitude = 10; return an error as the ‘-’ longitude = 0; finish.longitude = 2; operator is overloaded latitude = 0; finish.latitude = 1; } objDistance = start - finish; Console.WriteLine ("The Finish is {0} Degrees East public Distance(int longitude, int latitude) and {1} Degrees North of the Start.", { objDistance.longitude,objDistance.latitude); this.longitude = longitude; } this.latitude = latitude; } public static Distance operator - (Distance first, Distance second) { return new Distance(first.longitude - second.longitude, first.latitude - second.latitude); } //main } C# Simplified / Session 1 / 26 of 45 13
- L p b bao b c (Sealed Class) Không cho l p nào k th a t nó S d ng t khóa ‘sealed’. … sealed class classOne { //Class Implementation } … C# Simplified / Session 1 / 27 of 45 L p tr u tư ng L p tr u tư ng là l p ñư c s d ng ñ k th a và không th bao b c nó C# s d ng t khóa abstract trư c ñ nh nghĩa l p ñ ch m t l p là l p tr u tư ng M t l p tr u tư ng có th có phương th c không tr u tư ng Khi mu n vi t l i m t phương th c tr u tư ng l p k th a, ta dùng t khóa override C# Simplified / Session 1 / 28 of 45 14
- L p tr u tư ng – Ví d using System; abstract class BaseClass { public abstract void MethodA(); public void MethodB() { Console.WriteLine ("This is the non abstract method”); } } class DerivedClass : BaseClass { public override void MethodA() { Console.WriteLine ("This is the abstract method overriden in derived class"); } } C# Simplified / Session 1 / 29 of 45 class AbstractDemo { public static void Main() { DerivedClass objDerived = new DerivedClass(); BaseClass objBase = objDerived; objBase.MethodA(); objDerived.MethodB(); } } C# Simplified / Session 1 / 30 of 45 15
- Giao di n (Interfaces) Giao di n là l p tr u tư ng ch ch a các phương th c tr u tư ng mà m i phương th c ch có ch ký, không có thân L p k th a m t giao di n ph i cài ătj t t c các phương th c c a giao di n public interface IFile { int delFile(); void disFile(); } C# Simplified / Session 1 / 31 of 45 Giao di n – Ví d public interface IFile { int delFile(); void disFile(); } public class MyFile : IFile { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } } C# Simplified / Session 1 / 32 of 45 16
- Giao di n - Output class InterfaceDemo { public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); } } public class BaseforInterface { public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); } } C# Simplified / Session 1 / 33 of 45 Giao di n – K th a public interface IFile { int delFile(); void disFile(); } public class BaseforInterface { public void open() { System.Console.WriteLine ("This is the open method of BaseforInterface"); } } C# Simplified / Session 1 / 34 of 45 17
- Giao di n – K th a public class MyFile : BaseforInterface, IFile { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } } C# Simplified / Session 1 / 35 of 45 Giao di n – Output v k th a class Test { static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); } } C# Simplified / Session 1 / 36 of 45 18
- K th a t nhi u giao di n public interface IFileTwo { void applySecondInterface(); } C# Simplified / Session 1 / 37 of 45 K th a nhi u giao di n public class MyFile : BaseforInterface, IFile, IFileTwo { public int delFile() { System.Console.WriteLine ("DelFile Implementation!"); return(0); } public void disFile() { System.Console.WriteLine ("DisFile Implementation!"); } public void applySecondInterface() { System.Console.WriteLine ("ApplySecondInterface Implementation!"); } } C# Simplified / Session 1 / 38 of 45 19
- K th a nhi u giao di n - Output class MultipleInterfaces { public static void Main() { MyFile objMyFile = new MyFile(); objMyFile.disFile(); int retValue = objMyFile.delFile(); objMyFile.open(); objMyFile.applySecondInterface(); } } C# Simplified / Session 1 / 39 of 45 Namespaces M t namespace (không gian tên) là m t ñơn v logic c a ph n m m bao g m m t ho c nhi u l p Namespace ñư c s d ng ñ t ch c mã ngu n tránh xung ñ t tên gi m ñ ph c t p khi s d ng l i C# Simplified / Session 1 / 40 of 45 20

CÓ THỂ BẠN MUỐN DOWNLOAD
-
Kiến thức cơ bản về mạng: Phần 6 - Windows Domain .Trong một số bài trước
6 p |
190 |
85
-
Hướng dẫn toàn diện về bảo mật Windows 7 – Phần 2
19 p |
160 |
62
-
Kiến thức cơ bản về mạng: Phần 15 – Universal Groups & Group Nesting
7 p |
161 |
62
-
Thực thi và khắc phục sự cố triển khai chứng chỉ trong ISA Server 2006 – Phần 2
9 p |
138 |
43
-
Chương 1: Ôn tập một số nội dung chính của NNLTC
31 p |
150 |
33
-
C# and .NET Framework Lassion 5
9 p |
147 |
30
-
Bài giảng Lập trình C# 2010: Chương 1 - ĐH Công nghệ Đồng Nai
42 p |
128 |
21
-
Các mẹo Windows cần thiết
20 p |
109 |
17
-
Struct
5 p |
111 |
13
-
Phương thức ảo và tính đa hình
8 p |
196 |
9
-
Bài giảng: Bài 2. Ngôn ngữ lập trình C++
16 p |
131 |
8
-
Kiểm tra khả năng tương thích của Windows 7 – Phần 1
4 p |
113 |
8
-
Bài 11 Mục tiêu: Kết thúc bài học này, bạn có thể:
14 p |
64 |
4
-
Charms trong Windows 8: cách gọi & sử dụng
3 p |
103 |
4
-
Một số chủ đề phỏng vấn ứng viên C/C++
2 p |
67 |
4


Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn
