[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

Some readers have mastered otherProgramming LanguageTo provide some C #Advanced suggestions, hoping to help these readers better master C #Language.

From VB To C #

From VB6.0Transition to C #And some VBTransition to C #In the hope thatTo help readers.

C #CLanguage Style, and its design has a lot of reference to VB. The main points of the transformation are as follows:

● VBThe names of variables, methods, and types are case-insensitive, while C #Is case sensitive.

● VBThe statement is determined by line breaks, while C #Double quotation marks are used.To separate the syntax unit.

● VBWhen determining compound conditions, all condition items will execute the judgment, while C #It is executed in sequence. If it is determined, the judgment is terminated immediately.

For exampleIf value1 <> 0 or value2 = 2 then", If" value1 <> 0"After the establishment, it is reasonable to say that whether or not the second condition is true, this compound condition is true, but inVBMediumProgramStill calculate "valu2 = 2""Whether the condition is true. After all the condition items are determined, the system determines whether the entire compound condition is true.

In C #Execute the equivalent statement "If (value! = 0 or value2 = 2)". If" value1! "is determined! = 0"Then, the program determines that the compound judgment condition is true and does not calculate"Value2 = 2".

From this process, we can see that C #Compared with VB when performing compound condition judgmentHigh efficiency.

● ForeachStatement.

C #Support for VBMedium foreachStatement, VBThe statement in is "foreachVariable name inList variable name ", while C #Is written as "foreach (Type nameVariable name inList variable name)".

For example, there is an integer array in VBIs written

Dim Array ( 10 ) As   Integer
Dim Item As   Integer
For   Each Item In Array

Next

C #The statement is as follows:

Int [] Array =   New   Int [ 10 ];
Foreach ( Int Item In Array)

{
}

● Array subscript.

VBThe array subscript of is from 1 by default.Start computing, while C #Is from 0Start to calculate; VbUboundThe function returns the maximum value of the available lower mark, while C #Length of the array objectOr countThe attribute returns the number of elements in the array. At this time, the maximum value of the available sub-mark is minus 1..

For exampleUse the statement "dim array (10) as integer"Defines an array, and its number of elements is 10.The minimum subscript is 1.The maximum subscript is 10..

In C #Use the statement "int [] array = new int [10]The number of elements in the array is 10.The minimum subscript is 0.The maximum subscript is 9.And its lengthThe property value is 10..

In VBFor the array marker, the two sides of the tag value are Parentheses, but in C #Square brackets are used. For example, VBCode"Array (9)"Is to get 9th in the arrayElement, while in C #Must be written as "array [9]".

● Parameter transmission mode.

C #And VBEach function parameter is passed by value and by reference. VBBy default, the function parameter values are passed by reference, while C #By default, the function parameter values are passed by value.

For exampleStatement "function F1 (ARG as integer)"Defined function, equivalent to" function F1 (byref Arg as integer)", When the parameter value is modified inside the function body, the value also changes after the function jumps out.

Function F1 (ARG As   Integer )
ARG =   10
End Function

In this case, the statement "function F1 (ARG as integer)"Is equivalent to" function F1 (byref Arg as integer)", If the following VBCode

Dim   Val   As   Integer
Val   =   20
F1 ( Val )
'Here, the Val variable value is changed to 10 in the function body.

BecauseBy default, passing by reference is easy to cause imperceptible errors. ThereforeByval is explicitly written in the code.Or byref.

But in C #By default, it is passed by value. For example, for the following C #Code

Void F1 ( Int Arg)
{
ARG =   10 ;
}

If the following C #Code

Int Val =   20
F1 (VAL)
// Run the command here. The Val variable value is still 20.

In C #To pass by reference, you need to use the keyword "refTo modify the parameters, that is, it is written as "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 "out""Keyword to provide a more refined way to pass the parameter, that is, it is written like" Void F1 (Out int Arg). Marked "out"The keyword 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 the class type, VBAnd C #All are passed by reference.

●Function call method.

VBThe calling method is to directly write the function name, followed by the parameter, and the parameters are not separated by 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"Call F1 (10, 20)". But for C #The parameter must be enclosed in square brackets, so it must be written as "F1 (10, 20)".

● Exception handling.

VBYou can use "on error GOTO"Or" on error resume"To handle exceptions, and supports the use of" resume"Statement to jump back to the abnormal code. But C #"On Error resume" is not supported."Statement, and does not support" resume.

From C/C ++ To C #

For C/C ++The language is also slightly understandable, so here we propose someLanguage to C #Some suggestions on the language, hoping to understand the C/C ++Readers of the language provide some help. The main points are as follows:

● Pointer.

CThe most powerful function of a language is pointer. C #Pointers are also supported, but they are no longer recommended, so that pointers become very advanced C #Syntax.

In C #To use pointers in a language, you must first use the syntax structure "unsafe {}Create a code block in which pointers can be used.

C #"*"Must be used together with the basic type, otherwise the compilation is incorrect, such as the code" int * P1, P2, P3"InC #3 is defined inAnd the code "int * P1, * P2, * P3;"In C #In the CThe language is correct.

C #Conversion between different pointer types or between pointer types and integers is not allowed.The language is only allowed. For example, code

Int * P1;
Char * P2;
P1 = P2;

This code is in C #Is wrong, but in CLanguage.

● Security.

C #ProvidesMore stringent security. For example, C/C ++Do not check whether the array subscript is out of bounds, while C #Will be strictly checked .. NetThe framework provides a trusted running environment where all the programs running on it are restricted, including C #.

● Conditional compilation.

C #Similar to CThe Conditional compilation function of the language, supporting "# define"," # Undefine"," # IfAnd other commands. But does not support CStyle macros, including macro constants and macros with parameters, do not support "# include"Command.

● Access type members.

In C ++Use "->"Access type members, while in C #Directly use a character ".Access type members.

● Function.

In C/C ++The function can be compiled independently and does not belong to any type, such as mainFunction. But in C #There are no free functions. All functions must belong to a certain type, including mainFunction.

● String definition.

In C/C ++Use double quotation marks to define a string, and use the slash character "\"Escape, C #It is also supported, but it also provides a method to define multi-line strings, that is, adding "@"Symbol. For example, C #The Code defines a multi-line string.

String Mystr = @ "ABC
1234 \ T
5678 ";

In C #When defining a multi-line string, the slash escape is invalid.

Especially in the traditional C/C ++The string is "\ 0""As the end sign of the string, while in C #The string object in can contain "\ 0", This must be noted when passing string data between the two.

● Inheritance.

In C ++A class can inherit from multiple other classes and implement multiple interfaces #One class can inherit only one other class, and multiple interfaces can be implemented.

●Annotations.

C/C ++Supports double-slash characters "//"Defines a single line comment, using a slash and a star pair"/**/"Implemented multi-line comments, C #This method is also supported.

But C #Furthermore, use three consecutive slashes before a member or function "//"And specific XMLSyntax implementation docized comments, C #The compiler can extract documented comments from the code and automatically generate related sdks.Help documentation.

From Java To C #

For JavaThe syntax is also a little understandable. NowA little understanding of the syntax to propose from JavaGo to C #In the hope that you have mastered the JavaSyntax readers are helpful.

● JavaAnd C #The syntax is indeed very similar, and the code structure is similar. Both parties have corresponding keywords.

● C #Support delegation, while JavaNot supported. C #Can be said to be CLanguage function pointer OOPVersion, which is a variable that can store the entry point address of a function. Therefore, the delegate can enhance C #Language flexibility, while JavaThis feature is not supported.

● C # events are supported, but Java is not supported. C # events are supported by delegation. Multiple delegates can be bound to one event to support broadcast; in addition, Java the event mechanism needs to be simulated by implementing interfaces, which is not convenient.

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.