csharp_learn c#

csharp learn

JSON

C # parsing JSON string summary https://www.cnblogs.com/nc923/p/11418583.html

C # parsing json string processing (the most clear and easy to understand method)
https://blog.csdn.net/sajiazaici/article/details/77647625

  • json serialization: Manually generate json strings.
public class Employees
{
    public string firstName { get; set; }
    public string lastName { get; set; }
}

Employees e1 = new Employees();
e1.firstName = "Ma";
e1.lastName = "Yun";

string str = JsonConvert.SerializeObject(e1);
Console.WriteLine(str);

json deserialization: get the data in json.

public class RootObject
    {
        public string companyID { get; set; }
        public List<Employees> employees { get; set; }
        public List<Manager> manager { get; set; }
    }

 string jsonText = " {\"companyID\":\"15\",\"employees\":[{\"firstName\":\"Bill\",\"lastName\":\"Gates\"},{\"firstName\":\"George\",\"lastName\":\"Bush\"}],\"manager\":[{\"salary\":\"6000\",\"age\":\"23\"},{\"salary\":\"8000\",\"age\":\"26\"}]} ";

Console.WriteLine(jsonText);
Employees rb = JsonConvert.DeserializeObject<Employees>(jsonText);

-Problems encountered during deserialization:
"The object reference is not set to an instance of the object"

  • Get the json string in the string
    method:

    JObject jo = JObject.Parse(jsonText);

String

Get string substring
string.Substring


Project example

GIT open source project address: https://code.csdn.net/yeqi3000/supernetdebuger

C # communication programming exchange group: 83868794

Thread

The thread hangs ms
Thread.Sleep (2000);

C # Summary of the method of passing parameters to the thread
https://www.cnblogs.com/Liyuting/p/9087216.html

other

Method of C # function returning multiple values
https://www.cnblogs.com/masonmei/p/11546377.html

Customize the stack to achieve bracket matching
https://blog.csdn.net/lxy344x/article/details/53613805

String.IndexOf method
Definition
Namespace:
System
assembly:
mscorlib.dll
reports the zero-based index of the first occurrence of the specified Unicode character or string in this instance. If the character or string is not found in this instance, this method returns -1.


c # textbox scroll bar is always pointing to the bottom of the most simple solution
Updated: November 26, 2017 16:20:52 Contributor: mrr I want to comment
this article introduces the scroll bar c # textbox always points to the bottom For a simple solution at the end, friends who need it can refer to it.
When I first added the scroll bar, I found that the scroll bar always ran up, after research

The solution is as follows:

• this.textBox1.Focus ();
• Get focus
• this.textBox1.Select (this.textBox1.textLength, 0);
• Select the last line of text
• this.textBox1.ScrollToCaret ();
Push the scroll to the cursor
ps : Let's take a look at the c # textBox scroll bar has been in the bottom of the solution

this.textBox1.Focus (); // get the focus
this.textBox1.Select (this.textBox1.TextLength, 0); // text cursor to the last
this.textBox1.ScrollToCaret (); // cursor to scroll to


C # how to add an existing form to the project

There is a program to use a form in the previous project, now I want to add it to the current project, but by adding existing items, the three files of the form Form1.cs, Form1.Designer.cs, The Form1.resx file is displayed side by side in the project, there is no subordination, the added forms are blank, and cannot be displayed normally

----- When adding, just select Form1.cs, just don't select the other two.


Various time formats in .NET

DateTime.Now.ToString();            // 2008-9-4 20:02:10
DateTime.Now.ToLocalTime().ToString();        // 2008-9-4 20:12:12

//获取日期
DateTime.Now.ToLongDateString().ToString();    // 2008年9月4日
DateTime.Now.ToShortDateString().ToString();    // 2008-9-4
DateTime.Now.ToString("yyyy-MM-dd");        // 2008-09-04
DateTime.Now.Date.ToString();            // 2008-9-4 0:00:00

//获取时间
DateTime.Now.ToLongTimeString().ToString();    // 20:16:16
DateTime.Now.ToShortTimeString().ToString();    // 20:16
DateTime.Now.ToString("hh:mm:ss");         // 08:05:57
DateTime.Now.TimeOfDay.ToString();         // 20:33:50.7187500

//其他
DateTime.ToFileTime().ToString();        // 128650040212500000
DateTime.Now.ToFileTimeUtc().ToString();    // 128650040772968750
DateTime.Now.ToOADate().ToString();        // 39695.8461709606
DateTime.Now.ToUniversalTime().ToString();    // 2008-9-4 12:19:14

DateTime.Now.Year.ToString (); Get the year // 2008
DateTime.Now.Month.ToString (); Get the month // 9
DateTime.Now.DayOfWeek.ToString (); Get the week // Thursday
DateTime.Now.DayOfYear .ToString (); Get the first few days // 248
DateTime.Now.Hour.ToString (); Get the hour // 20
DateTime.Now.Minute.ToString (); Get the minute // 31
DateTime.Now.Second.ToString ( ); Get seconds // 45

// n is a number, you can count integers or decimals
dt.AddYears (n) .ToString (); // Time plus n years
dt.AddDays (n) .ToString (); // Add n days
dt. AddHours (n) .ToString (); // Add n hours
dt.AddMonths (n) .ToString (); // Add n months
dt.AddSeconds (n) .ToString (); // Add n seconds
dt.AddMinutes (n) .ToString (); // Add n points

System.DateTime.Now.ToString ("yyyy year MM month dd day dddd HH: mm"); The
output format is:
Monday, July 31, 2006 09:26
This is relatively simple


Display 0x00
static void Main (string [] args) in hex tmp

{
    byte[] tmp = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    string str1 = BitConverter.ToString(tmp);
    Console.WriteLine(str1);
}
/*

直接显示为
00-00-30-12-44-77-77-14
*/

https://bbs.csdn.net/topics/392158678?page=1

or

byte[] bufData = { 0x00, 0x56, 0x01, 0x00, 0xAD, 0x2F, 0xF1, 0x00, 0x67, 0xA3, 0x40, 0x00};

  Console.WriteLine("replace 0x00 to ?:{0} \r\n", Encoding.Default.GetString(bufData).Replace('\0','0'));

Determine whether the string contains Chinese

        /// <summary>
        /// 判断字符串中是否包含中文
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>判断结果</returns>
        private static bool HasChineseChar(string str)
        {
            return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }

Popup prompt, confirm
doc

Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)
displays a message box with the specified text, title, button, icon, default button, and options in front of the specified object.

public static System.Windows.Forms.DialogResult Show (System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);

The parameter
owner
IWin32Window
will have an implementation of the modal dialog box IWin32Window.
text
String The text
to be displayed in the message box.
caption
String
The text to be displayed in the title bar of the message box.
buttons
MessageBoxButtons One of the
MessageBoxButtons values ​​that specifies which buttons are displayed in the message box.
icon
MessageBoxIcon One of the
MessageBoxIcon values, which specifies which icon is displayed in the message box.
defaultButton
MessageBoxDefaultButton One of the
MessageBoxDefaultButton values, you can specify the default button of the message box.
options
MessageBoxOptions One of the
MessageBoxOptions values ​​that specifies which display and associated options will be used for the message box. To use the default value, pass in 0.
Returns one of
DialogResult
DialogResult values.
Exception
InvalidEnumArgumentException
buttons are not members of MessageBoxButtons.
-Or- icon is not a member of MessageBoxIcon.
-Or- defaultButton is not a member of MessageBoxDefaultButton.
InvalidOperationException
attempts to display the MessageBox in a process that is not in user interaction mode. This is specified by the UserInteractive attribute.
ArgumentException
options specify both DefaultDesktopOnly and ServiceNotification.
-Or- options specifies DefaultDesktopOnly or ServiceNotification and specifies a value in the owner parameter. These two options are only used when calling the method version that does not accept the owner parameter.
-Or- buttons specified an invalid combination of MessageBoxButtons.
Example
The following code example demonstrates how to display the MessageBox using the options supported by this overload of Show. After verifying that the string variable ServerName is empty, the example will display a MessageBox to provide the user with the option to cancel the operation. If the return value of the Show method is Yes, then close the MessageBox form.
C #

copy

private void validateUserEntry2()
{

    // Checks the value of the text.

    if(serverName.Text.Length == 0)
    {

        // Initializes the variables to pass to the MessageBox.Show method.

        string message = "You did not enter a server name. Cancel this operation?";
        string caption = "No Server Name Specified";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.

        result = MessageBox.Show(this, message, caption, buttons,
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
            MessageBoxOptions.RightAlign);

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}

Note
You can use the owner parameter to specify a specific object that implements the IWin32Window interface, which will be used as the top-level window and owner of the dialog box. The message box is a modal dialog box, which means that no input (keyboard or mouse clicks) will appear except for objects on the modal form. Before entering into another form, the program must hide or close the modal form (usually in response to certain user actions).
There can be up to three buttons on the message box.


Remove (Int32, Int32)
returns a new character string with the specified number of characters at the deleted specified position at the current starting point of this instance.

public string Remove (int startIndex, int count);

Parameter
startIndex Int32

The zero-based position at which to start deleting characters.
count Int32
The number of characters to be deleted.
Returns a new string of
String
. This string is equivalent to this instance except for the deleted characters.
Original address-Microsoft official documentation

Guess you like

Origin www.cnblogs.com/dysonnnn/p/12666744.html