Wednesday, April 21, 2010

STRUCTURES



In some situations all you really need is a small data structure. In this case, a class provides more functionality than you need, and for performance reasons you will probably prefer to use a struct. Look at this example:
class Dimensions
{
public double Length;
public double Width;
}
This code defines a class called Dimensions, which simply stores the length and width of some item. Perhaps you're writing a furniture-arranging program to let people experiment with rearranging their furniture on the computer and you want to store the dimensions of each item of furniture. It looks like you're breaking the rules of good program design by making the fields public, but the point is that you don't really need all the facilities of a class for this at all. All you have is two numbers, which you'll find convenient to treat as a pair rather than individually. There is no need for lots of methods, or for you to be able to inherit from the class, and you certainly don't want to have the .NET runtime go to the trouble of bringing in the heap with all the performance implications, just to store two doubles.
As mentioned earlier in this chapter, the only thing you need to change in the code to define a type as a struct instead of a class is to replace the keyword class with struct:
struct Dimensions
{
public double Length;
public double Width;
}
Defining functions for structs is also exactly the same as defining them for classes. The following code demonstrates a constructor and a property for a struct:
struct Dimensions
{
public double Length;
public double Width;
Dimensions(double length, double width)

{
 Length=length;
 Width=width;
}
}
In many ways, you can think of structs in C# as being like scaled-down classes. They are basically the same as classes, but designed more for cases where you simply want to group some data together. They differ from classes in the following ways:
  • Structs are value types, not reference types. This means they are stored either in the stack or inline (if they are part of another object that is stored on the heap) and have the same lifetime restrictions as the simple data types.
  • Structs do not support inheritance.
  • There are some differences in the way constructors work for structs. In particular, the compiler always supplies a default no-parameter constructor, which you are not permitted to replace.
  • With a struct, you can specify how the fields are to be laid out in  
Because structs are really intended to group data items together, you'll sometimes find that most or all of their fields are declared as public. This is, strictly speaking, contrary to the guidelines for writing .NET code — according to Microsoft, fields (other than const fields) should always be private and wrapped by public properties. However, for simple structs, many developers would nevertheless consider public fields to be acceptable programming practice.
Note
C++ developers beware; structs in C# are very different from classes in their implementation. This is very different than the situation in C++, for which classes and structs are virtually the same thing.
The following sections look at some of these differences in more detail.
Structs Are Value Types
Although structs are value types, you can often treat them syntactically in the same way as classes. For example, with the definition of the Dimensions class in the previous section, you could write:
Dimensions point = new Dimensions();
point.Length = 3;
point.Width = 6;
Note that because structs are value types, the new operator does not work in the same way as it does for classes and other reference types. Instead of allocating memory on the heap, the new operator simply calls the appropriate constructor, according to the parameters passed to it, initializing all fields. Indeed, for structs it is perfectly legal to write:
Dimensions point;
point.Length = 3;
point.Width = 6;
If Dimensions was a class, this would produce a compilation error, because point would contain an uninitialized reference — an address that points nowhere, so you could not start setting values to its fields. For a struct, however, the variable declaration actually allocates space on the stack for the entire struct, so it's ready to assign values to. Note, however, that the following code would cause a compilation error, with the compiler complaining that you are using an uninitialized variable:
Dimensions point;
Double D = point.Length;
Structs follow the same rules as any other data type: everything must be initialized before use. A struct is considered fully initialized either when the new operator has been called against it, or when values have been individually assigned to all its fields. And of course, a struct defined as a member field of a class is initialized by being zeroed-out automatically when the containing object is initialized.
The fact that structs are value types will affect performance, though depending on how you use your struct, this can be good or bad. On the positive side, allocating memory for structs is very fast because this takes place inline or on the stack. The same goes for removing structs when they go out of scope. On the other hand, whenever you pass a struct as a parameter or assign a struct to another struct (as in A=B, where A and B are structs), the full contents of the struct are copied, whereas for a class only the reference is copied. This will result in a performance loss that depends on the size of the struct — this should emphasize the fact that structs are really intended for small data structures. Note, however, that when passing a struct as a parameter to a method, you can avoid this performance loss by passing it as a ref parameter — in this case only the address in memory of the struct will be passed in, which is just as fast as passing in a class. On the other hand, if you do this, you'll have to be aware that it means the called method can in principle change the value of the struct.
Structs and Inheritance
Structs are not designed for inheritance. This means that it is not possible to inherit from a struct. The only exception to this is that structs, in common with every other type in C#, derive ultimately from the class System.Object. Hence, structs also have access to the methods of System.Object, and it is even possible to override them in structs — an obvious example would be overriding the ToString() method. The actual inheritance chain for structs is that each struct derives from a class, System.ValueType, which in turn derives from System.Object. ValueType does not add any new members to Object, but provides implementations of some of them that are more suitable for structs. Note that you cannot supply a different base class for a struct: Every struct is derived from ValueType.
Constructors for Structs
You can define constructors for structs in exactly the same way that you can for classes, except that you are not permitted to define a constructor that takes no parameters. This may seem nonsensical, and the reason is buried in the implementation of the .NET runtime. Some rare circumstances exist in which the.NET runtime would not be able to call a custom zero-parameter constructor that you have supplied. Microsoft has therefore taken the easy way out and banned zero-parameter constructors for structs in C#.
That said, the default constructor, which initializes all fields to zero values, is always present implicitly, even if you supply other constructors that take parameters. It's also impossible to circumvent the default constructor by supplying initial values for fields. The following code will cause a compile-time error:
struct Dimensions
{
public double Length = 1            // error. Initial values not allowed
public double Width = 2             // error. Initial values not allowed
Of course, if Dimensions had been declared as a class, this code would have compiled without any problems.
Incidentally, you can supply a Close() or Dispose() method for a struct in the same way you do for a class.
Example program
using System;
struct student
    {        
        public string name;
        public int rollnumber;
        public int marks;
        public student(string n, int r, int m)
        {
            name = n;
            marks = m;
            rollnumber = r;
        }
}
class Program
    {
        static void Main(string[] args)
        {
            student st = new student("Ramesh",1,85);
            Console.WriteLine("Marks are",st.marks);          
           
        }
    }
Nested Structures
C# permits declaration of structs nested inside other structs.The following code is valid
using System;
struct student
    {
        
        public string name;
        public int rollnumber;
        public int marks;
       public student(string n, int r, int m)
        {
            name = n;
            marks = m;
            rollnumber = r;
        }
     public struct Ramesh
      {
         public string address;
                 
      }
        
   
    }
    class Program
    {
        static void Main(string[] args)
        {
            student st = new student("Nikhil",13,78);
            student.Ramesh ob;
            ob.address = "Meerut";
            Console.WriteLine("Ramesh's address:{0}",ob.address);
            Console.WriteLine("Nikhil's Marks:{0}", st.marks);
           
        }
    }
We can also use struct variables as members of another struct.This implies nesting of references to structs.
using System;
struct A
    {
        
        public int x;
    }
    
struct B
      {
         public A a;//object of A
         public int y;
                 
      }        
   
    class Program
    {
        static void Main(string[] args)
        {
             B ob;
             ob.a.x=100;//’x’ is a member of ‘a’ and ‘a’ is a member of ‘ob’
             ob.y=400;  
           
        }
    }

Thursday, April 15, 2010

Indexer


As you know, array indexing is performed using the [ ] operator. It is possible to overload the [ ] operator for classes that you create, but you don’t use an operator method. Instead, you create an indexer. An indexer allows an object to be indexed like an array. The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints.
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
  }
}
Here, element-type is the base type of the indexer. Thus, each element accessed by the indexer will be of type element-type. The element type corresponds to the base type of an array. The parameter index receives the index of the element being accessed. Technically, this parameter does not have to be of type int, but since indexers are typically used to provide array indexing, an integer type is customary.
Inside the body of the indexer are defined two accessors that are called get and set. An accessor is similar to a method except that it does not have a return type or parameter declarations. The accessors are automatically called when the indexer is used, and both accessors receive index as a parameter. If the indexer is on the left side of an assignment statement, then the set accessor is called, and the element specified by index must be set. Otherwise, the get accessor is called, and the value associated with index must be returned. The set accessor also receives an implicit parameter called value, which contains the value being assigned to the specified index.
} 

For Example:

using System;
using System.Collections.Generic;
using System.Text;

namespace Indexers
{
    class ParentClass
    {
        private string[] range = new string[5];
        public string this[int indexrange]
        {
            get
            {
                return range[indexrange];
            }
            set
            {
                range[indexrange] = value;
            }
        }
    }

    /* The Above Class just act as array declaration using this pointer */

    class childclass
    {
        public static void Main()
        {
            ParentClass obj = new ParentClass();

            /* The Above Class ParentClass  create one object name is obj */

            obj[0] = "ONE";
            obj[1] = "TWO";
            obj[2] = "THREE";
            obj[3] = "FOUR ";
            obj[4] = "FIVE";
            Console.WriteLine("WELCOME TO YMCA \n");
            Console.WriteLine("\n");

            Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);
            Console.WriteLine("\n");
            Console.WriteLine("ADITYA TYAGI\n");
            Console.WriteLine("\n");
            Console.ReadLine();
        }
    }
}

Indexers Do Not Require an Underlying Array

It is important to understand that there is no requirement that an indexer actually operate on an array. It simply must provide functionality that appears “array-like” to the user of the indexer. For example, the following program has an indexer that acts like a read-only array that contains the powers of 2 from 0 to 15. Notice, however, that no actual array exists. Instead, the indexer simply computes the proper value for a given index.
// Indexers don't have to operate on actual arrays.
 
using System;
 
class PwrOfTwo {
 
  /* Access a logical array that contains
     the powers of 2 from 0 to 15. */
  public int this[int index] {
    // Compute and return power of 2.
    get {
      if((index >= 0) && (index < 16)) return pwr(index);
      else return -1;
    }
 
    // there is no set accessor
  }
 
  int pwr(int p) {
    int result = 1;
    for(int i=0; i < p; i++)
      result *= 2;
 
    return result;
  }
}
 
class UsePwrOfTwo {
  public static void Main() {
    PwrOfTwo pwr = new PwrOfTwo();
 
    Console.Write("First 8 powers of 2: ");
    for(int i=0; i < 8; i++)
      Console.Write(pwr[i] + " ");
    Console.WriteLine();
 
    Console.Write("Here are some errors: ");
    Console.Write(pwr[-1] + " " + pwr[17]);
 
    Console.WriteLine();
  }
}
The output from the program is shown here:
First 8 powers of 2: 1 2 4 8 16 32 64 128
Here are some errors: -1 -1
Notice that the indexer for PwrOfTwo includes a get accessor, but no set accessor. As explained, this means that the indexer is read-only. Thus, a PwrOfTwo object can be used on the right side of an assignment statement, but not on the left. For example, attempting to add this statement to the preceding program won’t work:
pwr[0] = 11; // won't compile
This statement will cause a compilation error because there is no set accessor defined for the indexer.
There are two important restrictions to using indexers. First, because an indexer does not define a storage location, a value produced by an indexer cannot be passed as a ref or out parameter to a method. Second, an indexer must be an instance member of its class; it cannot be declared static.

Wednesday, April 14, 2010

Abstract,Static and Sealed Classes


Abstract classes
The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only as a base class. An abstract class differs from a non-abstract class in the following ways:
 • An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types.
 • An abstract class is permitted (but not required) to contain abstract members.
 • An abstract class cannot be sealed.

 When a non-abstract class is derived from an abstract class, the non-abstract class shall include actual  implementations of all inherited abstract members, thereby overriding those abstract members. [Example: In the following code
 abstract class A
 {
 public abstract void F();
 }
 abstract class B: A
 {
 public void G() {}
 }
 class C: B
 {
 public override void F() {
 // actual implementation of F
 }
 }
 the abstract class A introduces an abstract method F. Class B introduces an additional method G, but since it doesn’t provide an implementation of F, B shall also be declared abstract. Class C overrides F and provides  an actual implementation. Since there are no abstract members in C, C is permitted (but not required) to be  non-abstract. end example]
 If one or more parts of a partial type declaration of a class include the abstract modifier, the class is abstract. Otherwise, the class is non-abstract.

 Sealed Classes
As powerful and useful as inheritance is, sometimes you will want to prevent it. For example, you might have a class that encapsulates the initialization sequence of some specialized hardware device, such as a medical monitor. In this case, you don’t want users of your class to be able to change the way the monitor is initialized, possibly setting the device incorrectly. Whatever the reason, in C# it is easy to prevent a class from being inherited by using the keyword sealed.
To prevent a class from being inherited, precede its declaration with sealed. As you might expect, it is illegal to declare a class as both abstract and sealed because an abstract class is incomplete by itself and relies upon its derived classes to provide complete implementations.
Here is an example of a sealed class:
sealed class A {
  // ...
}

// The following class is illegal.
class B : A { // ERROR! Can't derive class A
  // ...
}
As the comments imply, it is illegal for B to inherit A because A is declared as sealed.
Sealed Methods
When an instance method includes the sealed modifier, the method is said to be a sealed method,it means a derived class can not override this method.
A sealed method is used to override an inherited virtual method with the same signature.That means the sealed modifier is always used in combination with the override modifier.
Example
Class A
{ public virtual void show()
{
}
}
Class B:A
{ public sealed override void show()
{
}
}
Any derived class of B can not further override the method show().

 Static classes
 When a class declaration includes a static modifier, the class being declared is said to be a static class.
A static class declaration is subject to the following restrictions:
 • A static class shall not include a sealed or abstract modifier. (However, since a static class cannot be instantiated or derived from, it behaves as if it were both sealed and abstract.)
 • A static class shall not include a class-base specification and cannot explicitly specify a base
 class or a list of implemented interfaces. A static class implicitly inherits from type object.
 • A static class shall not contain any operators.
 • A static class shall not have members with protected or protected internal declared
 accessibility.
 • A static class shall only contain static members. [Note: constants and nested types are
 classified as static members. end note]
Example:
using System;

    static class A
    {
        public static void aMethod()
        {
            Console.WriteLine("Static class");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A.aMethod();
        }
    }

An object of type A is not needed to call the aMethod(). The type name has been used to make the call.

The members of a static class are not implicitly made static; except for constants and nested types, member declarations that are intended to be static shall explicitly include a static modifier.

Friday, March 19, 2010

String


MANIPULATING STRINGS
Strings
The String  class provide strings functionality in .NET. 
 Understanding the String Class

The String class implements the functionality of string data type. String class is defined in the System namespace. Here are few important things about the String class before we proceed:
  1. An instance of String is immutable, means once an instance is created, it cannot be modified. There are methods, which returns a new String to return a new sub string from it.
  2. Index of String is zero based. Means the first character of the index is 0th element of the String and last character of the String is total number of elements in the string - 1.
  3. By default a String is an empty string (""). So you don't have to initialize it as you used to do in C++ or other languages. An empty string is greater than the null value.
String class Members

String class provides two properties - Chars and Length. The Chars property returns the character at a specified position in a string and the Length property returns the number of characters in a string.

String class provides two types of methods - static and instance. Static methods can only be used by String class, not by an instance of the String class. The instance methods are used by instances of the String class.
Static Methods of String/string
Compare: Compares the values of two strings.

Concat: Concatenates (joins) two or more strings into one string.

Copy: Creates a new string from an existing string.

Equals :Compares two strings to determine whether they contain the same value. Returns true if the two values are equal; otherwise, returns false.

Join :Concatenates two or more strings. A specified “separator string” is placed between each of the original strings.


Methods and Properties of Each Instance

Clone: Returns a copy of the stored string.

CompareTo: Compares the value of this string with the value of another string. Returns a negative number if this string is less than the compared string, 0 if equal, and a positive number if the value of this string is greater.

CopyTo: Copies a portion of or all of a string to a new string or character array.

EndsWith: Determines whether the end of the value stored in the string is equal to a
string value. If they are equal, true is returned; otherwise, false is returned.
Equals Compares two strings to determine whether they contain the same value.
Returns true if the two values are equal; otherwise, returns false.

IndexOf: Returns the index (location) of the first match for a character or string. Returns
-1 if the value is not found.

Insert :Inserts a value into a string. This is done by returning a new string.

LastIndexOf: Returns the index (location) of the last match for a character or a string.
Returns -1 if the value is not found.

Length: Returns the length of the value stored in the string. The length is equal to the
number of characters contained.

PadLeft: Right-justifies the value of a string and then pads any remaining spaces with a
specified character (or space).

PadRight: Left-justifies the value of a string and then pads any remaining spaces with a
specified character (or space).

Remove: Deletes a specified number of characters from a specified location within a
string.

Split: The opposite of Join. Breaks a string into substrings based on a specified value. The specified value is used as a breaking point.

StartsWith: Determines whether the value stored in a string starts with a specified character
or set of characters. Returns true if there is a match and false if not. If
specified character is null, true is also returned.

Substring: Returns a substring from the original string starting at a specified location. The
number of characters for the substring might also be specified but is not required.

ToCharArray: Copies the characters in the current string value to a char array.

ToLower: Returns a copy of the current value in all lowercase letters.

ToUpper: Returns a copy of the current value in all uppercase characters.

Trim: Removes copies of a specified string from the beginning and end of the current
string.

TrimEnd: Removes copies of a specified string from the end of the current string.

TrimStart: Removes copies of a specified string from the beginning of the current string.

Comparing Strings

The Compare method compares two strings and returns an integer value. The return value of Compare method can be less than zero, greater than zero or equals to zero
 The following code compares two strings and return results on the System console.

// Comparing two strings
//====================================
string str1 = "ppp";
string str2 = "ccc";
int res = String.Compare(str1, str2);
Console.WriteLine("First result:" +res.ToString());
str2 = "ttt"; res = String.Compare(str1, str2);
Console.WriteLine("Second result:" +res.ToString());
str1 = "ttt"; res = String.Compare(str1, str2);
Console.WriteLine("Third result:" +res.ToString));
//====================================

The CompareTo method is an instance method. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method. The following source code compares two strings.

// CompareTo Method
string str = "kkk";
Console.WriteLine( str.CompareTo(str1) );

Copy and Concatenating Strings

The Concat method adds strings (or objects) and returns a new string. Using Concat method, you can add two strings, two objects and one string and one object or more combinations of these two.

The following source code concatenate two strings.

string str1 = "ppp";
string str2 = "ccc";
string strRes = String.Concat(str1, str2);
Console.WriteLine(strRes);

The following source code concatenates one string and one object.

object obj = (object)12;
strRes = String.Concat(str1, obj);
Console.WriteLine(strRes);

The Copy method copies contents of a string to another. The Copy method takes a string as input and returns another string with the same contents as the input string. For example, the following code copies str1 to strRes.

string str1 = "ppp";
string str2 = "ccc";
string strRes = String.Copy(str1);
Console.WriteLine("Copy result :" + strRes);

The CopyTo method copies a specified number of characters from a specified position in this instance to a specified position in an array of characters. For example, the following example copies contents of str1 to an array of characters. You can also specify the starting character of a string and number of characters you want to copy to the array.

string str1 = "ppp";
char[] chrs = new Char[2];
str1.CopyTo(0, chrs, 0, 2);

The Clone method returns a new copy of a string in form of object. The following code creates a clone of str1.

string str1 = "ppp";
object objClone = str1.Clone();
Console.WriteLine("Clone :"+objClone.ToString());

The Join method is useful when you need to insert a separator (String) between each element of a string array, yielding a single concatenated string. For example, the following sample inserts a comma and space (", ") between each element of an array of strings.

string str1 = "ppp";
string str2 = "ccc";
string str3 = "kkk";
string[] allStr = new String[]{str1, str2, str3};
string strRes = String.Join(", ", allStr);
Console.WriteLine("Join Results: "+ strRes);

Adding, Removing and Replacing Strings

The Insert method inserts a specified string at a specified index position in an instance. For example, the following source code inserts "bbb" after second character in str1 and the result string is "pbbbpp".

string str1 = "ppp";
string strRes = str1.Insert(2, "bbb");
Console.WriteLine(strRes.ToString());

The Remove method deletes a specified number of characters from a specified position in a string. This method returns result as a string. For example, the following code removes three characters from index 3.

string s = "123abc000";
Console.WriteLine(s.Remove(3, 3));

The Replace method replaces all occurrences of a specified character in a string. For example, the following source code replaces all p character instances of str1 with character l and returns string "lll".

string str1 = "ppp";
string repStr = str1.Replace('p', 'l');
Console.WriteLine("Replaced string:"+ repStr.ToString() );

The Split method separates strings by a specified set of characters and places these strings into an array of strings. For example, the following source code splits strArray based on ',' and stores all separated strings in an array.

string str1 = "ppp";
string str2 = "ccc";
string str3 = "kkk";
string strAll3 = str1 + ", " +str2+", "+str3 ;
string[] strArray = strAll3.Split(',');
foreach (string itm in strArray)
{
Console.WriteLine(itm.ToString() );
}

Uppercase and Lowercase
The ToUpper and ToLower methods convert a string in uppercase and lowercase respectively. These methods are easy to use. The following code shows how to use ToUppler and ToLower methods.

string aStr = "adgas";
string bStr = "ABNMDWER";
string strRes = aStr.ToUpper();
Console.WriteLine("Uppercase:"+ strRes.ToString());
strRes = bStr.ToLower();
Console.WriteLine("Lowercase:"+ strRes.ToString());
Formatting Strings

You can use the Format method to create formatted strings and concatenate multiple strings representing multiple objects. The Format method automatically converts any passed object into a string.

For example, the following code uses integer, floating number and string values and format them into a string using the Format method.

Listing 1: Using Format method to format a string

int val = 7;
string name = "Mr. John";
float num = 45.06f;
string str = String.Format ("Days Left : {0}. Current DataTime: {1:u}. \n String: {2}, Float: {3}" , val, DateTime.Now, name, num);
Console.WriteLine(str);


Trimming and Removing Characters from Strings

The String class provides Trim, TrimStart and TrimEnd methods to trim strings. The Trim method removes white spaces from the beginning and end of a string. The TrimEnd method removes characters specified in an array of characters from the end of a string and TrimStart method removes characters specified in an array of characters from the beginning of a string.

You can also use the Remove method to remove characters from a string. The Listing 2 code shows how to use these methods.
String str = " C# ";
Console.WriteLine("Hello{0}World!", str);
string trStr = str.Trim();
Console.WriteLine("Hello{0}World!", trStr );
str = "Hello World!";
char[] chArr = {'e', 'H','l','o',' ' };
trStr = str.TrimStart(chArr);
Console.WriteLine(trStr);
str = "Hello World!";
char[] chArr1 = {'e', 'H','l','o',' ' };
trStr = str.TrimEnd(chArr1);
Console.WriteLine(trStr);
string MyString = "Hello Delta World!";
Console.WriteLine(MyString.Remove(5,10));

Padding Strings

The PadLeft and PadRight methods can be used to pad strings. The PadLeft method right-aligns and pads a string so that its rightmost character is the specified distance from the beginning of the string. The PadRight method left-aligns and pads a string so that its rightmost character is a specified distance from the end of the string. These methods return new String objects that can either be padded with empty spaces or with custom characters. Listign 3 shows how to use these methods.

Listing 3: Using padding methods
string str1 = "My String";
Console.WriteLine(str1.PadLeft(20, '-'));string str2 = "My String";
Console.WriteLine(str2.PadRight(20, '-'));