C # method exercises,
Using System; using System. collections. generic; using System. linq; using System. text; namespace Demo1 {class Program {/// <summary> /// find the longest element in a string array /// </summary> /// <param name = "s"> </param> /// <returns> </returns> public static string GetLongest (string [] s) {string max = s [0]; for (int I = 0; I <s. length; I ++) {if (s [I]. length> max. length) {max = s [I];} return max;} static void Main (string [] args) {// implemented by using a method: There is a string array: // {"Malone", "Michael Jordan", "raygimiller", "Kobe"}, please output the longest string [] names = {"Malone ", "Michael Jordan", "raygimiller", "Kobe"}; Console. writeLine (GetLongest (names ));}}}
Using System; using System. collections. generic; using System. linq; using System. text; namespace Demo1 {class sumnumber {/// <summary> /// calculate the average value of the integer array and retain 2 decimal places /// </summary> /// <param name = "nums"> </param> // <returns> </returns> public double GetAvg (int [] nums) {double sum = 0; for (int I = 0; I <nums. length; I ++) {sum + = nums [I];} return sum/nums. length ;}}using System; using System. collections. generic; using System. linq; using System. text; namespace Demo1 {class Program {static void Main (string [] args) {int [] numbers = {1, 2, 7}; sumnumber sber = new sumnumber (); double avg = sber. getAvg (numbers); // retain 2 decimal places to convert avg to string type // then use Convet. toDouble conversion string str = avg. toString ("0.00"); avg = Convert. toDouble (str); Console. writeLine (avg); Console. readKey ();}}}