当前位置:网站首页>C # basic knowledge (3)
C # basic knowledge (3)
2022-07-03 23:06:00 【BeanInJ】
List of articles
Write it at the front
Main contents of this paper :Console class 、Math class 、Random class 、DateTime class 、Regex class 、 String common operations 、 Data conversion
1、C# Several common classes provided
1.1、Console class
Common methods
| Method | describe |
|---|---|
| Write | Output content to the console without wrapping |
| WriteLine | Output content to the console and wrap |
| Read | Read a character from the console |
| ReadLine | Read a line of characters from the console |
1.2、Math class
| Method | describe |
|---|---|
| Abs | Take the absolute value |
| Ceiling | Returns the smallest integer value greater than or equal to the specified double precision floating-point number |
| Floor | Returns the maximum integer value less than or equal to the specified double precision floating-point number |
| Equals | Returns whether the specified object instances are equal |
| Max | Returns the value of the larger of two numbers |
| Min | Returns the decimal value of two numbers |
| Sqrt | Returns the square root of a specified number |
| Round | Returns the rounded value |
1.3、Random class
| Method | describe |
|---|---|
| Next() | Each time a different random positive integer is generated |
| Next(int max Value) | To produce a ratio max Value Small positive integers |
| Next(int min Value,int max Value) | Produce a minValue~maxValue The positive integer , But does not contain maxValue |
| NextDouble() | Produce a 0.0~1.0 Floating point number |
| NextBytes(byte[] buffer) | To fill an array of specified bytes with random numbers |
1.4、DateTime class
| Method | describe |
|---|---|
| Date | Get the date part of the instance |
| Day | Get the date represented by the instance is the day of a month |
| DayOfWeek | Get the day of the week represented by the instance |
| DayOfYear | The date represented by this instance is the day of the year |
| Add(Timespan value) | Add a time interval value to the specified date instance value |
| AddDays(double value) | Add the specified number of days to the specified date instance value |
| AddHours(double value) | Adds the specified number of hours to the specified date instance value |
| AddMinutes(double value) | Adds the specified number of minutes to the specified date instance value |
| AddSeconds(double value) | Adds the specified number of seconds to the specified date instance value |
| AddMonths(int value) | Add the specified month to the specified date instance value |
| AddYears (int value) | Adds the specified year to the specified date instance value |
example
class Program
{
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine(" Current date is :{0}", dt);
Console.WriteLine(" The current time is the... Of this month {0} God ", dt.Day);
Console.WriteLine(" Now it's :{0}", dt.DayOfWeek);
Console.WriteLine(" The current year is {0} God ", dt.DayOfYear);
Console.WriteLine("30 The date in days is {0}", dt.AddDays(30));
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(2019, 6, 1);
TimeSpan ts = dt2 - dt1;
Console.WriteLine(" The number of days between is {0} God ", ts.Days);
}
}
1.5、Regex class ( Regular expressions )
Metacharacters in regular expressions
| character | describe |
|---|---|
| . | Match all characters except newline |
| \w | Match the letter 、 Numbers 、 Draw the line down |
| \s | Match blanks ( Space ) |
| \d | Match the Numbers |
| \b | Match the beginning or end of the expression |
| ^ | Match the beginning of the expression |
| $ | Match the end of the expression |
A character that represents a repetition in a regular expression
| word operator | Sketch Statement |
|---|---|
| * | 0 One or more characters |
| ? | 0 Time or 1 Sub characters |
| + | 1 One or more characters |
| {n} | n Sub characters |
| {n,M} | n To M Sub characters |
| {n, } | n More than characters |
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter a mailbox ");
string email = Console.ReadLine();
Regex regex = new Regex(@"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
if (regex.IsMatch(email))
{
Console.WriteLine(" The email format is correct .");
}
else
{
Console.WriteLine(" The email format is incorrect .");
}
}
}
2、 String common operations
class Program
{
static void Main(string[] args)
{
// To obtain the length of the
string str = " This is a string !";
Console.WriteLine(" The length of the string is :" + str.Length);
Console.WriteLine(" The first character in the string is :" + str[0]);
Console.WriteLine(" The last character in the string is :" + str[str.Length - 1]);
// Character position , No return -1
int i = str.IndexOf("@");
// Character substitution
str = str.Replace("!", ".");
// Character interception
str = str.Substring(0, 2);
// Character insertion
str = str.Insert(1, ",");
}
}
3、 Data conversion
3.1、 Implicit conversion
int a = 100;
double d = a; // take int Type conversion to double type
float f = 3.14f;
d = f; // take float Type conversion to double type
3.2、 Explicit conversion
| Method | describe |
|---|---|
| ToBoolean | If possible , Convert type to Boolean . |
| ToByte | Convert type to byte type . |
| ToChar | If possible , Convert the type to a single Unicode Character type . |
| ToDateTime | Put the type ( Integer or string type ) Convert to date - Time structure . |
| ToDecimal | Converts a floating point or integer type to a decimal type . |
| ToDouble | Convert type to double precision floating point type . |
| ToInt16 | Convert type to 16 Bit integer type . |
| ToInt32 | Convert type to 32 Bit integer type . |
| ToInt64 | Convert type to 64 Bit integer type . |
| ToSbyte | Convert type to signed byte type . |
| ToSingle | Convert type to small floating-point type . |
| ToString | Convert type to string type . |
| ToType | Converts the type to the specified type . |
| ToUInt16 | Convert type to 16 Bit unsigned integer type . |
| ToUInt32 | Convert type to 32 Bit unsigned integer type . |
| ToUInt64 | Convert type to 64 Bit unsigned integer type . |
3.3、 Coercive transformation
double dbl_num = 12345678910.456;
int k = (int) dbl_num ;// Cast is used here
3.4、 Parse Methods and ToString Method
Parse Method is used to convert a string type to any type
int num1 = int.Parse("12348");
ToString Method is used to convert any data type to a string type
int a=100;
string str=a.ToString();
3.5、Convert Method
It can convert the value of any data type into any data type , The premise is not to exceed the range of the specified data type .
| Method | explain |
|---|---|
| Convert.ToInt16() | Convert to integer (short) |
| Convert.ToInt32() | Convert to integer (int) |
| Convert.ToInt64() | Convert to integer (long) |
| Convert.ToChar() | Convert to character (char) |
| Convert.ToString() | Convert to string (string) |
| Convert.ToDateTime() | Convert to date type (datetime) |
| Convert.ToDouble() | Convert to double precision floating point (double) |
| Conert.ToSingle() | Convert to single precision floating point (float) |
class Program
{
static void Main(string[] args)
{
float num1 = 82.26f;
int integer;
string str;
integer = Convert.ToInt32(num1);
str = Convert.ToString(num1);
Console.WriteLine(" Value converted to integer data {0}", integer);
Console.WriteLine(" Convert to string {0},",str);
}
}
3.6、 Packing and unpacking ( Value type and reference type )
int val = 100;
object obj = val;
Console.WriteLine(" The value of the object = {0}", obj);
// This is a packing process , Is the process of converting a value type to a reference type
int val = 100;
object obj = val;
int num = (int) obj;
Console.WriteLine("num: {0}", num);
// It's an unpacking process , Is to convert a value type to a reference type , The process of converting a reference type to a value type
边栏推荐
- 2 spark environment setup local
- webAssembly
- How to quickly build high availability of service discovery
- [template summary] - binary search tree BST - Basics
- Common problems in multi-threaded learning (I) ArrayList under high concurrency and weird hasmap under concurrency
- Recursion and recursion
- MLX90614 driver, function introduction and PEC verification
- 在恒泰证券开户怎么样?安全吗?
- How to solve the problem of computer networking but showing no Internet connection
- Ansible common usage scenarios
猜你喜欢

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Sort merge sort
![[note] IPC traditional interprocess communication and binder interprocess communication principle](/img/f6/36c28df02198539e27352e3cdf4ba6.jpg)
[note] IPC traditional interprocess communication and binder interprocess communication principle

STM32 multi serial port implementation of printf -- Based on cubemx

Pat grade A - 1164 good in C (20 points)

Unique in China! Alibaba cloud container service enters the Forrester leader quadrant

Es6~es12 knowledge sorting and summary

Hcip 13th day notes

How to solve win10 black screen with only mouse arrow

Go Technology Daily (2022-02-13) - Summary of experience in database storage selection
随机推荐
Format cluster and start cluster
Team collaborative combat penetration tool CS artifact cobalt strike
Take you to master the formatter of visual studio code
Programming language (1)
STM32 multi serial port implementation of printf -- Based on cubemx
Harbor integrated LDAP authentication
Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
webAssembly
Blue Bridge Cup -- Mason prime
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
The overseas listing of Shangmei group received feedback, and brands such as Han Shu and Yiye have been notified for many times and received attention
Buuctf, misc: sniffed traffic
How to quickly build high availability of service discovery
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Wisdom tooth technology announced that it had completed the round D financing of US $100million and had not obtained a valid patent yet
[Android reverse] application data directory (files data directory | lib application built-in so dynamic library directory | databases SQLite3 database directory | cache directory)
[note] IPC traditional interprocess communication and binder interprocess communication principle
Unique in China! Alibaba cloud container service enters the Forrester leader quadrant
FPGA tutorial and Allegro tutorial - link
Sow of PMP