You can use the Array class provided in the .NET Framework to manipulate and sort the members of the array.


using System;

namespace ApplyArrays

class ApplyArrays

static void Main() {

// Initialize an array in same line as declare string[] sGoalList = {"Hike the Appalachian Trail", "Run a marathon",

"Give $1 million to worthwhile causes"}; // Write the members of sGoalList to the console before modifying Console.Write("Before sort:\n"); Console.WriteLine("{0}\n{1}\n{2}\n",sGoalList); // Write the sGoalList to the console after sorting Array.Sort(sGoalList);

Console.WriteLine( "\nAfter the sort: " ); Console.WriteLine("{0}\n{1}\n{2}\n",sGoalList);


WorkArrays - Notepad

File Edit Format Help

// Store response to goals in the array (declaring and initializing on s stringO sGoalResponse = {"If you are staring from GA, you should ge + "started in early spring, so you will not get caught in snot "Make sure that you have a good pair of shoes.", "Start saving as soon as possible,"};

// Give the user a list of goals to choose from Console.WriteLinefGOAL LIST"),

Console.WriteLineC'Goai" + i +" -" + sGoaiList[i]);

// Request the user to choose a goal. Console.VVriteLine (""), //Write an emply line for space Console.VVritefPlease choose the number of the " + I'goal that you wantto achieve [0,1,2] ");


ILU -1 ±J 1

FC: WINNT\System32S.cmd.exe_

Microsoft Windows 2000 [Version 5.00.2195] <C> Copyright 1985-2000 Microsoft Corp.

ICiXDocuments and Settings\Administrator>cd C:\CSharp C: SCsharp>csc Worltflrrays. cs

Microsoft <R> Uisual Ctt Compiler Uersion 7.00.9148 [CLR version vi.0.2 6151

Copyright <C> Microsoft Corp 2000. fill rights reserved.

UorkArrays.cs<8,10>: warning CS0219: The assigned but its ualue is never u

C • SCshari3>Workflrravs .exe COAL LIST

Goal 0 - Hike the Appalachian Trail Goal 1 — Fun the marathon

Goal 2 - Give $1 million to worthwhile ca riable 'sReplyStatement'

Please choos C:SCsharp>

the number of the goal that you want to .

, Save the file as the class name.

Note: See page 35 for more information on saving a file.

■ The program displays the message about the goal list and options.

Was this article helpful?

0 0

Post a comment