Chapter One overview of C # programming
Learning goals in this chapter:
- The main features of the C # language
- Integrated development environment for Visual Studio 2008/2017
- C # Console application and Windows Forms application design steps
- Basic writing rules and application Architecture for C # program code
1.1c# Language Introduction
C # is a simple, modern, universal, object-oriented programming language, which is a core programming language built into the Microsoft. NET common language runtime environment. Using the C # language, you can develop a variety of applications that run on the. NET framework, including: Console applications, Windows Forms applications, Web applications, and Web services.
first Write a basic example of a C # program:
usingSystem;namespacechapter_one{/*class name is HelloWorld*/ classHelloWOrld {/*Main function*/ Static voidMain (string[] args) { /*my first C # HelloWorld program*/Console.WriteLine ("Hello World"); Console.ReadLine (); } }}
The main features of 1.1.1c# language
The C # language is a new programming tool developed by Microsoft for the. NET environment that combines the power of the C + + language, the object-oriented nature of the Java language, and the ease of use of the Visual Basic language. NET environment of computer application provides a powerful, new, easy-to-use programming tools.
The main features of the C # language are as follows:
- Simple syntax
- Meeting Common Language Specifications
- Object oriented
- Complete security and error handling
- Compatibility and flexibility
- Version control
- with the The perfect combination of web
Powerful programming features in the C # language:
- Boolean condition (Boolean Conditions)
- Automatic garbage collection (Automatic garbage Collection)
- Standard library (Standard Library)
- Component version
- Properties (Properties) and event (events)
- Delegate (Delegates)
- Indexer (indexers)
- conditional Compilation (Conditional Compilation)
- Simple Multi-Threading (multithreading)
- LINQ and Lambda expressions
- Integrated Windows
Comparison of 1.1.2c# and other programming languages
C # is a new programming language built on the merits of programming languages such as C + + and Java, and lists the main similarities and differences between C # and C + + and Java.
Compare content |
C # programming language |
C + + programming language |
Language processing methods |
Compiled into intermediate language (MSIL) code, which is executed by a JIT (timely processor) to convert intermediate code to native code execution |
Directly compiled into code that can be executed natively |
Pointer data |
The default data, instead of using pointer data, uses a reference to the class instance. You can also use pointers in "unmanaged" mode |
Use pointer data extensively (especially in parameters of functions) |
Library |
Depend on. NET base class |
relies on a standard library based on inheritance and templates |
Inherited |
Single inheritance only, multiple inheritance through interfaces |
C + + allows multiple inheritance |
Memory management |
Use a garbage collection mechanism to automatically reclaim memory space that is no longer used at the right time |
You must explicitly delete (reclaim) the dynamically allocated storage space in your program |
String processing |
Strings are basic data types and provide a variety of ways to handle strings |
String processing based on array processing method |
Note : In the common language specification, source programs written in any language are compiled into the same intermediate language (MSIL) code, which is then executed by the common language runtime environment (Common Lanuage RUNTIME,CLR), which is called " Managed by ", so the code is also called" managed code . "
Compare content |
C # programming language |
Java programming language |
Language processing and how programs run |
C # code is compiled into intermediate language (MSIL) code and then runs in the common language runtime (CLR), and the CLR's JIT compiler compiles the intermediate code into local code execution |
Java code is compiled into an intermediate code that executes by running the Java Virtual machine provided by the environment JRE |
language function differences |
The source program file name is not limited by the class name, providing operator overloading, boxing and unpacking, method hiding and other functions |
The source program file name is limited by the class name |
The C # language writes the source program file processing as shown in:
Application Programming example of 1.2c# console
Control Desk (Console) application is one of the types of applications developed using the C # language, and because it does not involve elements of the Windows system, the structure is relatively simple
1.2.1c# the creation of a console application
in the The main steps for creating a C # Console application in Microsoft Visual Studio 2008/2017 (for short vs 2008/2017) are as follows:
(1) start "Microsoft Visual Studio"to enter the VS2017 integrated Development Environment page, as follows
(2) Select the project type as the console application and specify the project name and where to save it
(3) Enter program code in the application editing environment
(5) Debug Run Program , press F5 or select " Start Debugging " in the " Debug " menu group to name , execute (Debug)
1.2.2c# the structure of the console application
in a C # program, an application can consist of one or more classes, and all program code must be encapsulated in a class. A source program file can consist of one or more classes, but does not allow a class to be torn apart in different source program files. When you name a source program file, you can have the same name as the class, or it can be different, that is, the name of the source program file is not restricted by the class name.
a C # console application consists mainly of the following components:
1. Import other System definition elements section
high-level programming languages always rely on many system -predefined elements that need to be imported in order to be able to use these predefined elements in a C # program. In the console application created above, references to other namespaces are imported using the following code snippet:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
2. Namespaces
using Namespace and namespace identifiers (namespace names) to build the user namespace, the scope of the space is delimited by a pair of parentheses, as follows:
namespace Hello // By default the namespace name is the same as the solution name {}
3. Class
class must be contained in a namespace (For example, namespace Hello), the class is constructed using the keyword class and class identifier (class name, Prograam by default), and the scope of the class is defined using a pair of curly braces, as follows:
class program{}
4. Main method
Each application has an execution entry that indicates the start point of the program execution. The entry point in a C # application is identified by the main method, whose name is main (), and the parentheses behind it cannot be omitted, even if there are no parameters.
Static void Main (string[] args) {}
5. C # code in the method
in the method body (between the left and right brackets of the method) write code that implements the logical function of the method
Static voidMain (string[] args) {Console.Write ("Please enter your name:"); stringName = Console.ReadLine ();//Enter a name string to assign to the name variableConsole.WriteLine ("Welcome"+name+"Enter C # programming"); Console.ReadLine (); /*so that program design does not automatically exit the debugging environment*/}
Basic writing rules for 1.2.3c# program code
1. Program code distinguishes letter case
C # is a case-sensitive language, with different identifiers being considered different for alphabetic case.
2. Statement writing rules
(1) Each statement must be separated by a semicolon (";") As the end
(2) C # allows multiple statements to be written on the same line of code, but from a readability standpoint, this is not advocated
(3) C # is a block-structured programming language, and all statements are part of a block of code
(4) Punctuation marks in the statement as grammatical components must be Western punctuation marks, Chinese punctuation marks can only be used as character constants
3. Comment Information
Note Information is a non-executable part of the program and is used only to describe the program code, which helps to improve the readability of the program. the annotation method in C # consists of 3 types:
(1) single-line comment. On a statement line, a double slash "//" is used as the guide, and any subsequent content is commented, and is ignored at compile time
(2) multi-line comment. start with "/*" and End with "*/" , where all the contents (can be one row, or multiple lines) are commented information, but the comment text must not contain "*/"
(3) XML annotations. On a line of code, start with "///" and anything that follows is a comment, compile-time and extract it to form a special-format text file (XML) for creating a document specification
For beginners, the first two types of annotations are often used in programming.
1.3c#windows Form Application Design Example
Windows Forms applications form an interface that communicates with the user through various graphical user interface (graphical user Interface,gui) elements on the form.
1.3.1c#windows Creating a form Application
in the Creating a c#windows form application in Visual Studio 2008 typically requires the following 4 steps:
(1) Design user Interface
(2) setting object Properties
(3) Writing Object event procedure Code
(4) Save and run the program
Structure of the 1.3.2c#windows form application
form applications consist primarily of the following programs :
(1) import Other system predefined element definitions
(2) namespaces
(3) class
(4) method (Main method, event response process)
The source code for Program.cs is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Windows.Forms;namespacemotto{Static classProgram {///<summary> ///The main entry point of the application///</summary>[STAThread]Static voidMain () {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewForm1 ()); } }}
Summary of this chapter
This chapter describes the main features of the C # language and introduces the development of C # Console applications and Windows Forms applications, their composition, and the basic writing rules for C # program code through two simple examples.
Exercise :
(1) Overview of the main steps to create a C # console application in the VS 2017 integrated environment
(2) What are the main components of a C # console application?
(3) What is the escrow method ?
Overview of C # Programming in the first class