2. C # CORE Programming structure

Source: Internet
Author: User

  This study mainly refers to Andrew Troelsen's C # and. NET4 advanced Programming, this section mainly tells the following things: Hello World's Main method: Using VS2010 to create a new console application Hello World, The perfect code is as follows: Class and Main method: Here the system automatically creates a class called program, and there is a default main method. In this case, the class name of our program can be arbitrarily modified, but the main () method must be unique, and running after the modification will be an error. Because the main () method is the entry for our application. The error is as follows: The Main method is to reference the entry of the program, and the processing logic is inside the main method. By default, we generate a static, void (empty) return value, which takes the form of an array of parameter strings. However, this is not the only form of the main method, besides, we also have the following signature forms. If the defined entry function is not a null return value, then we have to provide a return value at the end of the program. (return value 0 indicates normal end): Static int Main (string[] args) {   //processing logic     return 0;} No parameter no return value static void Main () {}//No parameter has return value static int Main () {    return 0;} View parameters, which can be implemented by the members of the System.Environment class. (Environment.getcommandlineargs ()), the function returns a string array that can be used to iterate through the returned array to get the arguments. System.Console class, the class mainly has the following three functions: 1, the basic input and output. As the above code WriteLine method means that the output is in the console, and ReadLine accepts an input. In addition, there are write and read methods, and they are similar in usage. 2. Format the console output. Can be formatted with placeholders, such as: Console.WriteLine ("{0},{1}", 123,456), where the placeholder numbers correspond to the order of the output parameters, starting from 0. and placeholders can be in no particular order. 3. Format numeric data. Similar to the C language and C + + inside the%d,%x representation of the output number and hexadecimal format, which can be used d/d and x/x. Usage: Console.WriteLine ("C Format:{0:c}", 9999); 4. Otherwise, format the numeric data outside of the console application. Using string. Format () method for formatting, usage: string usernum = string. Format ("100000 in Hex is {0:x}", 100000);  system data type and C # simplified notation: Like any programming language, C # defines a set of basic data types that represent local variables, member variables, return values, and input parameters. such as bool, Byte, short, int, long, and so on. However, unlike other programming languages, these keywords are not simply tokens that the compiler can recognize, but the C # data type keyword is actually a simplified symbol of the complete type in the System namespace, such as ulong is shorthand for system.uint64. Variable declaration and initialization: int myInt; string myString; Initialization can be directly assigned int myint=0, or so, int a=1,b=2,c=a; built-in data type and new operator: all built-in data types support default constructors, which can be created with new open. such as the bool type, can also be expressed as: BOOL b=new bool (); The hierarchy of the data type class: the. NET basic data type has a class hierarchy, and the type at the top of the tired hierarchy provides some default behavior for the derived type. The relationships between these core types are as shown in:     numeric data type members: We're going to continue to study C # 's data types, You must know that. NET numeric types support the MaxValue and MinValue properties, which illustrate the range that a given type can store. usage, int. Maxvalue,double. MaxValue and so on. System.Boolean member: It does not support MinValue and MaxValue because he can only originate from the collection {True|false}. However, the TrueString and FalseString property collections are supported (return TRUE or FALSE, respectively). System.Char Members: The text data for C # is represented by the string and char keywords, both of which are Unicode-based. string represents a continuous character, and Char represents a single character. Parses a numeric value from the string data (. Parse () method): This technique is useful when you want to convert the data entered by the user into a numeric value. Use int i=int. Parse ("8"); System.DateTime and System.TimeSpan:DateTime represent DateTime, while a TimeSpan allows you to easily define and convert time units using individual members. NET 4 system.numErics namespace: It defines a BigInteger structure that represents a large numeric value with no fixed upper and lower bounds.   Using String data: Basic string manipulation: string. Length gets the string length, string. ToUpper gets the string into uppercase value, string. ToLower gets the string into a lowercase value. string concatenation: String. Concat (), but we can also use the + number to indicate stitching. The compilation results are the same for both. Escape characters: Like C, they are backslashes \, which can be expressed in the original expression of special characters in C #. Defines a verbatim string: C # introduces a string literal that is prefixed with @, which is called a verbatim string. The string that uses it does not use escape characters. String and equality: Use the Equals method of string or the inline equality operator (= =, etc.) for equality operations, which means case-sensitive, per-character equality operations, although the string type is a reference type. The string is immutable: a string-type method, imposing a new string object in a modified format, the original string does not change. Therefore, if the string class is abused, it becomes inefficient and causes code bloat, especially when strings are stitched together. (Because each action loads a new string object on the managed heap, The original will eventually be garbage collected) System.Text.StringBuilder type: This class is similar to the System.String class, but to prevent inefficiencies in the string class, StringBuilder defines a number of methods for replacing and formatting fragments. What makes it unique is that when we invoke members of this type, we are directly modifying the character data inside the object (and therefore more efficient), rather than perhaps by a copy of the data in the modified format.   Narrowing and widening data type conversions: That is, large values and small values see the operation because C # is type-safe, narrowing operations have errors, but can be narrowed by casting, but this is likely to result in data loss. The checked and unchecked keywords provided by C # will ensure that the data friar will definitely be detected. Checked usage: Used in a code like this: Byte sum=checked ((Byte) Add (B1,B2)); used on a block of statements, detection is not forced overflow:checked{    byte sum= (byte) ADD (B1,B2);    Console.WriteLine (sum);} The unchecked keyword is the same as the keyword usage of checked, except that he checks all the arithmetic logic and enters into the individualThe condition system runs out of the language of the anomaly. System.Convert: One of the benefits of using it is that it provides a language-independent way to convert data types (for example, VB types convert completely different C #).   Implicit type local variable (VAR): The C # language defines a local variable that can create an implicit type using the var keyword. You do not need to specify a specific data type (such as int, bool, string) to use the var keyword. When you do this, the compiler automatically infers the actual data type based on the initial value class of the local data point. Use the following: Var myint=0;//is defined as a shape, and Var is equivalent to Intvar mybool=true;//means that the definition is a Boolean value. where Var equals the restriction of the bool implicit type variable: First, the implicit type can only be used for local variables within a method or property scope. It is not legal to use the VAR keyword to define a return value, parameter, or field data of a custom type. Similarly, a local variable declared with the var keyword must be assigned an initial value at the time of Declaration, and the initial value cannot be null. Finally, it is illegal to define a controllable implicitly typed local variable with a C # tag. Implicit type data is strongly typed data: Implicit type variables affect the life of a variable at compile time, and then the data point is treated as a type of life, and assigning a different type to that variable will result in a compile-time error. Implicit type variable usage: will become very useful in LINQ queries.  c# iteration structure: For loops, like for loop usages in C and C + +, we can create complex termination conditions, construct an infinite loop, and use the Goto, continue, break keywords. Foreach/in loop: The C#foreach keyword allows you to iterate through all the items in the array, without having to test the array's on-line. When using Var in a foreach structure, the compiler can accurately determine the exact type of the type. While and Do/while loops: The while loop structure is useful when you want to execute a statement until a termination condition is met. Similar to a simple while loop, you can use the Do/while loop when we need to perform some number of patch actions. The difference between the two is that the Do/while loop will certainly execute at least once the corresponding block of code, while the while loop may end without execution.   Conditional structures and relationships/equality operators: If/else statements: If/else statements in C # can only be scoped Boolean expressions and cannot be used for values such as-1, 0, so if/else statements typically contain some C # operators. (= =,!) =, <, >, <=, >=, and logical operators &&, | |,! )。 Description:&& and | | ExerciseThe characters are ' shorted ' when necessary, that is, if an expression is determined to be false, the other expressions are not checked. Switch statement: As in the C-series language, the switch statement allows us to handle the program flow according to the defined selection. C # requires that each case (containing default) contain executable statements terminated with a break or goto to avoid failure.   Summary: This section examines some of the basic constructs of each C # executable and discusses some of the details of the built-in data types in C #. Also introduced are the Var implicit keywords and C # supported conditions and iteration statements. In the next section we will be doing research on the characteristics of the new language and

2, C # Core programming structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.