Escolar Documentos
Profissional Documentos
Cultura Documentos
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[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.
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.
What are Delegates in C# - Step By Step Creating and Using the Delegate
By - Gurunatha Dogi Apr 16th, 2013 Comments : 1 | Views : 2128
1
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.
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
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>
As you can see we have declared delegate "DelegateMath" with same return type (int) and same input parameters (int).
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;
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.
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.
1 sealed class Employee ///Sealed class 2{ 3 4} Public sealed void Display() { } /// Sealed Method
}
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