Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » General »
Keywords in C# .Net
|
Dear Friends,
Let us have a look at some of the important keywords in c#.
1. foreach Keyword: foreach is used to iterate through a collection. You can simply define a variable to hold the individual elements in the collection and then use it whatever way you want. Example: int[] iArray = new int[5]; //Assign iArray with some values foreach(int iLoop in iArray) { //Print the values MessageBox.Show(iLoop.ToString()); }
2. base Keyword: The base keyword is used to access base class members or methods within a derived class. Use base. to call the base class. Base does not work for interfaces. You need to call the interface name that you want to use. Base class cannot be called from within a static method.
3. this Keyword: The this keyword is used to refer to the current object in a method, can be used to differentiate between an input argument to a method and a member variable. Example: public Student(string name, string RollNo) { this.name = name; this.RollNo = RollNo; }
4. out Keyword: Arguments returned to the caller are out arguments. The out keyword causes arguments to be passed by reference. This is similar to the ref keyword except for the fact that ref variables need to be initialized. Both the calling and the called method must explicitly use the 'Out' keyword. Although variables passed as an out arguments need not be initialized prior to being passed, the calling method requires to assign it a value before returning it back.
5. abstract Keyword: The abstract keyword indicates that the class can only be used as a base class and is not instantiable.
6. event Keyword: The event keyword allows you to define events for the class. Event can be said to be a change that can be triggered and caught asynchronously in another method or class, to make you aware that a change occured in an object. As we all see some of the default events called like button click event. So, whenever that particular button is clicked, an event is fired and we do required coding. We can create our own events like if a User control is clicked then we can write an event that lets you know that it was clicked.
7. checked Keyword: The checked keyword is used to check for underflows or overflows in a given numeric expression.
8. unchecked Keyword: The unchecked keyword is used to check for overflows for integral type arithmetic operations and conversions.
9. extern Keyword: The extern keyword is used to define and declare a method, which is implemented externally i.e. in some other C# application.This keyword can also be used to define an alias for an external assembly. It is generally used with the DLLImport attribute while using the Interop services to call the unmanaged code. The method that is implemented externally must be declared static. Example: [DllImport("myAssembly.dll")] public static extern int MessageBox(int iVar1, string str1, string str2, int iVar2);
10. lock Keyword: The lock keyword is used for synchronization in threaded applications. A lock statement implements a critical section for that block of code.The next thread executes only when that block has exited.
11. readonly Keyword: The readonly keyword should be used when the value needs to be initialized once during runtime and then it never needs to be changed. Its value can be set either during class declaration or in the constructor of the class. Read-only variables are preferred over constants for situations where a field should have different values for different objects of the same class, but want the value of the field to remain same for the complete life span of that object.
12. sealed Keyword: The sealed keyword is used to indicate that the class cannot be used as a base class.
13. volatile Keyword: The volatile keyword specifies that multiple threads, which are running concurrently, can modify a variable. It ensures that a thread retrieves the latest value of the variable even if it was modified by another thread. The system returns the current value of the volatile variable to the method immediately whenever requested. Cheers!
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|