Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use variables and data types in C #

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use variables and data types in C#". In daily operation, I believe that many people have doubts about how to use variables and data types in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to use variables and data types in C#"! Next, please follow the editor to study!

There are multiple namespaces and classes in a program, and you can also use the using keyword to refer to namespaces. The use of the using keyword is either placed before all elements or before all elements defined in the namespace. The class methods of other namespaces can be used through the class. Method name to call.

First, the definition of variable variables;

In C #, each variable has a specific type. Therefore, before declaring a variable, you have to define what data type the variable belongs to.

Variables can be defined one by one, and they can also be defined continuously. Value_list, for example, is made up of identifiers separated by commas.

Int x, y, z * char c, n * float e, f

The premise of continuous definition is that these variables belong to the same data type.

Initialize assignment

After defining the data type in C #, you can assign a value of the corresponding type to the variable.

For example:

Int x, y, ZTX x = 1TX y = 2TX z = 3Tracchar c, nTAC = 'caterpillar n =' n'

In addition to the operation of assigning a value after definition, you can directly define the type of a variable and assign it a variable value:

Int Xerox 1, YP2, Zipchar clockwise, nasty input; user input value

The user can enter a value through the Console.ReadLine () method. However, these values are all string types by default, so if you want to convert the values of the input conversion to other types. For example, if you enter an integer, you can use Convert.ToInt32 (string); convert the corresponding string to an integer type:

Int m = Convert.ToInt32 (Console.ReadLine ()); identifier

Identifiers are used to identify classes, variables, functions, or any other user-defined item. Identifiers must follow the following basic rules:

Identifiers must begin with a letter, underscore, or @, followed by a number, letter, underscore, or @ symbol.

The first number of an identifier must not be a number.

Identifiers cannot contain any embedded spaces or symbols.

The identifier cannot be a keyword for C#. Unless the keyword is preceded by an @ prefix. For example: @ if is a valid identifier, but if is not.

Identifiers must be case sensitive, and uppercase and lowercase letters are considered different letters.

The identifier cannot be the same as the C# class library name.

2. Data type

In C #, variables can be divided into three data types: value type, reference type and pointer type.

Value Typ

List some common value types:

Bool (default: false)

Int (default: 0)

Double (default: 0.0D)

Float (default: 0.0F)

Decimal (default: 0.0m)

Char (default:'\ 0')

Reference type

Reference types do not contain the actual data stored in the variable, but they do contain references to the variable.

The reference type points to a memory location.

Built-in reference types:

Object type (Object)

The object type is the final accumulation of all data types in the C# common type system.

When a value type is converted to a reference type, the behavior is called boxing; when a reference type is converted to a value type, the behavior is called unboxing.

Object obj; / / declare object type obj = 100; / / this is boxing

Dynamic type (Dynamic)

Values of any type can be stored in dynamic data type variables, and type checking for these variables occurs during the check.

Dynamic = 100

Similar to object types, but object types occur at compile time, while dynamic types occur at check time.

String type (String)

Char is a character type and can only be assigned a value.

A string can be assigned a string of characters.

Char c = 'nasty] string s = "I am the king!"

In addition, char wraps characters in single quotes, and string wraps strings in double quotes.

Strings can be assigned not only with double quotes, but also with @ double quotes.

For example:

String s = @ "The first wordgood nice\ nbiuuuuuuuuu!"

A string preceded by @ is called a verbatim string. In which the transfer symbol\ will be invalidated. At the same time, the text is output according to the format in the program.

Pointer type (Pointer Types)

Pointer type variables store another type of memory address.

Declaration syntax:

Type* identifier;char* cptr;int* iptr; at this point, the study on "how to use variables and data types in C#" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report