YOMEDIA
ADSENSE
Bài giảng NET programming (2014) - Nguyễn Đạt Thông
69
lượt xem 8
download
lượt xem 8
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Bài giảng NET programming trình bày những nội dung cơ bản như sau: Tổng quan về .NET framework và MS Visual Studio .NET, tổng quan về C#, lập trình Windows Forms, đa luồng và kết nối mạng, kết nối cơ sở dữ liệu, Windows Presentation Foundations và Silverlight. Mời các bạn cùng tham khảo.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Bài giảng NET programming (2014) - Nguyễn Đạt Thông
- 2/19/2014 Nội dung 1. Tổng quan về .NET framework và MS Visual Studio .NET Nguyen Dat Thong 2. Tổng quan về C# Faculty of Mathematics and Informatics University of Science, VNU-HCM 3. Lập trình Windows Forms 4. Đa luồng và kết nối mạng 5. Kết nối cơ sở dữ liệu 6. Windows Presentation Foundations và Silverlight .NET Programming - Nguyễn Đạt Thông 2 Tổng quan về .NET Framework .NET Framework là gì? Common Language Runtime Chương 1 Base Class Library Common Language Infrastructure Assembly .NET Programming - Nguyễn Đạt Thông 3 .NET Programming - Nguyễn Đạt Thông 4 1
- 2/19/2014 .NET Framework là gì? Mục tiêu của .NET Framework The .NET Framework is a technology that supports building and To provide running the next generation of applications and XML Web services. a consistent object-oriented programming environment . The .NET Framework is a managed execution environment that a code-execution environment that minimizes software deployment and provides a variety of services to its running applications. versioning conflicts. a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party. a code-execution environment that eliminates the performance problems of scripted or interpreted environments. .NET Programming - Nguyễn Đạt Thông 5 .NET Programming - Nguyễn Đạt Thông 6 Mục tiêu của .NET Framework Các dịch vụ của .NET Framework To make the developer experience consistent across widely varying Memory management. Language interoperability. types of applications, such as Windows-based applications and Web-based applications. A common type system. Version compatibility. To build all communication on industry standards to ensure that code based on the .NET Framework can integrate with any other An extensive class library. Side-by-side execution. code. Development frameworks Multi-targeting. and technologies. .NET Programming - Nguyễn Đạt Thông 7 .NET Programming - Nguyễn Đạt Thông 8 2
- 2/19/2014 Kiến trúc .NET Framework Common Language Runtime (CLR) .NET Framework consists of two major components: The common language runtime (CLR) manages the common language runtime (CLR) memory, thread execution, code execution, code safety verification, compilation, the .NET Framework Class Library and other system services. All programs by any language which has written for the .NET framework are executed by the CLR and it is common to all versions of the .NET. The runtime enforces code access security. .NET Programming - Nguyễn Đạt Thông 9 .NET Programming - Nguyễn Đạt Thông 10 Các thành phần của CLR Common Type System (CTS) Common Type System (CTS) CTS VB .NET C# C++ System.Byte Byte byte unsigned char System.SByte SByte sbyte unsigned char Intermediate Language (IL) System.Int16 Short short short System.Int32 Integer int int or long System.Int64 Long long __int64 Just-In-Time (JIT) Compiler System.UInt16 UShort ushort unsigned short System.UInt32 UInteger uint unsigned int or unsigned long Garbage Collector (GC) System.UInt64 ULong ulong unsigned __int64 System.Single Single float float System.Double Double double double System.Object Object object object^ System.Char Char char wchar_t System.String String string String^ System.Decimal Decimal decimal Decimal System.Boolean Boolean bool bool .NET Programming - Nguyễn Đạt Thông 11 .NET Programming - Nguyễn Đạt Thông 12 3
- 2/19/2014 Intermediate Language (IL) Just-In-Time (JIT) Compiler Intermediate Language (IL) is a CPU-independent set of instructions. JIT Compiler is a component inside the “runtime” which performs compilation. IL is not byte-code, but is very close to it. Therefore, when the application is executed, the IL to machine code conversion The process of compiling IL code into machine code takes some is quick. time, but for each part of a program, it generally means only one-time compile per IL converted to CPU-specific code by Just-In-Time Compiler of process. Common Language Runtime (CLR) Once the code is compiled, the CLR holds it and executes the compiled version the next time it’s needed. .NET Programming - Nguyen Dat Thong 13 .NET Programming - Nguyễn Đạt Thông 14 Garbage Collector (GC) Các ứng dụng .NET Garbage Collection is the process of automatically de-allocating memory based on the program’s C# C# needs. Code Compiler GC’s purpose is to restore memory consumed by objects that are no longer referenced. Visual Basic Visual Basic JIT Compiler Code IL Compiler (CLR) GC does not handle other resources such as database connections, handles (files, windows, and so on), COBOL COBOL network ports, Code Compiler and hardware devices such as serial ports. Native Code .NET Programming - Nguyễn Đạt Thông 15 .NET Programming - Nguyen Dat Thong 16 4
- 2/19/2014 Base Class Library Base Class Library Standardized Non-standardized Base Class Library (BCL) is a core set of CLR class libraries System System.CodeDom that programs may utilize System.Collections System.ComponentModel System.Diagnostics System.Configuration and it is available for all CLR languages. System.Globalization System.Data System.IO System.Deployment System.Net System.DirectoryServices Framework Class Library (FCL) that adds some features to BCL. It System.Reflection System.Drawing includes support for System.Runtime System.Security System.EnterpriseServices System.Linq Client UI, System.Text System.Linq.Expressions System.Threading System.Management web UI, System.Xml System.Media database access, System.Diagnostics.CodeAnalysis System.Messaging System.Diagnostics.Contracts System.Resources distributed communication, System.Diagnostics.Eventing System.ServiceProcess and other libraries. System.Diagnostics.PerformanceData System.Timers System.Diagnostics.SymbolStore System.Transactions System.Web System.Windows.Forms .NET Programming - Nguyễn Đạt Thông 17 .NET Programming - Nguyễn Đạt Thông 18 Common Language Infrastructure (CLI) CLI The Common Language Infrastructure (CLI) The (CLR) is Microsoft's implementation of the CLI standard and is is an open specification that describes the executable code and runtime responsible for managing the execution of .NET programs. environment. Implementation Description Microsoft .NET framework Premier compiler which limited to run on the Windows. It developed by Microsoft and standardized by ISO and ECMA. CLI Cross-platform implementation that runs on both the Windows family Microsoft Silverlight and the Macintosh. Light-weight implementation of the .NET Framework designed to run on Microsoft Compact Framework It forms the core of the Microsoft .NET and other CLI Microsoft XNA PDAs and phones. Implementation for Xbox and Windows game developers. implementations. Mono Project Open source implementation designed to provide Windows, Linux, and Unix compatible version of the CLI. Focused on platform-portable applications that can run both the .NET and DotGNU the DotGNU. Supports GNU/Linux BSD, Cygwin/Mingw32, Mac OS X, Solaris, AIX, and PARISC. Also known as the Shared Source CLI. It’s CLI implementation that Rotor Microsoft developed to run on Windows, Mac OS X, and FreeBSD for educational purposes. .NET Programming - Nguyễn Đạt Thông 19 .NET Programming - Nguyễn Đạt Thông 20 5
- 2/19/2014 Assembly Assembly According to CLI specification, assembly is the CLI output after An assembly can contain more than one code module which has code’s compilation. used several different languages. Not support by Microsoft Visual Studio Assemblies can be library assemblies (DLL – Class Libraries) or Assemblies form the logical construct of a component or unit of process assemblies (EXE - Executables). deployment. An assembly can reference other assemblies and files Although an assembly can include multiple files, there is only one such as resource files that have been localized. version number. An assembly can consist of one or more files. Microsoft has provided “ILMerge.exe” utility for combining multiple Code files are called modules. modules and their manifests into a single file assembly. .NET Programming - Nguyễn Đạt Thông 21 .NET Programming - Nguyễn Đạt Thông 22 Assembly Assembly The files consisted in an assembly can include compiled modules, Assemblies are either strongly named or not strongly named. resources, and any other components listed in the assembly manifest. A strongly named assembly has a hash code built into its manifest that the loader can use to test the integrity of the assembly to ensure it has not The assembly manifest is typically included in the main assembly been modified. module and contains essential identification information, including which pieces belong to the assembly. Assemblies can also be digitally signed in order to identify their producer. .NET Programming - Nguyễn Đạt Thông 23 .NET Programming - Nguyễn Đạt Thông 24 6
- 2/19/2014 Ghi chú Tổng quan về MS Visual Studio .NET ………………………………………………………………………………… Visual Studio .NET ………………………………………………………………………………… ………………………………………………………………………………… Solution, Project, and Configuration ………………………………………………………………………………… ………………………………………………………………………………… Build and Clean ………………………………………………………………………………… ………………………………………………………………………………… ………………………………………………………………………………… Breakpoint ………………………………………………………………………………… ………………………………………………………………………………… Debugging ………………………………………………………………………………… ………………………………………………………………………………… MSDN .NET Programming - Nguyễn Đạt Thông 25 .NET Programming - Nguyễn Đạt Thông 26 Visual Studio .NET Visual Studio .NET Microsoft Visual Studio is an integrated development environment It is used to develop in both (IDE) from Microsoft. native code together with managed code It is used to develop console and graphical user interface applications along with It is used to develop for all platforms supported by Windows Forms applications, Microsoft Windows, web sites, Windows Mobile, web applications, Windows CE, and web services .NET Framework, .NET Compact Framework and Microsoft Silverlight. .NET Programming - Nguyễn Đạt Thông 27 .NET Programming - Nguyễn Đạt Thông 28 7
- 2/19/2014 Tính năng của Visual Studio .NET Trình soạn thảo Code Editor IntelliSense Code Refactoring Debugger Designer forms designer, web designer, class designer, and database schema designer .NET Programming - Nguyễn Đạt Thông 29 .NET Programming - Nguyễn Đạt Thông 30 Debugger Designer .NET Programming - Nguyễn Đạt Thông 31 .NET Programming - Nguyễn Đạt Thông 32 8
- 2/19/2014 Solution và Project Solution A solution is a grouping of Visual Studio stores the definition for a solution in two files one or more projects that work together to create an application. .sln and .suo Projects are used in a solution to The solution definition file (.sln) stores logically manage, build, and debug the items that make up your application. Projects that are associated with the solution. Solution build configurations. The output of a project is usually an executable program (.exe), The solution user options file (.sou) stores a dynamic-link library (.dll) file or metadata which is used to customize the IDE whenever the solution is active. a module, among others. .NET Programming - Nguyễn Đạt Thông 33 .NET Programming - Nguyễn Đạt Thông 34 Tạo một Solution mới Solution Files On the File menu, click New and then click New Project. .NET Programming - Nguyễn Đạt Thông 35 .NET Programming - Nguyễn Đạt Thông 36 9
- 2/19/2014 Công cụ Solution Explorer Project Solution Explorer provides Visual Studio provides several pre-defined project templates. an organized view of projects and their files as well as ready access to the commands that pertain to them. These templates are used to develop an application, a class, a control, or a library. To access Solution Explorer, select Solution Explorer on the View menu The default templates in Visual Studio include Visual Basic projects Visual C# projects Visual C++ projects .NET Programming - Nguyễn Đạt Thông 37 .NET Programming - Nguyễn Đạt Thông 38 Tạo một Project mới Một số Templates thông dụng On the File menu, click New and then click New Project. Template More Information The Empty Project template can be used when user wants to Empty Project create his own project type Use the Class Library template to quickly create reusable Class Library classes and components that can be shared with other projects Console Console applications are typically designed without a graphical Application user interface (GUI) and are compiled into an executable file. Windows Forms This creates a traditional standalone Windows application or a Application rich front-end to a distributed Web application. Windows Forms The Windows Control Library project template is used to create Control Library custom controls to use on Windows Forms ASP.NET Web Creates a new Web application. Application … .NET Programming - Nguyễn Đạt Thông 39 .NET Programming - Nguyễn Đạt Thông 40 10
- 2/19/2014 Các thành phần của Project Thêm “Reference” vào Project Properties Select the project, and open “Add Reference…” from Project menu. References Reference can be Source Files .NET libraries COM Projects in Solution Other libraries .NET Programming - Nguyễn Đạt Thông 41 .NET Programming - Nguyễn Đạt Thông 42 Thêm thành phần vào Project Configuration Select the project, and Solution build configurations open “Add New Item…” from Project menu. let you select which projects to include in different builds of a project or solution. Project configurations include detailed build and debug settings for each combination of solution build configuration and platform. To change configurations, open Configuration Manager from Build menu. .NET Programming - Nguyễn Đạt Thông 43 .NET Programming - Nguyễn Đạt Thông 44 11
- 2/19/2014 Configuration Manager Build và Clean To build a project or entire solution, select “Build XXX” from Build menu. To clean project or entire solution, select “Clean XXX” from Build menu. To rebuild (clean and build again) a project or entire solution, select “Rebuild XXX” from Build menu. .NET Programming - Nguyễn Đạt Thông 45 .NET Programming - Nguyễn Đạt Thông 46 Errors và Warnings Start và Debug Building a project or entire solution could raise errors and warnings. Once projects are built successfully, Visual Studio can start running “StartUp Project” and debugging all projects within the solution. Visual Studio helps to point out where they occurs and gives their causes. Projects should be built in “Debug” mode. To start debugging, choose an executable project Select “Set as StartUp Project” Select “Start new instance” from “Debug”. .NET Programming - Nguyễn Đạt Thông 47 .NET Programming - Nguyễn Đạt Thông 48 12
- 2/19/2014 Breakpoints Đặt breakpoint Breakpoints are points that break the debugging application. To set a breakpoint Open the source file Move to code line to be broken The debugging application will be broken Select “Toggle Breakpoint” from “Debug” menu. as soon as it meets a “breakpoint”. To remove a breakpoint Move cursor to that point Select “Toggle Breakpoint” again To remove or disable all breakpoints Select “Delete All Breakpoints” or Select “Disable All Breakpoints” .NET Programming - Nguyễn Đạt Thông 49 .NET Programming - Nguyễn Đạt Thông 50 Thay đổi thuộc tính của breakpoint Thuộc tính của breakpoint Select an existing breakpoint to change it properties Location Where breakpoint is set Condition When need to break Hit Count Ignore some hits before break Filter Break on specific processes and threads When Hit Specific what to do when breakpoint is hit .NET Programming - Nguyễn Đạt Thông 51 .NET Programming - Nguyễn Đạt Thông 52 13
- 2/19/2014 Tùy chỉnh breakpoint Debugging Once the application is broken at a breakpoint, Visual Studio provides current state and information that helps programmers to debug the program. The information include States of threads Values of variables Call Stack .NET Programming - Nguyễn Đạt Thông 53 .NET Programming - Nguyễn Đạt Thông 54 Debugging Step–by–Step Step–by–Step Once the application is broken at a breakpoint, Visual Studio provides step–by–step running mechanism that helps Step Over programmers find the problems. Step Over ○ Execute current statement and move to next one Step Into Step Out Step Into ○ If current statement is a call to a procedure, the next statement displayed is the first statement in the procedure Step Over Step Out ○ Executes the remaining lines of a function in which the current execution point lies. Continue .NET Programming - Nguyễn Đạt Thông 55 .NET Programming - Nguyễn Đạt Thông 56 14
- 2/19/2014 MSDN MSDN Links MSDN is a reference for all Microsoft developers. MSDN http://msdn.microsoft.com/en-US/ It provides information about all Microsoft classes, structures and functions. MSDN Library http://msdn.microsoft.com/library/default.aspx It also provides sample code and technical articles. MSDN .NET Development http://msdn.microsoft.com/en-us/library/ff361664.aspx MSDN could be used offline and integrated with Microsoft Visual Studio. MSDN .NET Class Library http://msdn.microsoft.com/en-us/library/ms229335(v=vs.90).aspx .NET Programming - Nguyễn Đạt Thông 57 .NET Programming - Nguyễn Đạt Thông 58 MSDN Web MSDN Web .NET Programming - Nguyễn Đạt Thông 59 .NET Programming - Nguyễn Đạt Thông 60 15
- 2/19/2014 MSDN Web MSDN trong Visual Studio .NET Programming - Nguyễn Đạt Thông 61 .NET Programming - Nguyễn Đạt Thông 62 Cài đặt MSDN Thiết lập cấu hình MSDN Select “Tool >> Options >> Environment >> Help” Select “Integrated Help Viewer” .NET Programming - Nguyễn Đạt Thông 63 .NET Programming - Nguyễn Đạt Thông 64 16
- 2/19/2014 Tóm lược Ghi chú How to create a solution and projects? ………………………………………………………………………………… ………………………………………………………………………………… How to add new items to a project? ………………………………………………………………………………… ………………………………………………………………………………… ………………………………………………………………………………… How to build and debug a program? ………………………………………………………………………………… ………………………………………………………………………………… How to set and customize breakpoints? ………………………………………………………………………………… ………………………………………………………………………………… ………………………………………………………………………………… How to step–by–step debug? ………………………………………………………………………………… ………………………………………………………………………………… .NET Programming - Nguyễn Đạt Thông 65 .NET Programming - Nguyễn Đạt Thông 66 Tổng quan C# C# Overview First program: Hello C# Statements and Expressions Controlling program Chương 2 Variables Data types Value Types Reference Types Type Conversion Console Input/Ouput .NET Programming - Nguyễn Đạt Thông 67 .NET Programming - Nguyễn Đạt Thông 68 17
- 2/19/2014 Tổng quan về C# Đặc trưng của C# C# is developed by Anders Hejlsberg and Scott Wiltamuth C# is a completely Object-Oriented Language. at Microsoft Corporation. In C#, every program is class. C# language is similar to C++ because it is developed from C++ and Java. In C#, every work is done through objects. C# is added many new features allowing programmers to program easier and friendlier. .NET Programming - Nguyễn Đạt Thông 69 .NET Programming - Nguyễn Đạt Thông 70 Tính năng tiêu biểu của C# Ngôn ngữ C# Provides native support for COM (Component Object Model) C# source code compiles into managed code an intermediate language (IL). Manages memory allocation and de-allocation automatically by Garbage Collector of CLR. At runtime, the Common Language Runtime (CLR) compiles the code by using Just In Time (JIT) compiling. Allows restricted use of native pointers. The JIT compiler compiles a function or method only the first time and it produces machine code native to the platform on which it’s running. .NET Programming - Nguyễn Đạt Thông 71 .NET Programming - Nguyễn Đạt Thông 72 18
- 2/19/2014 Các tính năng mới Chương trình đầu tiên 2.0 Hello C# Generics, Iterators, Partial types, Anonymous method, Static classes, Property Console application accessibility control, etc. Print out “Hello C#” 3.0 How to create Implicitly Typed Local Variables, Object and Collection Initializers, Extension Create a solution and a “Console application” project named “FirstProgram” Methods, Partial Methods, Anonymous Types, Query Keywords, Lambda Expressions, etc. Open “Program.cs” source file and add following statements to the “Main” function 4.0 Console.WriteLine("Hello C#"); Named and Optional Parameters, Dynamic Support, Generic Variance, COM Console.ReadLine(); Interoperability. .NET Programming - Nguyễn Đạt Thông 73 .NET Programming - Nguyễn Đạt Thông 74 Thực thi Hàm main Set “FirstProgram” as StartUp project The Main method is the entry point of an .exe program; it is where the program control starts and ends. Build and Run it Main is declared inside a class or struct. using System; using System.Collections.Generic; using System.Linq; using System.Text; Main must be static and it should not be public. namespace FirstProgram { class Program { Type: static static void Main(string[] args) { Console.WriteLine("Hello C#"); Return type: void or int Console.ReadLine(); } Parameters: none or string[] } } .NET Programming - Nguyễn Đạt Thông 75 .NET Programming - Nguyễn Đạt Thông 76 19
- 2/19/2014 Hàm main: Ví dụ Chương trình thứ hai Return type is int Change Main function to static int Main() { static void Main(string[] args) { // do something Console.WriteLine("Hello {0} from C#", args[0]); return 0; Console.ReadLine(); } } Manipulate arguments Add command line arguments for debugging Select project “FirstProgram”, static void Main(string[] args) { // Display the number of command line arguments Open “FirstProgram Properties…” from Project menu, // args DO NOT contains the name of the program System.Console.WriteLine(args.Length); Open “Debug” tab in Properties page } Add “World” to “Command line arguments” .NET Programming - Nguyễn Đạt Thông 77 .NET Programming - Nguyễn Đạt Thông 78 Thay đổi thuộc tính của project Thực thi chương trình thứ hai using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FirstProgram { class Program { static int Main(string[] args) { Console.WriteLine("Hello {0} from C#", args[0]); Console.ReadLine(); return 0; } } } .NET Programming - Nguyễn Đạt Thông 79 .NET Programming - Nguyễn Đạt Thông 80 20
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
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