Owner: C# Rocks URL:http://csharprocks.blogspot.com/ Join Date: Mon, 18 Jun 2007 22:19:03 -0500 Rating:0 Site Description: This blog is dedicated to the latest C# 2005 programming language...If u like C, C++...C# or C Sharp is the next big thing for you... Site statistics:Click here
Thanks! 2007-06-18 08:44:00 Everyone for being a part of C# Rocks!It feels really great to write to a real good audience, C# Rocks! has now 50+ posts and is still growing. I now its a long way to go!, but milestones are a part of development isn't it?. So, once again I thank all the readers and visitors for being my valuable audience, I appreciate your each visit. I hope you continue to be a part of C# Rocks!WB
Read more:Thanks
Creating out Parameters 2007-06-18 01:30:00 I assume that you properly went through my post on Using ref parameters, today I'm focussing on the out parameters.The out is short for output. When you pass an out parameter to a method, the compiler checks whether the passed parameters are initialised in the method. In the following example I have passed two arguments using the out keyword. Since, I haven't initialised the parameters in the method swap(), the compiler doesn't compile and returns two errors as follows:private void Form1_Load(object sender, EventArgs e) { int a = 10; //Two variables int b = 20; //values before swapping Console.WriteLine("value of a = " + a + " value of b = " + b); //Calling the static swap method. //notice here that the two parameters are passed using the out keyword fun.swap( out a,out b); //values after swapping. Console.WriteLine("value of a = " + a + " value of b = " Read more:Creating
Using ref Parameters / Passing values by reference. 2007-06-17 23:41:00 You may have noticed when you pass an argument to a method, the method creates a copy of the argument to use it in its code block, irrespective whether the argument is a value type or reference
type. To understand this concept clearly, review the following example carefully, in this example two arguments are passed to the static method, swap().class Program { static void Main(string[] args) { int a = 10; //Two variables int b = 20; //values before swapping Console.WriteLine("value of a = " + a + " value of b = " + b); //Calling the static swap method. fun.swap( a , b); //values after swapping. Console.WriteLine("value of a = " + a + " value of b = " + b); Console.Read(); } class fun { //static method swap, takes two arguments. public static void swap( int a
Interface Vs Abstract class / when should I use an abstract class or interface? 2007-06-15 07:26:00 After going through my posts on interface and abstract class
, you can get confused which one should I implement. Well, this is quite a challenging question and only you can answer it. How? Well, when you know what you are going to do without worrying much on how you going to achieve it, use an interface and when you know what you are going to implement use an abstract class. Still confused, continue reading, an abstract class can contain many methods, but it requires at least one method to be abstract i.e. an abstract class can provide a general functionally but the specialised functionality is specified by the derived classes. So, an abstract can contain a combination of implemented and non implemented methods. If there are no implemented method(s), then it is serving as an interface and you should consider using an interface in this case. On the other hand if it contains only implemented methods then it can't be abstract, because we need at least one method to be abstract.Whereas an
Abstract Class/abstract classes and methods / Using abstract classes 2007-06-15 04:21:00 What is an abstract class/method.In my previous posts, whenever I created an object of some class type. But there are some classes for which we can't create objects. Such classes are called Abstract Classes. These classes are used as base classes in inheritance hierarchies. We refer to them as abstract classes. We can consider abstract classes as incomplete or partial classes, which require further addition. Its only function is to provide an appropriate base class from which other classes can inherit. As such, they determine the nature of the members that the derived class must implement, but it doesn't itself provide any implementation of one ore more members (methods, properties or indexers).An abstract method is created by specifying the abstract type modifier. An abstract method does not include any implementation in the base class. Thus, a derived class must override it. You might think an abstract method works like a virtual method, well quite true! Abstract methods are in
Common Interfaces of the .NET Framework Class Library 2007-06-14 21:49:00 Common interfaces in the .NET Framework
Class Library
(FCL). These interfaces are implemented and used in the same manner as those you created earlier.Common interfaces of the .NET Framework Class Library. InterfaceDescriptionIComparableInterface IComparable can also be used to allow objects of a class that implements the interface to be compared to one another. The interface contains one method, CompareTo, that compares the object that calls the method to the object passed as an argument to the method. Classes must implement CompareTo to return a value indicating whether the object on which it is invoked is less than (negative integer return value), equal to (0 return value) or greater than (positive integer return value) the object passed as an argument, using any criteria specified by the programmer. IComponentImplemented by any class that represents a component, including Graphical User Interface (GUI) controls (such as buttons or labels). Interface IComponent defines the Read more:Common
, Interfaces
Implementing Properties in a interface or Interface properties 2007-06-14 08:59:00 In my previous posts I have specified only methods as a part of interface, now let's specify properties inside an interface.class Program { static void Main(string[] args) { MyClass mc = new MyClass(); mc.no = 2; //set no=2 Console.WriteLine(mc.no); //returns 4 Console.Read(); } interface IProp { int no { get;//return no. set; //sets no. } } class MyClass:IProp //Implemnet IProp { private int val; //private member public int no //property body implemented in the class { get { val += 2; return val; } set
Using explicit implementation to remove ambiguity 2007-06-14 03:24:00 When to use an explicitly implemented interface?In my earlier posts I mentioned its better to use explicit implementation over implicit. The reason, well I have an example mentioned.class Program { static void Main(string[] args) { number n1 = new number(); //Ambiguity rises here, do u know whose cal() is it?!? Console.WriteLine( n1.cal(10)); Console.Read(); } interface IAdd { int cal(int x);//cal method } interface IMul { int cal(int x);//cal method, same signature } class number : IAdd, IMul //Uses both interfaces. { public int cal(int x) //Which interface's method gets called? { return x * x; } }
Interface-Creating a private implementation/ Implicit and Explicit Implementations 2007-06-13 07:17:00 Review the following example: class Program { static void Main(string[] args) { person p1 = new person(); p1.display(); IDisplay d = p1; //Create an interface reference variable d.show(); //Calling the explicitly implemented method Console.ReadLine(); } interface IDisplay //Declasre IDisplay as an interface { // no access modifiers, methods are public // no implementation void display(); //Method signature only, no implementation. void show(); } class person : IDisplay //Inherit IDisplay { public string _name; //members public person() //defualt constructor { _name = "Waseem"; } Read more:Creating
, Explicit
, Implementations
, Interface
Using Interface Reference or referencing a class through its interface 2007-06-13 01:01:00 It's quite surprising but yes you can create an interface reference variable. As you already know that you can reference an object by using a variable defined as class
that his higher up the hierarchy. You can reference an object by using a variable defined as an interface that its class implements. person p1 = new person();IDispay d = p1; //Legal d.display(); In this example, you can reference a person object by using an IDisplay variable.We can declare a method, that accepts only those arguments that implement the interface. class Program { static void Main(string[] args) { person p1 = new person(); person.checker(p1); //passes the p1 as an argument to the checker method. student s1 = new student(); person.checker(s1); //compliation error, since student does not implement IDisplay Console
More on Interfaces 2007-06-12 23:03:00 Things to remember:The method names and return types should exactly match.Any parameters (including ref and out keyword modifiers) should match exactly.The method name is prefaced by the name of the interface.Implementing More Than One Interface:Classes can implement more than one interface.class Program { static void Main(string[] args) { person p1 = new person(); p1.display(); //uses its own implementation for the display method p1.read(); p1.display(); Console.Read(); } interface IDispay //Declasre IDisplay as an interface { // no access modifiers, methods are public // no implementation void display(); //Method signature only, no implementation. } interface IRead //Declare IRead as an an Read more:Interfaces
Understanding Interfaces 2007-06-12 04:03:00 What is an interface?From my previous posts you must already know how to derive a class from another class, but the real power of inheritance lies in inheriting from an interface. An interface allows you to completely separate the name of a method from its implementation. In an interface, no method can include a body, i.e. an interface provides no implementation whatsoever. It stresses more on what is to be done than how it is to be done. An interface can be implemented by any number of classes. One class can implement any number of interfaces. Interfaces
only include method signatures; their implementations are determined by their derived classes. Thus, two classes can implement the same interface in different ways. C# supports "one interface and multiple methods".An example, how to declare an interface:interface IDispay //Declasre IDisplay as an interface { // no access modifiers, methods are public // no implementation void Read more:Understanding
Creating Multiple Inheritance/Hierarchy 2007-06-11 23:48:00 Hello and welcome to your ultimate guide to C# programming. Today we'll discuss about multiple inheritance. Till now we discussed inheritances to just one level, I.e. one base and one derived class. Today well add more to our knowledge.C# doesn't support multiple inheritances, unlike c++. But it does support multilevel inheritance. In multilevel inheritance the derived class acts as base class for other class. To understand multilevel inheritance in c# read the example thoroughly.class Program{ static void Main(string[] args) { C c = new C(1, 2, 3); //calls the overloaded constructor for class C c.display(); //calls the overridden display() Console.Read(); } class A { private int _a; public A() //Default constructor { _a = 0; } public A(int a)// Read more:Creating
, Hierarchy
, Inheritance
, Multiple
Virtual Methods 2007-06-11 00:30:00 Before I start my today's post on virtual methods, I would like to share some of my experiences with you. Well, I try to post on this blog some topics on the wonderful C# programming language. Some of you may think that I have C# on the brain, well that's not completely true. The only force that drives me posting on this blog is the idea of helping others. I don't earn much from this blog, as this blog is quite fresh, earning was not may aim when I started this blog; I just thought it to be an outcome of hard work. Anyways, without wasting much time and energy lets get started with one of the most important aspects in object oriented programming Virtual methods. First thing's first. What are virtual methods?Those who are already familiar with Object Oriented Programming & some of the fundamentals there in, shouldn't have much problem understanding Virtual methods, well those who do not, I strongly recommend reading some basics of OOP (search in Google for more). What is a virt Read more:Methods
Using base to access a hidden name 2007-06-09 06:41:00 You can access base class methods/variables which are hidden by the member names of the derived class. Lets us consider a situation:class a { public int i=0; } class b : a { new public int i; void put(int a, int b) { base.i = a; //assigns a to base class variable i. i = b; //assigns b to derived class variable i. } } Here, the base class member is accessed using the base keyword.
Inheritance and Name Hiding 2007-06-09 06:33:00 It is possible for a derived class to define a variable/method that has the same name as that of the base class's variable/method. When this happens, the member in the base class is hidden within the defined class. The compiler generates a warning message telling you that variable/method hides the inherited variable/method of the base class.public humans(string name, int age) { _name=name; _age=age; } public void display() { Console.WriteLine("Name =" + _name); Console.WriteLine("Age = " + _age); } } class students:humans //Derived from humans class { private int _sID; public students(string name, int age, int sID):base(name,age) //Calls humans { _sID = sID; Read more:Inheritance
Calling Base class Constructors 2007-06-08 22:51:00 Calling Base Class ConstructorsHow to call the base class
constructor from the derived class?Every class has its own constructor whether you create it or not (if you don't provide a default constructor, the compiler generates a default constructor itself). When you inherit a class, it contains members and methods. The members of the base class require initialization, since the constructor of the derived class can initialize only the derived class members. The solution is quite simple; you use the base keyword to call a base class constructor, when the constructor is defined. E.g. class humans { //Members of the base class private string _name; private int _age; //base class constructor public humans(string name, int age) { _name=name; _age=age; } } class students:humans //Derive Read more:Calling
Using Inheritance in c# 2007-06-07 07:11:00 Using Inheritance
Its time to get under the hood, so here it goes…In c#, the syntax for declaring a class is derived from other class is as follows:class student:human { //Actual Implementation. } Here, student is derived from the base class human. Unlike c++, c# classes can be derived from just one class but can derive many interfaces. More on interfaces in my future posts. Here in c# inheritance is always implicitly public. One more important thing, when you define classes, these classes are automatically inherits all the features of System.Object class. E.g. the .ToString method.Member Access and inheritance:Members of class are either declared public or private depending upon their levels of protection. Members declared private prevent any unauthorized use. When a class inherits from a base class which has private members, the derived class cannot access the private members of the base class. A private class member will remain private to its class
Types of Inheritance 2007-06-06 06:21:00 Types of Inheritance
Before you lay your hands on writing the code, you should understand the types of inheritance.Implementation inheritance:Implementation inheritance means that a type takes all the members & methods/functions of the base type. The derived type uses the implementation of the base type functions. The derived class has the option of either using the base functions/methods implementation or override it to be more specific. This type of inheritance is useful when you want to add functionality to an existing class or where the numbers of related classes have functionality.e.g. System.Windows.Forms.TextBox and System.Windows.Forms.ListBox are derived from Controls. But each control has its own method overridden to provide their unique functionality.Interface Inheritance:In this inheritance, the derived class only implements the method signatures rather than implementing the entire method. This inheritance is most used when you want distinct functionality. In c#, unlike
Introduction to Inheritance 2007-06-06 05:32:00 InheritanceHi! There, this is my new post on one of the fundamental topics of Object Oriented Programming. Understanding inheritance is quite challenging, but hey we all love challenges, don't we? Before I explain inheritance, I assume you have a good understanding of Objects and classes. If you don't I would suggest you to lay your hands on some fundamental programming book or search the internet. Ill add some more posts on object oriented fundamental topics in the course of time. When I first learnt about inheritance a couple of questions really haunted me. "Why inheritance, what is inheritance" and so on. After working with c# a lot, I can now understand the use of it. Inheritance is often used when we say a child inherits genes from parents. He a child inherits some genes from mom and some from daddy. A child has properties like face cut, colour of eyes and hair, etc mostly inherited from parents or grandparents. In programming, inheritance is classification. To fully understand Read more:Inheritance
, Introduction
Difference between array and arrayList 2007-06-05 22:48:00 If you go through my previous posts "Difference between array and collections", you will probably understand the difference. However, as a revision all I can say here is that array stores value types which do not require any unboxing, whereas ArrayList stores elements as objects which require unboxing. Array and ArrayList also have some distinct features, array cannot grow or shrink in size, i.e. we cant change the size of the array or we cannot redim it what it used to be in VB. In case of ArrayList can be dynamically resized or reduced. The ArrayList.count property returns the total number of elements in the arraylist to keep track of the size of the arraylist. ArrayList uses complex boxing and unboxing, arrays as such are much easier in their implementation.
ArrayList Properties & Methods 2007-06-05 22:33:00 ArrayList methods and properties Method or propertyPurposeAdapter( ) Public static method that creates an ArrayList wrapper for an IList object. FixedSize( ) Overloaded public static method that returns a list object as a wrapper. The list is of fixed size; elements can be modified but not added or removed. ReadOnly( ) Overloaded public static method that returns a list class as a wrapper, allowing read-only access. Repeat( ) Public static method that returns an ArrayList whose elements are copies of the specified value. Synchronized( ) Overloaded public static method that returns a list wrapper that is thread-safe. Capacity Property to get or set the number of elements the ArrayList can contain. Count Property to get the number of elements currently in the array.IsFixedSize Property to get to find out if the ArrayList is of fixed size. IsReadOnly Property to get to find out if the ArrayList is read-only. IsSynchronized Property to get to find out if the ArrayList is thread-safe. Item( Read more:Methods
, Properties
Arrays Vs Collections 2007-06-05 08:50:00 Arrays Vs CollectionsAn array list declares the type of elements it holds.A collection stores elements as objects.An array instance cannot be grow or shrink. It size remains constant.Collections can grow dynamically.We cannot create a read only array.Collections can be used in Read-Only modes. The collection class provides a ReadOnly method for this.
DataSet vs. DataReader 2007-05-08 10:57:00 Dataset vs DatareaderTo determine whether to use the DataSet or the DataReader when you design your application, consider the level of functionality that is needed in the application.Use the DataSet in order to do the following with your application:Navigate between multiple discrete tables of results.Manipulate data from multiple sources (for example, a mixture of data from more than one database, from an XML file, and from a spreadsheet).Exchange data between tiers or using an XML Web service. Unlike the DataReader, the DataSet can be passed to a remote client.Reuse the same set of rows to achieve a performance gain by caching them (such as for sorting, searching, or filtering the data).Perform a large amount of processing per row. Extended processing on each row returned using a DataReader ties up the connection serving the DataReader longer than necessary, impacting performance.Manipulate data using XML operations such as Extensible Stylesheet Language Transformations (XSLT transfo
About Me... 2007-04-27 03:54:00 Hi,I am Waseem Bashir, recently graduated as a BCA (Bachelor of Computer Applications). I am a C# developer and i intend to dedicate this blog to this wonderful programming language.This blog will contain all the tips n tricks in Visual C# 2005.Feel free to be a part of this blog...Happy Blogging!Cya soon
Reversing a Jagged Array 2007-06-22 05:49:00 ProblemThe Array
.Reverse method does not provide a way to reverse each subarray in a jagged array. You need this functionality.SolutionUse the ReverseJagged
Array<T> method: public static void ReverseJaggedArray<T>(T[][] theArray) { for (int rowIndex = 0; rowIndex <= (theArray.GetUpperBound(0)); rowIndex++) { for (int colIndex = 0; colIndex <= (theArray[rowIndex].GetUpperBound(0) / 2); colIndex++) { T tempHolder = theArray[rowIndex][colIndex]; theArray[rowIndex][colIndex] = theArray[rowIndex][theArray[rowIndex].GetUpperBound(0) - colIndex]; theArray[rowIndex][theArray[rowIndex].GetUpperBound(0) - colIndex] = tempHolder; } } }DiscussionThe following TestReverseJaggedArray method shows how the ReverseJaggedArray<T> Read more:Reversing
Delegates 2007-06-22 01:36:00 What are delegates?If you are familiar with C/C++, in C# a delegate is similar to a function pointer. "A delegate is a pointer to a method". Is delegate a method or a class?Delegates are implemented as classes derived from System.MulticastDelegate which is derived from the base class System.Delegate. But when you create an instance of a delegate, what you create is also referred to as a delegate. Understanding delegates:A delegate looks and behaves like a normal method when called. However, delegates can refer to different methods at runtime. How does the delegate refer to a method?From my previous posts, reference is nothing but a memory address. E.g. when a reference to an object is created, actually the address of the object is stored. Similarly, every method has a physical location in memory. Delegates store the address of the methods. Thus, a delegate refers to a method.Delegates can be used to refer different methods during the runtime of a program. This is really important fea
Delegates 2007-06-22 00:20:00 Topics covered:• What are delegates?• Understanding Delegates.• Why delegates?• Declaring and using Delegates.
Generating a Class Diagram 2007-06-21 03:47:00 Generating a Class Diagram
The Class View window is useful for displaying the hierarchy of classes and interfaces in a project. Visual Studio 2005 also enables you to generate class diagrams which depict this same information graphically (you can also use a class diagram to add new classes and interfaces, and define methods, properties, and other class members).To generate a new class diagram, click the Project menu, and then click Add New Item. In the Add New Item window select the Class Diagram template and then click Add. This action will generate an empty diagram, and you can create new types by dragging items from the Class Designer category in the Toolbox. You can generate a diagram of all existing classes by clicking and dragging them individually from the Class View window, or by clicking and dragging the namespace to which they belong. The diagram shows the relationships between the classes and interfaces, and you can expand the definition of each class to show its contents. Yo
Struct vs Classs 2007-06-21 02:57:00 QuestionStructClassIs this a value type or a reference type?A structure is a value type.A class is a reference type.Do instances live on the stack or the heap?Structure instances are called values and live on the stack.Class instances are called objects and live on the heap.Can you declare a default constructor?NoYesIf you declare your own constructor, will the compiler still generate the default constructor?YesNoIf you don't initialize a field in your own constructor, will the compiler automatically initialize it for you?NoYesAre you allowed to initialize instance fields at their point of declaration?NoYesSource: Internet