C # Notes for using basic types.

Source: Internet
Author: User

1: as shown below:

Float F = 1.2;

Decimal d = 1.2;

This Code cannot be compiled, because floating-point constants such as 1.2 are of the double type by default in C #. If you want to assign values as above, you must write the following code:

Float F = 1.2f;

Decimal d = 1.2 m; // note that this is M !!!!

2: how to convert a numeric string into a real numeric variable:

Note: Int. parse () is the same as int32.parse (), because int is essentially the alias of system. int32.

Of course, the most common method is int. parse (); usage is as follows:

=== Example program that uses int.Parse (C#) ===
using System;
class Program
{
static void Main()
{
// Convert string to number.
string text = "500";
int num = int.Parse(text);
Console.WriteLine(num);
}
}
=== Output of the program ===
500

The code float F1 = int. parse (S2); can also be compiled because the compiler automatically converts the int type returned by Int. parse () to float.

A function closely related to int. parse () is: Int. tryparse ();

 

There is also one: Convert. int32 ();

=== Example program that uses convert. toint32 (C #) ===
Using system;
Class Program
{
Static void main ()
{
// Convert 'text' string to an integer with convert. toint32.
String text = "500 ";
Int num = convert. toint32 (text );
Console. writeline (Num );
}
}
=== Output of the program ===
500
How to convert a basic type to a string is a very simple problem. Each type contains a tostring () method and can be used.

 

3: in C #, you can perform the modulo operation (remainder) on floating point variables as follows:
Float F1 = 15.6f % 2f;
Console. writeline (F1); // output is 1.6
It should be noted that if the desired result must exist in the float type, the 15.6 must be followed by F. Otherwise, the double type cannot be converted to float during compilation.

4: in C #, ++, -- the operator can act on the floating point type .... If F1 = 1.6; F1 ++; it would be 2.6, huh, well, it should have such a function. In addition, the rules for front ++ and back ++ are the same as those for C ++.

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.