Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 2nd, 2006, 04:29 PM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default i want answer

welcome every body
 i am a bigenner in c# & need your helps
  i have a question
 what is operator overload??
 & what is the mean of passing uninitialized reference?

      thanks alot :D

 
Old February 2nd, 2006, 07:30 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Method Overloading: Multiple methods with the same name but different signatures.
Rules for overloading: Methods can't differ by return type, Methods can't differ by reference or "out" parameter.

Unlike C++ and VB, there are NO default parameters in C#. We must use overloading to achieve the same functionality as default parameters.

"passing uninitialized reference?"
a reference is an address (in memory) to an object, I believe the only time you can pass an uninitialized reference is by using the "out" keyword. We use the "out" keyword to pass an uninitialized reference to a function, the function is then expected to assign a reference to the variable, this variable can then be used from the program.


- Adam Kahtava
 
Old February 4th, 2006, 05:10 PM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks about your answers
 but the defination you write about operator override isn"t the defination of it
 it is the defination of methode override not operator override!!!!!!!!!!
  are you see that??

 
Old February 4th, 2006, 05:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

I just worked through some of Richter’s and Troelsen’s stuff on testing reference and value type state equality yesterday and did some == and != operator overloading. I overloaded the operators in a reference type so that they call my overridden Equals method internally. That way I can compare two reference types for state equality, instead of seeing if they point to the same reference (reference equality, the default behavior).

Here’s the class with the overridden Equals and GetHashCode methods, and the overloaded == and != operators:

public class Person
    {
        private int id;
        private string firstName;
        private string lastName;

        public Person(int ID, string FirstName, string LastName) {
            this.id = ID;
            this.firstName = FirstName;
            this.lastName = LastName;
        }

        public int ID {
            get { return id; }
            set { id = value; }
        }
        public string FirstName {
            get { return firstName; }
            set { firstName = value; }
        }
        public string LastName {
            get { return lastName; }
            set { lastName = value; }
        }

        // Override Object.Equals so I can see if the STATE of two
        // person objects is equal, rather than checking default
        // reference equality.
        public override bool Equals(object obj) {

            if (obj == null) return false;

            if (this.GetType() != obj.GetType()) return false;

            Person other = (Person)obj;

            // Compare reference type fields.
            if (!Object.Equals(this.firstName, other.firstName) ||
                !Object.Equals(this.lastName, other.lastName)) return false;

            // Compare value type field.
            if (!this.id.Equals(other.id)) return false;

            // Objects are equal.
            return true;
        }

        // Stronly-typed operator == overload that calls the type-safe
        // Equals override internally.
        public static bool operator==(Person p1, Person p2) {
            return (p1.Equals(p2));
        }

        // Strongly-typed operator != overload that calls the type-safe
        // Equals override internally.
        public static bool operator!=(Person p1, Person p2) {
            return !(p1 == p2);
        }

        public override int GetHashCode() {
            return id.GetHashCode();
        }
    }

Here’s the code to compare two instances of the Person type for state equality:

static void Main() {

            Person p1 = new Person(1, "Bob", "Bedell");
            Person p2 = new Person(1, "Bob", "Bedell");

            // Test Overridden Object.Equals()
            if(p1.Equals(p2) && p1.GetHashCode() == p2.GetHashCode())
                Console.WriteLine("p1 and p2 have same state\n");
            else
                Console.WriteLine("p1 and p2 are DIFFERENT\n");

            // Test Overloaded == operator
            if(p1 == p2 && p1.GetHashCode() == p2.GetHashCode())
                Console.WriteLine("p1 and p2 have same state\n");
            else
                Console.WriteLine("p1 and p2 are DIFFERENT\n");

        }

Value types are handled a little differently, but that should give you some info on how operator’s are overloaded, and why you would want to.

HTH,

Bob


 
Old February 4th, 2006, 09:03 PM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All unary and binary operators have pre-defined implementations, that are automatically available in any expressions. In addition to this pre-defined implementations, user defined implementations can also be introduced in C#. The mechanism of giving a special meaning to a standard C# operator with respect to a user defined data type such as classes or structures is known as operator overloading. Remember that it is not possible to overload all operators in C#.

See full article here:
http://www.csharphelp.com/archives/archive135.html

Search "what is operator overloading C#" on Google

- Adam Kahtava
 
Old February 7th, 2006, 07:49 AM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks about these answer
 i now understand operator overloading

 but i have another question
 what is enumeration ?? & when and why we use it??

  thanks alot :D ;)

 
Old February 7th, 2006, 10:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Enumerating in C#, probably better called ‘iterating’, is accomplished by implementing an object (an enumerator or iterator) that allows you to move sequentially through the elements or items of another object (e.g., a custom collection) using syntax like:

Code:
foreach (object obj in collection)
Here’s how to create an enumerator for a simple custom collection class that just implements ICollection (which in turn implements IEnumerable).

========================
myCustomCollection class
========================
Code:
using System;
using System.Collections;

public class myCustomCollection : ICollection {

    // The CustomCollection class allocates 
    // a simple array.
    private int[] intArray = {1, 2, 3};
    private int count;

    public myCustomCollection() {
        count = 3;
    }

    // ICollection Members
    int ICollection.Count {
        get { return count; }
    }

    bool ICollection.IsSynchronized {
        get { return false; }
    }

    object ICollection.SyncRoot {
        get { return this; }
    }

    void ICollection.CopyTo(Array array, int index) {
        foreach (int i in intArray) {
            array.SetValue(i, index);
            index = index + 1;
        }
    }

    // IEnumerable Members

    // The call to GetEnumerator returns a cursor
    // that enables you to iterate through the
    // elements of the collection using 'for next'
    IEnumerator IEnumerable.GetEnumerator() {
        return new myEnumerator(intArray);
    }
}
Here’s the enumerator object that implements IEnumerator and is allocated by myCustomCollection's call to GetEnumerator():

========================
myEnumerator class
========================
Code:
using System;
using System.Collections;

public class myEnumerator : IEnumerator {

    // Holds the elements of the myCustomCollection
    // class when GetEnumerator is called. 
    private int[] intArray;
    private int cursor;

    public myEnumerator(int[] array) {
        this.intArray = array;
        cursor = -1;
    }

    // IEnumerator Members

    // Returns the item pointed to by the cursor.
    object IEnumerator.Current {
        get { 
            if ((cursor < 0) || (cursor == intArray.Length))
                throw new InvalidOperationException();
            return intArray[cursor];
        }
    }

    // Moves the cursor to the next item.
    // Returns true if successful.
    bool IEnumerator.MoveNext() {
        if (cursor < intArray.Length)
            cursor ++;

        return (!(cursor == intArray.Length));
    }

    // Resets the cursor to -1.
    void IEnumerator.Reset() {
        cursor = -1;
    }
}
Here’s a little controller class to implement a Main method entry point, allocate a new myCustomCollection object, and iterate through the custom collections data member (an array), writing each array element to the Output window.

========================
myController class
========================
Code:
using System;

class myController {
    static void Main() {
        myCustomCollection collection = new myCustomCollection();

        foreach (object obj in collection)
            System.Diagnostics.Debug.WriteLine(obj.ToString());
    }
}
HTH,

Bob

 
Old February 9th, 2006, 03:59 PM
Registered User
 
Join Date: Feb 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what is the meaning of unwinding the stack????






Similar Threads
Thread Thread Starter Forum Replies Last Post
Try this, answer me surendran PHP How-To 1 November 14th, 2006 07:09 AM
Please answer canasdaq XML 3 April 19th, 2006 09:04 AM
answer IvAnR C++ Programming 1 July 22nd, 2005 08:01 AM
About the answer!!! soso ASP.NET 1.0 and 1.1 Basics 0 July 9th, 2003 11:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.