Basic knowledge that is often misunderstood C,

Source: Internet
Author: User

Basic knowledge that is often misunderstood C,

  • Misunderstanding of value type and reference type (the reference type is stored on the stack, and 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.

public class User{   public int Age { get; set; }}

The value of this variable in this User class is always in the same way as other data in the object, that is, in the stack. in fact, only local variables and method parameters are on the stack, but some local variables are not stored in special cases.

On the stack, such as the anonymous function closure, these features are 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 of the features added later are the help of the compiler,

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

 

static void Main(string[] args){    var str = "Hello, world.";    str = 10;}

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, the data type is determined during compilation, so you cannot use the following code

Static void Main (string [] args) {var obj = null; // declare a null pointer var obj ;}

This also proves that it does determine the type at compilation, but the compiler helps you to change the magic. Because the compiler deduced at compilation, it does not know what this is, in this way, its magic will fail.

 

(Subsequent supplements ......)

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.