[The Path to commercial software development for graduates] C # advanced suggestions

Source: Internet
Author: User

Recently, I started to contact students, college interns and graduates. Here I will talk about some suggestions for students who have been in the workplace and have been wandering between Niu A and Niu C but have not found Niu B, it is hoped that these beginners can enter the software development industry to bring some help, so that graduates can more smoothly enter the software development company to start a career, life to a perfect turn.

---------------------------------------------------------

C # advanced recommendations

From VB to C #I have changed from VB6.0 to C #. Here I suggest converting VB to C #, hoping to help those who have mastered VB. The C # syntax adopts the C language style, and its design references the idea of VB. The main points of the transformation are as follows: ● VB variables, methods, and type names are case-insensitive, while C # is case-sensitive. ● VB statements are determined by line breaks, while C # uses double quotation marks (;) to separate syntax units. ● When VB makes a compound condition judgment, all the condition items will execute the judgment, while C # will execute the judgment in sequence. If the condition is determined, the judgment will be terminated immediately. For example, if the statement "if value1 <> 0 or value2 = 2 then" is executed in VB, if "value1 <> 0" is determined to be true, it is reasonable to say that no matter whether the second condition is true, this composite condition is true. However, in VB, the program calculates whether "valu2 = 2" is true. When all the condition items are determined, the system determines whether the composite condition is true. In C #, execute the equivalent statement "if (value! = 0 or value2 = 2) ". If" value1! "is determined! If it is set to 0, the program determines that the compound judgment condition is true and does not calculate the "value2 = 2" judgment condition. From this process, we can see that C # is more efficient than VB in executing compound condition judgment. ● Foreach statement. C # supports the foreach statement in VB. The syntax in VB is "foreach variable name in list variable name", and C # is "foreach (type name variable name in list variable name) ". For example, if there is an integer array, the way to traverse the Data Group in VB is
 
 
  1. Dim array(10) as Integer 
  2. Dim item as Integer 
  3. For Each item in Array  
  4.    
  5. Next  
C # is written as follows:
 
 
  1. int[] array = new int[10];  
  2. foreach( int item in array )  
  3. {  
  4. }  
● Array subscript. The array subscript of VB is calculated from 1 by default, while C # is calculated from 0. The UBound function of VB returns the maximum value of the available lower mark, the Length or Count attribute of the C # array object returns the number of elements in the array. At this time, the maximum value of the available sub-mark is less than 1. For example, when VB uses the statement "Dim Array (10) As Integer" to define an Array, the number of elements is 10, the minimum subscript is 1, and the maximum subscript is 10. In C #, an Array defined by the statement "int [] Array = new int [10]" is used. The number of elements is 10, the minimum subscript is 0, and the maximum subscript is 9, the value of Length is 10. In VB, square brackets are used on both sides of the array marker, but square brackets are used in C. For example, the VB code "Array (9)" is to obtain the value of the first element in the Array, and in C #, it must be written as "Array [9]". ● Parameter transmission mode. C # and VB are differentiated between passing by value and passing by reference when passing function parameters. VB's function parameter values are passed by reference by default, while C #'s function parameter values are passed by value by default. For example, a Function defined by the VB statement "Function F1 (arg as Integer)" is equivalent to "Function F1 (ByRef arg as Integer)". When the parameter value is modified inside the Function body, this value also changes after the function is jumped out.
 
 
  1. Function F1( arg as Integer )  
  2.    arg = 10  
  3. end Function  
In this case, the statement "Function F1 (arg as Integer)" is equivalent to "Function F1 (byRef arg as Integer)". If the following VB code is executed
 
 
  1. Dim Val as Integer
  2. Val = 20
  3. F1 (Val)
  4. 'Here, the Val variable value is changed to 10 in the function body.
In VB, passing by reference by default leaves an error that is hard to detect. Therefore, when writing VB code, the parameter passing method of ByVal or ByRef is explicitly written. However, in C #, values are passed by default. For example, for the following C # code
 
 
  1. void F1( int arg )  
  2. {  
  3.     arg = 10 ;  
  4. }  
If the following C # code is executed
 
 
  1. Int Val = 20
  2. F1 (Val)
  3. // Execute here. The Val variable value is still 20.
In C #, to pass by reference, you need to use the keyword "ref" to modify the parameter, that is, to write code similar to "void F1 (ref int arg. In this way, the parameter value is modified inside the function body, and the modified parameter value is obtained outside the function. C # provides the "out" keyword to pass parameters in a more refined way, that is, code similar to "void F1 (out int arg. The keyword "out" indicates that the parameter is passed by reference, and the parameter value must be modified inside the function body; otherwise, the compilation is incorrect. This is also an abnormal function return value. For class types, both VB and C # are passed by reference. ● Function call method. The VB call method is to directly write the Function name, followed by the parameter, and the parameters are separated without parentheses. For example, the Function "Function F1 (byval a as Integer, byval B as Integer)" is defined )", the code that calls this function is usually "F1 10, 20". In a few cases, you can also write it as "Call F1 (10, 20 )". However, C # Must be enclosed in parentheses on both sides of the parameter. Therefore, it must be written as "F1 (10, 20 )". ● Exception handling. In VB, you can use "on error goto" or "on error resume" to handle exceptions. You can also use the "resume" statement to jump back to the Code where an exception occurs. However, C # does not support code redirection similar to the "on error resume" statement. [Yuan Yongfu copyright] From C/C ++ to C #I also have a little understanding of the C/C ++ language. Therefore, I would like to provide some reference suggestions for the transformation from the C/C ++ language to the C # language, hope to help those who have mastered the C/C ++ language. The main points are as follows: ● pointer. The most powerful function of C is pointer. C # also supports pointers, but it is no longer recommended, so that pointers become very advanced C # syntax. To use pointers in C #, you must first use the syntax structure "unsafe {}" to create a code block in which pointers can be used. When defining pointer variables, "*" in C # Must be used together with the basic type; otherwise, compilation errors, such as the code "int * p1, p2, p3 "defines three pointer variables pointing to the integer type in C #, and the code" int * p1, * p2, * p3; in C #, a compilation error occurs, which is correct in C. C # conversion between different pointer types or between pointer types and integers is not allowed, but only in C language. For example, code
 
 
  1. int* p1;  
  2. char*p2;  
  3. p1 = p2;  
This code is wrong in C #, but in C language. ● Security. C # provides stricter security than C/C ++. For example, C/C ++ does not check whether the array subscript is out of bounds, while C # Strictly checks .. The. NET Framework provides a trusted runtime environment where all programs running are restricted, including C #. ● Conditional compilation. C # supports Conditional compilation similar to the C language, and supports commands such as "# define", "# undefine", and "# if. However, the C-style macros, including macro constants and macros with parameters, are not supported. ● Access type members. In C ++, "->" is used to access type members, while in C #, "." is directly used to access type members. [Yuan Yongfu copyright] ● function. In C/C ++, functions can be compiled independently, not of any type, such as the Main function. But there is no free function in C #, and all functions must belong to a certain type, including the Main function. ● String definition. In C/C ++, double quotation marks are used to define a string, and the slash character "\" is used for escape. C # also supports, however, it also provides a method to define multi-line strings, that is, adding the "@" symbol before the double quotation marks. For example, the following C # Code defines a multiline string.
 
 
  1. string myStr = @“abc  
  2. 1234 \t  
  3. 5678”;  
When C # defines a multi-line string, the slash escape is invalid. In particular, in traditional C/C ++, the string uses "\ 0" as the end sign of the string, in C #, the string object can contain "\ 0", which requires attention when passing string data between the two. ● Inheritance. In C ++, a class can inherit multiple other classes and implement multiple interfaces. in C #, a class can only inherit one other class, you can also implement multiple interfaces. ● Note. C/C ++ supports the double slash character "//" to define a single line comment, and uses the slash and star number to "/**/" to implement multi-line comments. C # also supports this method. However, C # goes further. Before a member or function, use the three consecutive slash "//" and the specific XML syntax to implement docized comments, C # the compiler can extract docized comments from the code and automatically generate relevant SDK help documents. [Yuan Yongfu copyright] From JAVA to C #I also have a little understanding of JAVA syntax, but I really just have a little understanding. Now, I have a personal understanding of the JAVA syntax to propose a transfer from JAVA to C #, hoping to help readers who have mastered the JAVA syntax. ● The JAVA and C # syntaxes are indeed very similar, and the code structure is similar. Both parties have corresponding keywords. ● C # supports delegation, but JAVA does not. The delegate in C # can be said to be the OOP version of the function pointer in C language. It is a variable that can store the entry point address of a function. Therefore, the delegate can enhance the flexibility of C, JAVA does not support this feature. ● C # supports events, but JAVA does not. C # events are supported by delegation. You can bind multiple delegates to an event to support broadcast. In addition, JAVA needs to simulate the event mechanism by implementing interfaces, this operation is not convenient. As some readers have mastered the technology of other programming languages, we provide some advanced C # suggestions for these readers, hoping to help these readers better master the C # language. [Yuan Yongfu copyright]

This article from the "Nanjing yuan Yongfu" blog, please be sure to keep this source http://xdesigner.blog.51cto.com/3148541/636403

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.