Posts

Showing posts from September, 2017

Why 'this' keyword cannot be used in a static method

this is used to refer to the parent object of a variable or method. When you declare static  on a method the method can be called without needing to instantiate an object of the class. Therefore the  this keyword  is not allowed because your  static  method is not associated with any objects.

Indexer in C#

An  indexer  allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a  virtual array . You can then access the instance of this class using the array access operator ([ ]). In other words it simplifies the way we access the collection. using System ; namespace IndexerApplication { class IndexedNames { private string [] namelist = new string [ size ]; static public int size = 10 ; public IndexedNames () { for ( int i = 0 ; i < size ; i ++) namelist [ i ] = "N. A." ; } public string this [ int index ] { get { string tmp ; if ( index >= 0 && index <= size - 1 ) { tmp = namelist [ index ]; } else { tmp = "" ; } ...

Delegates and Events in C#

Delegates A delegate in C# is similar to a function pointer in C or C++.   A delegate is like a pointer to a function. It is a reference type data type and it holds the reference of a method. All the delegates are implicitly derived from  System.Delegate  class. U sing a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegates are two types       -     Single Cast Delegates       -    Multi Cast Delegates Single Cast Delegates Single cast delegate means which hold address of single method. using System ; delegate int NumberChanger ( int n ); namespace DelegateAppl { class TestDelegate { static int num = 10 ; public static int AddNum ( int p ) { ...