Java attack C # -- the basis of syntax,

Source: Internet
Author: User

Java attack C # -- the basis of syntax,

Summary of this Chapter

The previous chapter describes common knowledge points for project development. This chapter officially introduces the basic syntax of C. We all know that C # is also an object-oriented computer language. In addition, the similarity between JAVA and JAVA is as high as 80%. Therefore, many syntax points are basically the same. If you have learned JAVA, start to learn C. The biggest problem is the unfamiliar environment. Such as the development environment. At the same time, I hope readers will understand it. If you think you have learned it after reading the author's blog, I can only say that you are naive. The author's blog can only be regarded as one kind of assistance. Accelerate your understanding of C. In addition, the author will not explain C # in depth, and focus on getting started. Of course, it is undeniable that the knowledge written by the author is not necessarily correct.

Basic syntax Data Type

Data type. That is, the so-called variable. This is the foundation. Let's take a look at some common data types in C.

C #:

Int intValue = 1; // same as JAVA. In C #, int is divided into int32, int16, and int64. Int64 is like long longValue = 2l; // It is the same as JAVA. Float floatValue = 1f; // same as JAVA. Double doubleValue = 2d; // same as JAVA. String stringValue = "aomi"; // C # contains two types of strings: string and String (note that the first letter is capitalized ). It is commonly written in the string type. String is the object type corresponding to string. JAVA only has the String type. Char charValue = 'C'; // same as JAVA. Bool boolValue = false; // It is the same as JAVA's boolean. Only ean is missing. C # also has the Boolean object type corresponding to bool. Decimal decimalValue = 3.4 m; // unique to C. It is also a floating point number. It is generally used for money-related businesses. It is the same as float and double. This is just the end of M. Int? IntMark = null; // the data type that can be empty. C # is unique. In JAVA, I have never seen var varInt = 1; // A LITTLE BIT is as child as a weakening type. The compiler determines the type of the current variable based on the context. Var varString = "aomi" // same as above

The above is the definition of common data types during development. We can see that. You only need to remember several differences to learn the knowledge points here.

1. the decimal type is different: As long as you understand that the use of decimal is similar to that of double (if you want to deepen it, it is different ).

2. Different string types: the JAVA header letters are uppercase letters. C # Is lowercase. Note the comparison of the string type. C # uses the = and Equals methods. Both of them compare values (of course, non-strings are compared references ). But JAVA is different. You understand this.

3. Different bool types: ean is missing. The usage is the same.

4. Keyword of var: one word is as child as the weakening type. The compiler determines the type of the current variable based on the context.

5. Data Type? : Can be empty data type. The default value of the INT type is 0. It cannot be null. It cannot be null. When? That is different.

After the definition of the data type is clear, you need to understand the data type and then convert it. C # There are three most common conversion methods. As follows:

C #:

int parseVlaue = Int32.Parse("123");int convertVlaue = Convert.ToInt32("123");int value = (int) 4.5d;

The above Code provides three common types of conversion methods. I believe JAVA also often sees the Parse method. For example, Integer. parseInt. I will not talk much about it. The method in the middle is the most common method in C. This is what I did not see in JAVA. The Convert class is a tool class for conversion. It contains many conversion methods. How to Learn? Write about Convert. At this time, the development tool prompts various methods. Can this be learned? Or press the shortcut key Ctrl + J. The prompt form is as follows:

The above conversions are Explicit conversions, about Implicit conversions. I will not mention it much. Another point worth noting: 1.C# there is a common object with the ToString method. Sometimes this method will help you convert the corresponding string.

Basic syntax judgment and cyclic statements

After the data type ends, the corresponding judgment and loop statements are used. Let's take a look at the usage of the judgment statement.

C #:

Var ifIntValue = 123; if (ifIntValue> 100) {// logic code} else if (ifIntValue <10) {// logic code} else {// logic code} string aomi = "aomi"; switch (aomi) {case "aomi": // logic code break; default: // logic code break ;}

It is clear that the judgment statement has no difference. If it is hard to say. I found a strange result: the above default: The break; C # In it cannot be deleted. An error occurs when you delete the file. JAVA.

After learning about the judgment statement, let's take a look at the loop statement. Similar to the code

Int j = 0;

While (j <10) {// logic code} do {} while (j <10); for (int I = 0; I <10; I ++) {// logical code} string [] values = {"aa", "bb"}; foreach (string v in values) // equivalent to JAVA for (variable type variable name: array variable); {// logic code}

We can see the last difference: JAVA uses for (variable type variable name: array variable), and C # Is foreach (variable type variable name in array variable ). Note the nuances of in. In addition, the use of break and continue in the loop is not the same.

Arrays and sets of basic syntax

We all know the concept of array. The instantiation form is the same as that of JAVA or C. There are three common examples of Instantiation:

1. instantiation without an initial value.

 int [] array = new  int[4];

2. instantiation has an initial value.

int [] list = new int[4]{1,2,3,4};

The above code is used in C. But JAVA does not. The value 4 in [4] must be removed from JAVA to pass. Modify [4] to []. But in C #, you can pass all values not going to the value 4.

3. Anonymously instantiate an array. Both sides can pass through. This is something I did not think.

String[] values = { "aa", "bb" };

Collection classes can be said to be the most common part in programming. C # collection classes are all in the System. Collections namespace. Most of them are implemented in three interface classes: IList interface, ICollection interface, IEnumerable interface, and IDictionary interface. I like to divide collection classes into two types: one is set and the other is dictionary.

IEnumerable interface: in other words, this interface is related to foreach. The interface to be implemented when the set is iterated. I have never known how to describe this interface. Similar to the Iterator interface of JAVA.

ICollection interface: obtain the number of collection classes and copy an array type.

IList interface: similar to the JAVA List interface function.

IDictionary interface: similar to JAVA's Map interface function.

C # common examples are as follows:

IList strList = new ArrayList();ArrayList strList = new ArrayList();

The ArrayList class is a common class of the Collection class. Obtain an element in the set. C # This is slightly different.

IList strList = new ArrayList();object obj = strList[0];

The strList [0] Code is equivalent to JAVA's strList. get (0 ). What about deletion?

IList strList = new ArrayList();strList.RemoveAt(0);

The above is a type of deletion. Another way is to upload the corresponding values of a set for deletion. Finally, let's take a look at the increase.

int aaa = 1;IList strList = new ArrayList();strList.Add(aaa);

As mentioned above, I like to divide the collection classes into two types. The above section is the collection section. Next, let's take a look at the dictionary section. I believe everyone knows that a KEY has a corresponding VALUE. That is, key-value pairs.

IDictionary dic = new Hashtable();dic.Add("aomi", 123);

The above is to instantiate a dictionary and add the corresponding data. You can see how to add data.

The IDictionary interface has two important attribute members: Keys and Values. A set of keys and values. So we can do a lot of things based on these two attribute members.

IDictionary dic = new Hashtable();foreach(object keyObj in dic.Keys){     Console.WriteLine(dic[keyObj]);}

The acquisition method of the preceding set is similar. Dic [keyObj] is equivalent to dic. get (key ). Of course, Values can also be iterated directly. I will not talk much about it. For deletion, you can call the Remove Method to input the corresponding key parameters.

The above knowledge is some of the frequently-used writing methods long ago. Current version changes. A lot of common knowledge is also replaced. But the writing method will not change.

The ArrayList class is replaced by the List <T> class.

The Hashtable class is replaced by the Dictionary <TKey, TValue> class.

The above and T, TKey, and TValue are generic. The following is the corresponding instantiation.

IList strList = new List<object>();IDictionary dic = new Dictionary<object,object>();

In addition to the preceding descriptions, the collection class. There are also the Queue class, SortedList class, And Stack class. I will not discuss it here. If you are interested, you can find the information.

Note: we often talk about thread-safe collection classes in our applications:

1. ConcurrentQueue

2. QueueConcurrentStack

3. ConcurrentBag

4. ConcurrentDictionary

5. BlockingCollection

Summary

This chapter describes C # basic syntax. I believe it is very easy for experienced JAVA developers. At the same time, we can see that the syntax between JAVA and C # is similar. The difference lies in the difference in writing.

Related Article

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.