Você está na página 1de 9

Difference Between Array and Array List in C#

By - Gurunatha Dogi Apr 24th, 2013 Comments : 2 | Views : 8712


9

Array
Arrays are strongly typed arrays. They usually start with zero indexed so that can call by using indexes during runtime. These arrays are of specified length that cannot be change during runtime. Declaration: To declare the array use "[]" brackets after data type and then assign the fixed length or fixed size to an array as shown below.

1 static void Main(string[] args) 2{ 3 int[] ar = new int[10]; 4}


Above code shows that we have declared an integer type array size of 10 (Means we can store up to 10 integer values in that array). You can repeat the same process for different data types as shown below.

1 static void Main(string[] args) 2{ 3 int[] ar = new int[3] {1,2,3}; 4 string[] ar = new string[3] {A,B,C}; 5 double[] ar = new double [3] {0.1,0.2,0.03}; 6}

Accessing an array:
To access or to fetch values from an array you can use loops. Best practices to use FOR or FOR-EACH loops.

1 static void Main(string[] args) 2{ 3 int[] ar = new int[3] {1,2,3}; 4 foreach(int i in ar){ 5 Console.Writeline(i);//Displays 1, 2, 3 6 } 7}
Above code access all values from an array by using for-each loop and display them as output.

What is an Array List?


Array List is not strongly type. They are implemented from collection base that is "System.Collection".Array List resizes dynamically it can take any size of values from any data type. It stores a collection of values from different data types or same data types. If the values stored in collection are of different data types then type cast is must. Declaration: Declaration of an Array List is pretty simple first import namespace i.e. "System.Collection". Then call Array-List followed by Array-List object name assign it to Array-List.

1 Using System.Collection; 2 static void Main(string[] args) 3{ 4 ArrayList arrList = new ArrayList(); 5}
To add values to an array-list you can call "Add" method of Array-List to keep adding values continuously as shown below.

1 using System.Collection; 2 static void Main(string[] args) 3{ 4 ArrayList arrList = new ArrayList(); 5 arrList.Add(A);//Added string value 6 arrList.Add(1);//Added integer value 7 arrList.Add(0.05);//Added real value 8}

Accessing an array-list:
To access or to fetch values from an array-list you can use loops. Best practices to use FOR or FOR-EACH loops.

01 static void Main(string[] args) 02 { 03 04 05 06 07 08 09 10 11 } foreach(string stri in arrList){ Console.Writeline(stri);//Displays A, B, C } ArrayList arrList = new ArrayList(); arrList.Add(A);//Added string value arrList.Add(B);//Added string value arrList.Add(C);//Added string value

Above code access all values from an array-list by using for-each loop and display them as output.

Difference between Array-List and Array.



Arrays are strongly typed Array-Lists are not strongly typed. Elements in Arrays have to be of same data type (int, string, double, char, bool). Elements in Array-List can have a combination of combined data types or single data type. Note: If Array-List has combined data types then type cast is must. Arrays are fixed specified length size therefore they cannot be resize dynamically during runtime. Array-List can resize dynamically during runtime

What are Delegates in C# - Step By Step Creating and Using the Delegate

By - Gurunatha Dogi Apr 16th, 2013 Comments : 1 | Views : 2128
1

Definition of Delegate in C#:


Delegates are abstract pointer reference to methods or functions so that you can invoke in very generalize manner. It is also referred to as type safe method pointer. It helps to encapsulate the method name and calls it asynchronously.

Syntax of a Delegate:
1 public delegate void DelegateMethod(); //Example 1 2 OR 3 public delegate int DelegateMath(int x, int y); //Example 2

Note: Declare a delegate object with a signature that exactly matches the method signature that you are trying to encapsulate otherwise delegate will wont work if the delegate signature doesnt match with method.

So exactly matching of methods for both delegates will be.

1 public void MyMethod() 2 //Example 1 3} 4 OR 5 public int Math(int a, int b) 6 return a + b;//Example 2 7}

This is all about the syntax now lets go and try an understand creating and using the delegate in our code

Step By Step Creating and Using the Delegate


There are four steps invloded to create and to use the delegate as shown in below diagram

Using our (D C P I) protocol we will try to understand delegates

Step 1 Declare a Delegate


Before declaring a delegate let's create a class "clsMath" and simple functions as shown in below code.

1 Class claMath

2 3 4 5 6

public int Add(int a, int b) return a + b; } public int Sub(int a, int b) return a - b;

7 } 8}
Since the functions are declared so now let me create my delegate as same signature and same return type as my functions is.

1 Class clsMath<br>

public delegate int DelegateMath(int x, int y);<br>}

As you can see we have declared delegate "DelegateMath" with same return type (int) and same input parameters (int).

Step 2 Create a Delegate reference


1 DelegateMath objDelegateMath = null;
Above code we have created a delegate reference that is "objDelegateMath" and assigned it to "null".

Step 3 Point the reference pointer to methods


1 objDelegateMath = Add;//For Add Function 2 3 objDelegateMath = Sub;//For Sub Function
Now we will create a new function with return type as my delegate I'm doing this because I want to call any one function at a time i.e. "Add" or "Sub" and will use the input pointer to call the any one function and I will also include delegate reference pointer to point both of our functions and return delegate reference with any one function pointed as per the input pointer as shown in below code.

01 public DelegateMath PointtoMath(int getpointer) 02 03 04 05 06 07 08 09 10 11 } DelegateMath objDelegateMath = null; if (getpointer == 1) objDelegateMath = Add; } else objDelegateMath = Sub; } return objDelegateMath;

Step 4 Invoke the methods through delegate


1 objMaths.PointtoMath(1).Invoke(20, 20).ToString()//Outputs 40

To invoke the methods through the delegate we have created a main class of a program with main method in console application and inside the main class created the object for class "clsMath" and taken inputs for operation i.e. for "Add" or "Sub" and inputs for two numbers. Finally we invoke the methods through a delegate by passing parameters to it as shown in below snippet of code.

01 static void Main(string[] args) 02 03 04 05 06 07 08 09 10 11 12 13 14 15 }


Conclusion or Display Output Result

clsMaths objMaths = new clsMaths(); Console.WriteLine("Please enter the operation code 1 for addition and 2 for substraction"); int opt = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nPlease enter the first number"); int num1 = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nPlease enter the second number"); int num2 = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("\nOutput answer is --> 0}",objMaths.PointtoMath(opt).Invoke(num1, num2).ToString());

Hey friends if you have any doubts or queries kindly let me know through you valuable comments thank you.

Sealed Classes in C# with an example and explanation



By - Gurunatha Dogi Jun 25th, 2013 Comments : 6 | Views : 2051
5

About Sealed Modifier ?


Sealed modifier restricts the inheritance feature of an object oriented programming means sealed classes cannot be inherited or implemented further to any classes. To implement sealed classes in programs we need to use "sealed" keyword before the "class" keyword.

Why Sealed Modifier ?


We use sealed modifiers in our programs because: Sealed Modifier prevents a class from being inherited. When sealed modifier is applied to a method then it prevents a method from being overloaded in further classes.

1 sealed class Employee ///Sealed class 2{ 3 4} Public sealed void Display() { } /// Sealed Method

Example of Sealed Modifier:


1 sealed class Employee 2{ 3 public void DisplayEmployeeDetails(){ 4 Console.WriteLine("Employee Name --> Questpond and Employee Code --> 009"); 5 6}
In

}
above the code we have class "Employee"with "sealed" type modifier and simple

"DisplayEmployeeDetails" method which displays employee details. Now let's perform test on sealed type modifier. To perform this test we will create a main program of console with static main method. Now let's try to inherit the "Employee" class to our main program as shown in below code.

As you can see when we tried to build the application we got following error which states that we cannot derive from sealed type member but we can aggregate the "Employee" class with sealed type modifier in our main method as shown in below code.

So when we try to call "DisplayEmployeeDetails" method through the object "objEmp" we could find "DisplayEmployeeDetails" method as shown in above code. Conclusion: Sealed member prevent the class for further inheriting. So this all about sealed modifiers in c-sharp. If you have any doubts or queries regarding sealed modifiers then kindly let me know through your valuable comments and if you like this article information then share this with your friends and colleagues. Thank You

Ref : http://www.onlinebuff.com/search.php?query_keyword=Generics&btnsrch.x=17&btnsrch.y=8

Você também pode gostar