当前位置:网站首页>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
边栏推荐
- Mongoose the table associated with the primary key, and automatically bring out the data of another table
- IO flow review
- Niuke winter vacation training camp 4 g (enumeration optimization, Euler power reduction)
- C summary of knowledge point definitions, summary notes
- Simple solution of m3u8 file format
- Opengauss database log management guide
- How to connect a laptop to a projector
- [untitled]
- How to understand the gain bandwidth product operational amplifier gain
- Ten minutes will take you in-depth understanding of multithreading. Multithreading on lock optimization (I)
猜你喜欢
Yyds dry goods inventory Spring Festival "make" your own fireworks
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!
Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place
Quick one click batch adding video text watermark and modifying video size simple tutorial
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
To rotate 90 degrees clockwise and modify the video format
4 environment construction -standalone ha
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
Summary of fluent systemchrome
Programming language (1)
随机推荐
Sow of PMP
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
In 2022, 6G development has indeed warmed up
This time, thoroughly understand bidirectional data binding 01
[sg function] 2021 Niuke winter vacation training camp 6 h. winter messenger 2
To rotate 90 degrees clockwise and modify the video format
[issue 16] golang's one-year experience in developing Purdue Technology
Hcip day 16 notes
Summary of basic knowledge of exception handling
Day30-t540-2022-02-14-don't answer by yourself
How about opening an account at Hengtai securities? Is it safe?
Op amp related - link
Es6~es12 knowledge sorting and summary
Blue Bridge Cup -- guess age
Errors taken 1 Position1 argument but 2 were given in Mockingbird
Qtoolbutton - menu and popup mode
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
Esp-idf turns off serial port log output.