Basic knowledge that is often misunderstood (1) C #,

Source: Internet
Author: User

Basic knowledge that is often misunderstood (1) C #,

 

 

 

  • Misunderstanding of value type and reference type (The reference type is stored on the stack,The value type is stored on the stack.)

 

 

When learning C # basic space, I can't escape the value type and reference type. Many new users, including my previous understanding of it, just stay in"The reference type is stored on the stack,The value type is stored on the stack.".

This misunderstanding is mainly attributed to the fact that we have no brains at all. The first sentence is correct, and reference instances are always created on the stack. but there is a problem with the last sentence. suppose a class has an int type instance variable.

(The following code)

1     public class Person2     {3        public int Age { get; set; }4     }

 

In this case, the value of this variable in the Person class is always associated with other data in the object, that is, on the stack. in fact, only local variables and method parameters are on the stack, but for c #2.0 and later versions, many local variables are not fully stored.

On the stack,For example, the anonymous function closure is not suitable for beginners.

 

  • Is Var a dynamic type?

This issue is confusing when JavaScript programmers learn C. although C # is not a completely strong-type language (C #4.0 or above allows you to use dynamic types), C #3.0 or below is basically a strong-type language.

Before explaining the var keyword, we need to know that C #2.0 has modified the CLR, but basically no major changes have been made in subsequent versions, this means that many features are being silently assisted by the compiler,

For example, C #3.0 introducesVarType. To better demonstrate that Var is not a dynamic type, I will introduce an instance.

1         static void Main(string[] args)2         {3 4             var str = "Hello, world.";5 6             str = 10;7 8         }

The above Code cannot be compiled, and the compiler will tell you"The type "int" cannot be implicitly converted to "string ".".

Because the str type is String, you have determined the type when writing the Declaration str variable. What is the purpose of var?

The reason why Var implicit type is used is simple. It reduces the input of code, which increases readability. Especially when generics are involved, the type name changes quite long. I still introduce an instance.

1 static void Main (string [] args) 2 {3 4 // The first 5 Dictionary without using the var variable <List <int>, IEnumerable <Person> dic1 = new Dictionary <List <int>, IEnumerable <Person> (); 6 7 // After the var variable is used, 8 var dic2 = new Dictionary <List <int>, IEnumerable <Person> (); 9}

It can be seen that the variables using var are indeed more readable, and the amount of code is reduced (but the var type can not be abused anywhere)

When declaring the dic2 variable, it determines the type at compilation, rather than at runtime, to prove this, you can move the cursor over var. VS will prompt you for the type you are using.

 

 

This also proves that it determines the type during compilation, but the compiler helps you to change the magic. if you are still skeptical, you can declare a var number. Try to see if it passes. The answer must be no.

Because the compiler deduced it during compilation, it would not have known what it was, so its magic would have failed.

It should also be noted that not all scenarios of var can be inferred. In fact, it has shortcomings and will not be listed here.

 

Do not abuse the var implicit type

Example

Static void Main (string [] args) {// you cannot intuitively know the type of the number var number = getValue ();}

 

The above Code cannot intuitively know what type of variable number is during maintenance, but you need to move the cursor over var to know.

Now let's talk about it for the time being. First, let's take a nap. If you like it, I will list more common mistakes in the future. If anything is wrong

You are welcome to learn from each other.

 

 

 

 

 

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.