Skip to main content

Posts

Showing posts with the label c#

Multicast Delegates :: Advance : C# programming for c++ programmers

Defination: A Multicast delegate is a delegate that has references to more than one function. When you call a multicast delegate, all the functions the delegate is pointing to, are invoked. Example 1: using System; namespace demo {     public delegate void demoDelegate();          public class demoSample     {         static void Main()         {             demoDelegate del1 = new demoDelegate(demoMethodOne);             demoDelegate del2 = new demoDelegate(demoMethodTwo);             demoDelegate del3 = new demoDelegate(demoMethodThree);                          demoDelegate del4 = del1 + del2 + del3 - del3;                          del4();  // del4 ...

Delegates :: Advance : C# programming for c++ programmers

1. Definition:  A Delegate is a type safe function pointers and it  hold   reference (i.e. Pointer) to a function.  Delegates are like reference types like classes and interfaces. Unlike structures are value type. The reasont o say delegate as type safe because  delegate must match the signature of the function, the delegate points to, otherwise you get a compiler error. Syntax:  Same a member function with a keyword delegate public delegate void func(string param); class demo {     public static void Main()     {         // ignature must match the signature of the delegate         func del = new func(method);         // Invoke the delegate, which will invoke the method         del("Hello from Delegte");     }     public static void method(string param)     {         Co...

indexers :: Advance : C# programming for c++ programmers

1. Where are indexers used in .NET : To store or retrieve data from session state or application state variables, we make use indexers. Session["sessiondata"] = "sessiondata"; 2. Defination : An indexer allows an object to be indexed such as an array Syntax : A one or multi param indexer has the following syntax: element-type this[int index, ..] {    // The get accessor.    get    {       // return the value specified by index    }        // The set accessor.    set    {       // set the value specified by index    } } Points to remember: 1. We then created an indexer using "this" keyword. This indexer takes employeeId as parameter and returns employee name. public string this[int employeeId] 2. Just like properties indexers have get and set accessors. 3. Indexers can also be overloaded.  using System; using System.Collectio...

Reflection::Advance : C# programming for c++ programmers

Reflection Reflection is the ability of inspecting an assemblie's metadata at runtime.   It is used to find all types in an assembly and/or dynamically invoke methods in an assembly.  This includes information about the type, properties, methods, and events of an object. With Reflection, we can dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.There are several uses of reflection. 1. When you drag and drop a button on a win forms or an asp.net application. The properties window uses reflection to show all the properties of the Button class. So,reflection is extensivley used by IDE or a UI designers. 2. Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don't have any information at compile time. So, reflection enables you to use code that is not available a...

Attributes::Advance : C# programming for c++ programmers

1. C# - Attributes: Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection. Atrributes can be applied on class, methods, assemblies etc. You You can add declarative information to a program by using an attribute. A declarative tag is depicted by square ([ ]) brackets placed above the element it is used for. A few pre-defined attributes with in the .NET framework. Obsolete - Marks types and type members outdated WebMethod - To expose a method as an XML Web service method Serializable - Indicates that a class can be serialized Predefined Types Of Atrributes 1). AttributeUsage 2). Conditional 3). Obsolete 1). AttributeUsage:       It specifies the types of items to which the attribute can be applied.       Syntax for specifying this attribute is as follows:       [AttributeUsage(         validon,   //The parame...

Properties::Advance : C# programming for c++ programmers

Properties: Usually inside a class we provide function GET and SET, similarly C# provides a built in mechanism called properties. Example :  class MyClass {       private int x;       public int X       { get {    return x; } set {    x = value; } } } After .net 3.0 class MyClass {             public int X{get;set;}  // Auto generates private feild with the name of property. } Note: Static Properties: C# also supports static properties.set/get accessor of static property can access only other static members of the class. Also static properties are invoking by using the class name. Properties & Inheritance: The properties of a Base class can be inherited to a Derived class. Properties & Polymorphism: A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, over...

Basics : C# programming for c++ programmers

1. Data Conversion:              Implicit: Implicit conversion is done by the compiler: When there is no loss of information if the conversion is done  If there is no possibility of throwing exceptions during the conversion. Example:  Converting an int to a float will not loose any data and no exception will be thrown, hence an implicit conversion can be done.  Explicit: Where as when converting a float to an int, we loose the fractional part and also a possibility of overflow exception. Hence, in this case an explicit conversion is required. For explicit conversion we can use cast operator or the convert class in c#. Example:   int i = (int)f;   float f = 100.25F; Difference Parse() and TryParse()  1. Parse() method throws an exception if it cannot parse the value, whereas TryParse() returns        a bool indicating whether it succeeded or failed. 2. Use Parse() i...