C # Basics

Posted by wildwobby on Sat, 06 Nov 2021 23:51:02 +0100

C# type conversion

There are two types of conversions in C#:

  • Implicit type conversion: that is, automatic type conversion, which is converted in a safe way in C# without data loss. When the operand types involved in the operation on both sides of the symbol are inconsistent, automatic conversion will occur if the following conditions are met
    1. Two types are compatible, such as int and double (both digital)
    2. The target type is larger than the source type. For example, double > int means that small types are converted to large types

  • Explicit type conversion: that is, forced type conversion, which will cause data loss
    1. The two types are compatible
    2. The source type is greater than the target type (from large to small)

Method of type conversion

1.Convert conversion

using System;
namespace TryConvert
{
	class c
	{
		static void Main(string[] args)
		{
			string word="123";
			double n=Convert.ToDouble(word );
			Console.WriteLine(n);
			Console.ReadKey();
		}
	}
}

When the two types are incompatible, use Convert to Convert. The type must be the same (number to number)

2.To conversion

When the types are different, the To conversion is used, as follows:

namespace TypeConversionApplication
{
    class StringConversion
    {
        static void Main(string[] args)
        {
            int i = 8;
            float f = 67.00f;
            double d = 89.63082;
            bool b = true;

            Console.WriteLine(i.ToString());
            Console.WriteLine(f.ToString());
            Console.WriteLine(d.ToString());
            Console.WriteLine(b.ToString());
            Console.ReadKey();
           
        }
    }
}

In terms of type conversion, there are ToInt32 (converting 32 to integer, int type has 16 bits and 64, just replace 32), ToString (converting to string type), ToChar (converting to character type), ToDecimal (converting to double precision floating point type), ToBoolean (converting to boolean type) ToDate Time (converting integer or string type to date time structure), etc

3.Parse

	int numbrOne=Convert.ToInt32("123");
	// Using Convert conversion, you will succeed if you succeed, and throw exceptions if you fail
	
	int number=int.Parse("");  //It is as like as two peas in Convert as like as two peas.
			         //Difference: Convert internally calls int.Parse (which is more efficient and less called)
			         //Double.parse decimal char like int.Parse

4.TryParse

Try to convert a string to type int:

int.TryParse:    //It is a little troublesome to use, and the performance is relatively high. Failure will not throw exceptions and will not affect the performance of the system or software	
  Try to convert a string to int Type:
	int number =0;
	//Return value (the result seen after calling the parameter) parameter (the condition that must be provided to the method to complete the method)
	bool b=int.TryParse("123",out numebr);//Try to convert the string to int type. If successful, assign the successfully converted type to numebr, and return a true to indicate success
	Console.WriteLine(b);                             //If the conversion fails, a false is returned and numebr is assigned to 0
	Console.WriteLine(number);
	
	Console.ReadKey();

variable

Variable, the name of the store that provides the program with the operation. In C #, each variable has a specific type, which determines the size and layout of the storage space occupied by the variable. When the value is within the range, you can perform a series of operations on the variable. When the value exceeds the range, an exception will occur.

1. Variable type

  1. Integer type: sbyte byte int uint short ushort long ulong char
  2. Floating point f: float double
  3. Null type: a data type that can be null
  4. Boolean type: True or False, the given value
  5. Decimal type: decimal

2. Variable definition

int a,b,c;
double d,e;
char f;
  • Initialization of variables
int a=100;

You can initialize the variable directly at the beginning, or you can define the variable first and assign a value to the variable when using the variable

3. Accept user input

The Console class provided in the System namespace provides the function = = ReadLine() = = to accept the value entered by the user

int number;
number=Convert.ToInt32(Console.ReadLine());

Because Console.ReadLine receives data in string format, Convert is used to Convert the data.

constant

Constants are fixed values that we define and do not change during program execution. Constants can be any type, integer, floating point, character, enumeration. Constants cannot be modified after definition.

const int number=30 //Constant, cannot be reassigned

1. Integer constant

Integers have decimal, octal and hexadecimal. Octal prefix is 0, hexadecimal prefix is 0x or 0x, and integer can also have suffix, which is a combination of U and L

70          //decimal system 
0754        //octal number system 
0xa5        //hexadecimal 
10          //int 
10u         //Unsigned int 
10l         //long 
10ul        //Unsigned long 

2. Floating point constant

Floating point constants are composed of integer part, decimal point, decimal part and exponential part.

  • When using floating-point representation, it must include decimal point, exponent, or both. When expressed in exponential form, it must include integer part, decimal part or both. The signed exponent is expressed by E or E.
3.14159       /* legitimate */
314159E-5L    /* legitimate */
510E          /* Illegal: incomplete index */
210f          /* Illegal: no decimal or index */
.e55          /* Illegal: missing integer or decimal */

3. Character constant

  •  \\	\ character
    
  •  \'	' character
    
  •  \"	" character
    
  • \?	? character
    
  • \a	Alert or bell
    
  • \b	Backspace key( Backspace)
    
  • \f	Page feed( Form feed)
    
  • \n	Newline character( Newline)
    
  • \r	enter
    
  • \t	Horizontal tab tab
    
  • \v	vertical tab  tab
    
  • \ooo	One to three digit octal numbers
    
  • \xhh . . .	The hexadecimal number of one or more digits
    

4. String constant

  • String constants are in or enclosed in @. String constants can contain ordinary characters, escape characters and general characters.
  • When string constants, you can split a long line into multiple lines, and you can use spaces to separate parts.
string a = @"one
two
three";

Topics: C# Back-end