Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Jump Statement(s),Enumerations, Arrays, ArrayList
|
Scope: Scope of this article is upto its Title and with descriptive view to its practical scenario.
The statement(s) as cleared from the name, provides us the facility to round-around (to and fro) within the block(s).
- The goto statement
This statement provides us the facility to jump from a block to labeled, with some restriction(s):
- Can't jump block for loop
- Can't jump a class
The frequent use of this statement is not recommended.
- The break statement
This is doing nothing but just exit from a statement(s) of block, mostly loops. You have already used the same one in switch...case statement.
- The continue statement
Its is similar to break statement, its also used with loop(s).
- The return statement
The return statement is used to exit a method of a class. It just exit the method. When a method has some returning value then return has expression returning that value, else it just exit the method.
The following code rewrites the above in a bunch of statement(s): See Code-Jump Statement(s).
/* This Example is a part of different * examples shown in Book: * C#2005 Beginners: A Step Ahead * Written by: Gaurav Arora * Reach at : gaurav.aroraose@yahoo.co.in*/
// File name : jumpstatements.cs
using System;
namespace CSharp.AStepAhead.jumpstatements {
class jumpstatements { static int Square(int n) { return (n * n); } static void Main() { lblEntry: Console.Clear(); Console.WriteLine("Enter Choice: 1 goto 2 break 3 continue 4 return 5 Exit"); Console.Write("Please enter your selection [1-5]: "); string s = Console.ReadLine(); int n; n = int.Parse(s); if (n > 5 || n < 1) { Console.Beep(); Console.WriteLine("Please Enter a valid Input [1- 5]"); goto lblEntry;
} while (n < 5) { if (n == 1) { Console.WriteLine("\nYou have entered : {0}", n); goto nLabel; nLabel: Console.WriteLine("\nThis is using goto Statement"); Console.ReadLine(); goto lblEntry; } else if (n == 2) { Console.WriteLine("\nYou have entered : {0}", n); Console.WriteLine("\nIt beaks when i=n=2\n"); for (int i = 0; i <= 9; i++) { Console.WriteLine("i:{0}\n",i); if (i == 2) break; } Console.ReadLine(); goto lblEntry; } else if (n == 3) { Console.WriteLine("\nYou have entered : {0}", n); for (int i = 2; i <= 10; i++) { if (i < 9) { continue; } Console.WriteLine("i:{0}\n",i); } Console.ReadLine(); goto lblEntry; }
else if (n == 4) { Console.WriteLine("\nYou have entered : {0}", n); Console.WriteLine("Enter number:"); int num = int.Parse( Console.ReadLine()); Console.WriteLine("Square of {0} is {1}", num, Square(num)); } Console.ReadLine(); goto lblEntry; } Console.Clear(); Console.WriteLine("Exiting ...\n"); Console.WriteLine("Have a nice day!"); Console.ReadLine();
} } }
[h4]Enumerations[/h4] Enumerations are very beneficial to our work. These are basically, user-defined integer type value. By default value of first enumerator is 0 (zero) and successively increased by 1 for next enumerator.
There are following benefits of enumerations:
- Enumerations make your code easier.
- These make code clear because here you just pass the integer value in order.
- Make code sharp when you type in Visual Studio IDE each and every will be added in IntelliSense, it's a magic.
To implementation of Enumeration See Code-Enumeration.
/* This Example is a part of different * examples shown in Book: * C#2005 Beginners: A Step Ahead * Written by: Gaurav Arora * Reach at : gaurav.aroraose@yahoo.co.in*/
// File name : enumerations.cs
using System; namespace CSharp.AStepAhead.enumerations { enum DaysofWeek { Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6
}
class enumerations { static void weekDay(DaysofWeek day) { switch (day) { case DaysofWeek.Sunday: Console.WriteLine("Enjoy your Sunday!"); break; case DaysofWeek.Saturday: Console.WriteLine("Have a nice weekend"); break; case DaysofWeek.Monday: Console.WriteLine("Welcome to Office"); break; default: Console.WriteLine("Hello, its working day"); break; } } static void Main() { Console.WriteLine("Enter Week Day (0-6)"); //DaysofWeek d = DaysofWeek.Sunday; // Console.WriteLine(d.ToString()); //get the string
string n = Console.ReadLine(); DaysofWeek dd = (DaysofWeek)Enum.Parse(typeof(DaysofWeek), n, true); //Console.WriteLine((int)dd); //get the relevant int value
weekDay(dd); Console.ReadLine(); } }
}
Note: In above with the help of this statement DaysofWeek dd = (DaysofWeek)Enum.Parse(typeof(DaysofWeek), n, true); you can produce output both in int and string as: Console.WriteLine((int)dd); //get the relevant int value Console.WriteLine(dd.ToString()); //get the relevant string
Arrays An array is a data structure that contains a number of variables of the same type. Arrays are declared with a type: type[] arrayName;
Arrays has following types:
- Single-Dimensional Array
An array which has single column as:
string[] stringArray = new string[6]; int[] intArray = new intArray[5] { 1, 3, 5, 7, 9 }; string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
- Multi-Dimensional Array
An array can have multiple row-columns, known as multi-dimensional array.
int[,] array = new int[4, 2]; int[, ,] array1 = new int[4, 2, 3]; int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } } };
Jagged-Array This is an Array within Array. A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." The following examples show how to declare, initialize, and access jagged arrays.
int[][] jaggedArray2 = new int[][] { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} };
int[][,] jaggedArray4 = new int[3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22}, {99,88}, {0,9} } };
ArrayList It is just like array but, an ArrayList whose size is increase and decrease dynamically. ArrayList can hold item of different types. As ArrayList can increase or decrease size dynamically you do not have to use REDIM / MALLOCK/CALLOCK keyword(s). You can access any item in array using the INDEX value of the array position. To implementation of ArrayList See Code-ArrayList.
/* This Example is a part of different * examples shown in Book: * C#2005 Beginners: A Step Ahead * Written by: Gaurav Arora * Reach at : gaurav.aroraose@yahoo.co.in*/
// File name : arraylist.cs
using System; using System.Collections;
namespace CSharp.AStepAhead.arraylist { class arraylist { static void dismyAList(IEnumerable myArrayList) { foreach (object obj in myArrayList) { Console.WriteLine(obj + "\n"); } Console.ReadLine(); }
static void disIndex(ArrayList myArrayList, Object myObj) { int myIndex = myArrayList.BinarySearch(myObj); if (myIndex < 0) Console.WriteLine("The object to search for ({0}) is not found. The next larger object is at index {1}.", myObj, ~myIndex); else Console.WriteLine("The object to search for ({0}) is at index {1}.", myObj, myIndex); }
static void Main() { ArrayList myAList = new ArrayList();
//Add few elements myAList.Add("1st Element"); myAList.Add("2nd Element"); myAList.Add("3rd Element"); Console.WriteLine("ArrayList is with Capacity{0}, Size {1}", myAList.Capacity, myAList.Count); Console.WriteLine("Initial Element of myAList:\n");
dismyAList(myAList);
string[] myStringArray = new string[2]; myStringArray[0] = "1st Element of myStringArray"; myStringArray[1] = "2nd Element of myStringArray"; int[] myIntArray = new int[4]; myIntArray[0] = 1; myIntArray[1] = 2; myIntArray[2] = 3; myIntArray[3] = 6; //Add range of string array myAList.AddRange(myStringArray);
//Add range of integer array myAList.AddRange(myIntArray);
Console.WriteLine("After adding range myAList Contains:\n");
dismyAList(myAList); string str = "This is inserted element"; myAList.Insert(1, str); Console.WriteLine("Finally, myAList Contains:\n"); dismyAList(myAList);
// Creates and initializes a new ArrayList. BinarySearch requires // a sorted ArrayList. ArrayList myAL = new ArrayList(); for (int i = 0; i <= 9; i++) myAL.Add(i * 2);
Console.Clear(); Console.WriteLine("\nNew ArrayList Contains:"); dismyAList(myAL); Console.WriteLine("\nEnter Element to know the Index:"); Object myObj = (int.Parse(Console.ReadLine())); disIndex(myAL, myObj); Console.ReadLine(); } }
}
For more details, visit http://www.msdotnetheaven.com/ChaptersCsharp/csharplanguagei.htm#
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|