C # differences with Visual Basic (2) -- Data Type

Source: Internet
Author: User

Data Type

C # data types are divided into three categories: Value Type, reference type, and type conversion.


Value TypeThere are three types: simple type, structure type, and enumeration type. WhereSimple TypeIt also includes integer, floating point, decimal, and Boolean. The simple type in C # is similar to the numeric type in VB, so we can compare the two. The numeric type in VB includes Integer type (integer and long integer), floating point type (single precision floating point type, double precision floating point type), currency type and byte type.


However, it should be clear that floating point data is used to represent the real number with decimal places. in C #, why does it list the decimal type separately? The original decimal type in C # is a high-precision data type, which occupies 16 bytes mainly to meet the needs of high-precision financial and Jinrong computer fields. The range of decimal data is far smaller than the floating point type, but it is much more accurate than the floating point type. The standard between the two is m or m after the decimal type data, otherwise the system will regard it as a standard floating point type for processing.


However, in practice, we often encounter this situation, that is, some data information is combined by several data types, we can't just define an array to cover processing all the information, so we need to use the structure type.Structure TypeIs a combination type that combines different types of data information. The structure is a custom data type. The following is a simple example.

The following is an example of the structure type.

# Region this is a structure type struct workerinformation // defines the structure type of a factory employee information {long cardno; // defines the structure of the data member, including the card number, name, gender, home address; contact information: string name; string sex; int age; string address; long telephone; void structmethod () {// executable code} # endregion

The example is very simple. The above is also annotated and will not be further explained here.

Next let's look at enumeration. What is enumeration? In one sentence,Enumeration typeAll values of a variable are listed one by one, and the value of this variable is within the listed range. The enum keyword is used to define enumeration. The following is an example.

 enum family            {                grandfather,grandmother,father,mother,sister,me;            }

This example is a simple enumeration. by defining a family variable, all values in this variable are listed one by one.


Let's talk about it below.Reference TypeIt is the parallel type of the Value Type in C #. The difference between it and the value type lies in their storage mode in memory. The value type is stored in the stack. The actual data stored by the reference type is the address of the currently referenced value, and the object itself is stored in the memory heap. There are some interfaces. In other words, a simple data type stores its own values, while a reference type stores its own values directly to the values of an object. Here, we don't have to worry about heap and stack.


Here we need to mention the difference between stack and stack. Stack is used to store data with a fixed length. The heap is the memory allocated by new, which is generally slow and prone to memory fragments, but is easy to use. In C #, the basic data types bool and long are both value types. On the contrary, most complex C # data types, including the classes we declare, are reference classes, which are allocated to the heap.


Finally, let's talk about it.Type conversionData types can be converted to each other under certain conditions. In C #, two conversion modes are allowed, namely, implicit conversion (implicit conversions) and explicit conversion (Explicit conversions ). Implicit conversions are the default values of the system and can be converted without declaration. On the contrary, Explicit conversions require the user to explicitly specify the conversion type, it is generally used when implicit conversion of this type does not exist.


In contrast, the Data Types of VB are much clearer. The data types provided by VB include numeric, numeric, Boolean, date, variable, and object. What I want to talk about here is that the data types of each programming language are different, but when we continue to master other programming languages while learning a programming language, it is necessary to compare the previously mastered content and compare the two. The same content should be further reviewed, and different places should be differentiated to master, this is a skill we should grasp when learning.



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.