Monday, November 22, 2010

Delegate

A delegate object encapsulates a reference to a method.

Indexers : Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

An indexer is a member that enables an object to be indexed in the same way as an array.

Indexer
Allows methods on an object to be called as though the object is an array.
Accessed through an index.
Must be an instance member.
A get accessor of an indexer has the same formal parameter list as the indexer.
A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.
class IndexerImplemenationStack{


            {
            private string[] range = new string[5];            public string this[int index]               get                 {

                 }
                    return range[index];              set                  {
                     range[index] =
                  }
              }
}

Ind[0] =
Ind[1] =

{

}
IndexerImplemenationStack Ind= new IndexerImplemenationStack();"sachin";"Rastogi";for (int i = 0; i< 2; i++)       Console.WriteLine(Ind[i]);
value;
Property
Allows methods to be called as though they were public data members.
Accessed through a simple name.
Can be a static or an instance member.
A get accessor of a property has no parameters.
A set accessor of a property contains the implicit value parameter.

5 comments:

  1. Serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.

    Serialization is the process of converting complex objects into stream of bytes for storage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form. The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serialization namespace. The ISerializable interface allows you to make any class Serializable.

    Here are the following steps that we are going to do to create a serializable class and test it.

    Create a custom class named Employee and assign properties.
    Define the serialization functions.
    Create a main class and instantiate our Employee class.
    Serialize the object to a sample file.
    Deserialize the values by reading it from the file.

    sing System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;

    namespace MyObjSerial
    {
    [Serializable()] //Set this attribute to all the classes that want to serialize

    public class Employee : ISerializable //derive your class from ISerializable

    {
    public int EmpId;
    public string EmpName;

    //Default constructor

    public Employee()
    {
    EmpId = 0;
    EmpName = null;
    }
    }
    }
    public class ObjSerial
    {
    public static void Main(String[] args)
    {
    //Create a new Employee object

    Employee mp = new Employee();
    mp.EmpId = 10;
    mp.EmpName = "Omkumar";

    //Add code below for serialization

    }
    }
    using System;
    using System.IO;
    using System.Xml;
    using System.Drawing;
    using System.Reflection;
    using System.Xml.Serialization;
    public abstract class XmlSerializable
    {
    public virtual void Save(string path)
    {
    // Code omitted
    }

    public virtual void Load(string path)
    {
    // Code omitted
    }
    }
    public class Settings : XmlSerializable
    {
    private int mSetting1;
    public int Setting1
    {
    get { return mSetting1; }
    set { mSetting1 = value; }
    }

    private string mSetting2 = "";
    public string mSetting2
    {
    get { return mSetting2; }
    set { mSetting2 = value; }
    }
    }
    public virtual void Save(string path)
    {
    StreamWriter w = new StreamWriter(path);
    XmlSerializer s = new XmlSerializer(this.GetType());
    s.Serialize(w, this);
    w.Close();
    }

    XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information. For example, if you have a Book object that exists in the Library namespace, there is no guarantee that it will be deserialized into an object of the same type.

    ReplyDelete
  2. using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    public void serialize(string fileName)
    {
    FileStream s = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
    BinaryFormatter B = new BinaryFormatter();
    B.Serialize(s, this);
    s.Close();
    }

    ReplyDelete
  3. Here first we create an instance of FileStream class which, will create file and open it in read and write mode. Next we create an instance of BinaryForamtter class. This class have Seralize method which take parameters FileStream and Instance which you want to serialize.

    ReplyDelete
  4. DeSerializing the object
    public Articles deSerialize(string fileName)
    {
    FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    BinaryFormatter F = new BinaryFormatter();
    Articles s1 = (Articles)F.Deserialize(Fs);
    Fs.Close();
    return s1;
    }

    ReplyDelete
  5. 8 Char Password

    using System;
    using System.Security.Cryptography;
    public class RandomPassword
    {
    // Define default min and max password lengths.
    private static int DEFAULT_MIN_PASSWORD_LENGTH = 8;
    private static int DEFAULT_MAX_PASSWORD_LENGTH = 10;

    // Define supported password characters divided into groups.
    // You can add (or remove) characters to (from) these groups.
    private static string PASSWORD_CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
    private static string PASSWORD_CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
    private static string PASSWORD_CHARS_NUMERIC= "23456789";
    private static string PASSWORD_CHARS_SPECIAL= "*$-+?_&=!%{}/";
    public static string Generate()
    {
    return Generate(DEFAULT_MIN_PASSWORD_LENGTH,
    DEFAULT_MAX_PASSWORD_LENGTH);
    }

    public static string Generate(int length)
    {
    return Generate(length, length);
    }

    public static string Generate(int minLength,
    int maxLength)
    {
    // Make sure that input parameters are valid.
    if (minLength <= 0 || maxLength <= 0 || minLength > maxLength)
    return null;

    // Create a local array containing supported password characters
    // grouped by types. You can remove character groups from this
    // array, but doing so will weaken the password strength.
    char[][] charGroups = new char[][]
    {
    PASSWORD_CHARS_LCASE.ToCharArray(),
    PASSWORD_CHARS_UCASE.ToCharArray(),
    PASSWORD_CHARS_NUMERIC.ToCharArray(),
    PASSWORD_CHARS_SPECIAL.ToCharArray()
    };

    int[] charsLeftInGroup = new int[charGroups.Length];

    // Initially, all characters in each group are not used.
    for (int i=0; i
    /// Illustrates the use of the RandomPassword class.
    ///
    public class RandomPasswordTest
    {
    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main(string[] args)
    {
    // Print 100 randomly generated passwords (8-to-10 char long).
    for (int i=0; i<100; i++)
    Console.WriteLine(RandomPassword.Generate(8, 10));
    }
    }

    ReplyDelete