当前位置:网站首页>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
边栏推荐
- Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
- Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
- Ten minutes will take you in-depth understanding of multithreading. Multithreading on lock optimization (I)
- Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
- Shell script three swordsman awk
- Creation of the template of the password management software keepassdx
- [Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)
- Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
- Arc135 partial solution
- Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
猜你喜欢

Esp-idf turns off serial port log output.

Qtoolbutton - menu and popup mode

Hcip day 15 notes

Firefox set up proxy server

MLX90614 driver, function introduction and PEC verification

Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
![[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)](/img/1d/044e81258db86cf34eddd3b8f5cf90.jpg)
[Android reverse] use DB browser to view and modify SQLite database (download DB browser installation package | install DB browser tool)

Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120

How to switch between dual graphics cards of notebook computer

Buuctf, misc: n solutions
随机推荐
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Summary of fluent systemchrome
Pyqt5 sensitive word detection tool production, operator's Gospel
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
Take you to master the formatter of visual studio code
Learning notes of raspberry pie 4B - IO communication (SPI)
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Quick one click batch adding video text watermark and modifying video size simple tutorial
Unity shader visualizer shader graph
Yyds dry goods inventory hands-on teach you to create a jigsaw puzzle using the canvasapi
Introduction to the gtid mode of MySQL master-slave replication
SDMU OJ#P19. Stock trading
How to quickly build high availability of service discovery
Sort merge sort
[note] IPC traditional interprocess communication and binder interprocess communication principle
NPM script
ThreadLocal function, scene and principle
Errors taken 1 Position1 argument but 2 were given in Mockingbird
IO flow review
Team collaborative combat penetration tool CS artifact cobalt strike