I have seen a lot of summaries on the Internet over the past two days, especially in the blog parks. I have also learned a lot about it and I have also summarized it myself, in addition, many people also give me some comments and opinions. In any case, I should consolidate the so-called basic knowledge first.
The first blog in 2014, hoping to start from the basics. I also hope that I can learn and integrate it systematically. Because I have never studied it in the system before, I used it all, and I forgot about it first. So I will record it here for future reference. Well, I don't have to talk nonsense. Let's get started.
1. Classes and objects
Class Definition: an abstract set of objects with the same attributes and functions. -- A flash of brain,Is a class actually a set?
Class instance: instantiation is the process of creating an object. It is created using the new keyword.
Let's take a look at the following code:
Name { ; Main(=
Cat cat = new Cat (); in fact, it does two things. You don't know, you know, no, I know it now (I must have known it before, but it is already a thousand miles away ):
2. goto: The statement directly passes program control to the mark statement.
Goto seems to have never been used in development.
Main( i = Console.WriteLine(++ (i < repeat;
This is a simple way to jump to the mark repeat when conditions are met;
Output result:
Here is a detailed introduction to goto, including its advantages and disadvantages and application scenarios.
Http://www.cnblogs.com/IT-Bear/archive/2012/03/05/2380389.html
3. In the programming Syntax of "continue", "break", "return: C #", we often encounter "break", "continue", and "return ".
The break statement causes the running program to immediately exit the loop contained in the innermost layer or exit a switch statement. Because it is used to exit a loop or switch statement, this form of break statement is valid only when it appears in these statements.
If the termination condition of a loop is very complex, it is much easier to use the break statement to implement certain conditions than to use a loop expression to express all the conditions.
( i = ; i <= ; i++ (i == )
The continue statement is similar to the break statement. The difference is that it is not to exit a loop, but to start a new iteration of the loop.
The continue statement can only be used in the loop body of the while statement, do/while statement, for statement, or for/in statement. It may cause errors when used elsewhere!
( i = ; i <= ; i++ (i == )
The return statement is used to specify the value returned by the function. The return statement can only appear in the function body. syntax errors may occur anywhere else in the code!
When a return statement is executed, function execution stops even if there are other statements in the function body!
4. I ++ and ++ I. The former is first followed by the latter, and the latter is first followed by the latter.
This is awkward when I was studying at school. Now I haven't looked at it for a long time and it's blurred. This may also be a problem many companies are willing to make during the interview.
Main( valueOne = = valueOne++= = ++
Let's look at a simple example:
k = i = = ++i + (i++, k, i);
First, if I = 3; return I ++; then 3 is returned; if return ++ I; then 4 is returned.
So ++ I is now I = 4; the expression is changed to 4 + (4 + +), while 4 + + is used first, so it is 8 + +, in the same way, 8 + + returns 8, I +, and I is 5.
Of course, there may be more than one solution. If you are interested, you can discuss it together.
5. The case in the switch can be written in this way.
Main( Democrat = LiberalRepublican = Republican = myChoice =
Case LiberalRepublican:
Case Republican:
Both of these conditions are met:
Console.WriteLine();
6. The ternary operator is equivalent to if else.
Main( valueOne = valueTwo = maxValue = valueOne > valueTwo ?
maxValue = valueOne > valueTwo ? valueOne : valueTwo;
This sentence is equivalent to an if else
(valueOne >==
7. ref and out: both are passed by address. after use, the values of the original parameters are changed.
Rel can pass the value of the parameter to the function, but the out parameter must be cleared, that is, you cannot pass a value from out. After the out parameter is passed, the value of the parameter is null, so you must initialize it once. This is the difference between the two, or, as some netizens say, rel is inbound and outbound, while out is outbound.
Let's take a look.Ref: The effect is that when the control is passed back to the call method, any changes to the parameters in the method will be reflected in this variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods.
Example:
Method( = Main( val =
Parameters passed to the ref parameter must be initialized first. This is different from out. The latter parameter does not need to be explicitly initialized before being passed.
Let's take a look.Out: Out keyword causes the parameter to be passed through reference. This is similar to the ref keyword, except that the ref requires that the variables must be initialized before being passed. To use the out parameter, the out keyword must be explicitly used for method definition and call.
Example:
Method( = Main(
Although the variables passed as the out parameter do not need to be initialized before being passed, you need to call a method to assign values before the method returns.
The ref and out keywords are processed in different ways at runtime, but are processed in the same way at compilation. Therefore, if one method uses the ref parameter while the other method uses the out parameter, the two methods cannot be reloaded. For example, from the compilation perspective, the two methods in the following code are identical, so the following code is not compiled:
However, if one method uses the ref or out parameter, and the other method does not use the two parameters, you can perform the overload operation, as shown below:
SampleMethod( SampleMethod( i) { }
The two methods can be compiled successfully.
8,String and string, which have never been distinguished before, but pay attention to them.
In C #, string is the alias of System. String, so there is basically no difference in use. Traditionally, when we treat a string as an object (Object with values), we use a string.
When we use it as a class (the method defined in the String class is required), we use string, for example: String greet = String. Format ("Hello {0 }! ", Place ).
In fact, it is also possible to use it indiscriminately, but this is a clear concept.
In additionString is a reserved character of C #. It cannot be used as a variable name. String is not. The String must first reference the System namespace.
It can also be understood as follows: string is the reflection of String type in C #. It is a simplified writing method, just like int corresponding to Int32. The two can be used in C.
ThenString is a class, and string is a data type. String is the class in c #, and String is the class of. net Framework (blue is not displayed in c # IDE)
C # string ing is the String of. net Framework. If a string is used, the compiler will compile it into a String. Therefore, if a String is used directly, the compiler will do less work.
9. Heavy-duty operators
Let's take a look at a few simple lines of code:
Main( x = y = sum = x +
An int sum = x + y; addition operation.
A little encapsulation:
Main( x = y = sum = Add( x, x +
If we need to know the sum of one attribute of two classes, we may look like this:
Name { ; Age { ; Person( name, .Name =.Age = Main(= Person(, = Person(, sum = Add( x, x +
Let's change it again:
Main(= Person(, = Person(, sum = p1 + Name { ; Age { ; Person( name, .Name =.Age = + p1.Age+
I feel like my foundation is a bit messy. Make up for it. In this way, I have never been clear about many basic things. I am here to make a good summary, mainly for your own review and consolidation, but also hope to have a role for some people. Of course, there may be many problems in my arrangement. You are welcome to correct me.