Common methods of using static methods in C # and strings (Seventh day)

Source: Internet
Author: User

Again came to today's summary time, because yesterday in the cloud and college learned knowledge did not understand, today the teacher and special to us in very detail to say again, here very thank the teacher. O (∩_∩) o not much to say, the following begins to summarize the use of static methods and strings of common methods.

Theory: Static method statics

[Access modifier] [Static] Return value type method name ()

{

Method body;

}

• Naming rules: Method names start with uppercase, parameter names start with lowercase, parameter names, variable names make sense • Calls to methods are called in two ways for static methods • If you are in the same class, write the name directly on the line. • or the class name. Method name (); return can exit the method immediately. common methods that have been learned:Console.WriteLine (); Console.Write (); console.readline (); Console.readkey (); Int. Parse (string); • For methods that are decorated by static, use: convert.toint32. class name. Method Name ().• If you call your own static-decorated method in a class, you can omit the class name. Scope of the variable• A variable defined in a method is called a local variable whose scope begins with the definition and ends at the end of the curly brace where it is located. What if you want to access the variables in another method in one method? • Two solutions: parameters and return valuesThe return statement in the method causes the function to return immediately. Return in a function that returns a void, return value in a function that returns a value other than void method out parameters and ref parameters

The ref function parameter is passed by default, which is "copy one", Example:

Out is internally assigned to external variables, and out is generally used in places where functions require multiple return values.

Overloading of functions

Conditions that make up the Overload: the parameter types are different or the number of parameters is different (not rigorous), regardless of the return value.

Common methods of strings

Converts a string of STR to uppercase: Str. ToUpper ()

Converts a string of str to lowercase: str. ToLower ()

Intercept string: str. Substring (parameter 1, parameter 2)//parameter 1 is where the intercept starts, and parameter 2 is the length of the Intercept. (Note: All positions are starting from 0)

Gets the index of the string: str. IndexOf (the character to get the index)

Substitution of strings: Str. Replace ("The character to replace (old value)", "Replace result (new value)"

Combining strings: String Str=string.join ("|", STRs)//string[]strs={"A", "B", "C", "D"}

Remove space: Str. Trim ()

Try conversion: Console.WriteLine ("Please enter a number");  int num; Int.TryParse(Console.ReadLine (), out num);//The first argument is the string to be converted, the converted Result

Determines whether the character is empty: String str=string.empty; String.IsNullOrEmpty (str)//NULL then return True

Var: starting with Visual C # 3.0, variables declared in the method scope can have implicitly-typed Var.
an implicitly typed local variable is a strongly typed variable (as if you have declared the type), but the type is determined by the compiler. The following two I declarations are functionally equivalent:
Example: var i = 10; Implicitly typed
int i = 10; Explicitly typed

Real: * Find the largest integer in the array
static void Main (string[] args)        {            int[] num = {21,-5,32,14,1};            int max = Searchnum (num);            Console.WriteLine (max);            Console.readkey ();        }                <summary>///Find the largest integer in the array///</summary>//        <param name= "num" > Integer array </param >        ///<returns> max value </returns>        static int searchnum (Int[]num)       {            int max = 0;            for (int i = 0; i < Num. Length; i++)           {                if (Max<num[i])                {                    max = num[i];                }           }                return max;               }    

Repeat to let the user enter a number to determine whether the number is prime, enter Q end

    Static voidMain (string[] args)            {Isper ();        Console.readkey (); }        Static voidIsper () { while(true) {Console.WriteLine ("Pro Please enter a number, press Q to end");
stringstr =Console.ReadLine ();
if(str = ="Q") {Console.WriteLine ("Input End");
Break; } Else{
intnum =Convert.ToInt32 (str);
BOOLb =true; for(inti =2; I <num-1; i++) { if(num% i = =0) {Console.WriteLine ("not prime .");
b=false; Break; } } if(b = =true) {Console.WriteLine ("is prime"); } } } }

Output a string array as a | split form, such as "Messi | kaka | Jong" (using this method to achieve this function)

       static void Main (string[] args)        {            string[] Strarray = {"Messi", "Kaka", "Cristiano Ronaldo"};
Oper (Strarray);
Console.readkey (); } static void Oper (string[] strs) { string resoult = string. Empty;
for (int i = 0; i < STRs. Length-1; i++) { Resoult + = Strs[i] + "|"; } Resoult + = Strs[strs. LENGTH-1]; Console.WriteLine (Resoult); }

to find the maximum and minimum values (with out ) of an array of type int using the method implementation

 static void Main (string[] args) {int[] num = { -5,-11,5,0,1 9,30};            
int Max; int min;
Max (num, out Max);
Console.WriteLine (max);

min (num, out min); Console.WriteLine (min); Console.readkey (); } static void Max (int[]num,out int max) {int m = 0;
for (int i = 0; i < Num. Length; i++) {if (M<num[i])
{m = num[i]; }}
Max = m;
public static void min (int[]num,out int Min) {
int m = 0;
for (int i = 0; i < Num. Length; i++) {if (M>num[i]) {m = num[i]; }}
min = m; }

Ask the user to enter a number, judging if it is positive then add one, if it is negative then subtract a method implementation (with ref)

static void Main (string[] args)        {             int a = 5;            int resoult= Operator (ref a);//The parameter passed in is just a (copy)            Console.WriteLine (a);            Console.WriteLine (Resoult);            Console.readkey ();        }        static int Operator (ref int num)  //pass by reference        {           return  num > 0? ++num:--num;                  }

Well, let's write about it today. Come on!

Common methods of using static methods in C # and strings (seventh day)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.