Click here to Skip to main content
15,905,781 members
Home / Discussions / C#
   

C#

 
GeneralRe: TCPClient and TCPListener Pin
michaelgr112-Dec-11 8:57
michaelgr112-Dec-11 8:57 
GeneralRe: TCPClient and TCPListener Pin
Addy Tas12-Dec-11 11:44
Addy Tas12-Dec-11 11:44 
GeneralRe: TCPClient and TCPListener Pin
jschell13-Dec-11 8:32
jschell13-Dec-11 8:32 
QuestionWriting a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 0:27
Derek Henderson12-Dec-11 0:27 
AnswerRe: Writing a 6502 cross assembler in c# Pin
Luc Pattyn12-Dec-11 1:05
sitebuilderLuc Pattyn12-Dec-11 1:05 
GeneralRe: Writing a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 1:19
Derek Henderson12-Dec-11 1:19 
GeneralRe: Writing a 6502 cross assembler in c# Pin
harold aptroot12-Dec-11 4:52
harold aptroot12-Dec-11 4:52 
AnswerRe: Writing a 6502 cross assembler in c# Pin
Richard MacCutchan12-Dec-11 5:01
mveRichard MacCutchan12-Dec-11 5:01 
GeneralRe: Writing a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 5:06
Derek Henderson12-Dec-11 5:06 
AnswerRe: Writing a 6502 cross assembler in c# Pin
jschell12-Dec-11 8:34
jschell12-Dec-11 8:34 
Questionestablishing a connection error for mssql 2005 Pin
Member 808991411-Dec-11 23:53
Member 808991411-Dec-11 23:53 
AnswerRe: establishing a connection error for mssql 2005 Pin
thatraja12-Dec-11 3:03
professionalthatraja12-Dec-11 3:03 
QuestionEventType : clr20r3 ,system.windows.markup.xamlparse error Pin
sgkin11-Dec-11 22:24
sgkin11-Dec-11 22:24 
AnswerRe: EventType : clr20r3 ,system.windows.markup.xamlparse error Pin
#realJSOP12-Dec-11 4:40
professional#realJSOP12-Dec-11 4:40 
QuestionSave Word document to database (using OpenXML) Pin
dennieku11-Dec-11 21:37
dennieku11-Dec-11 21:37 
AnswerRe: Save Word document to database (using OpenXML) Pin
Luc Pattyn12-Dec-11 1:09
sitebuilderLuc Pattyn12-Dec-11 1:09 
AnswerRe: Save Word document to database (using OpenXML) Pin
Eddy Vluggen12-Dec-11 9:59
professionalEddy Vluggen12-Dec-11 9:59 
Questionself-initializing static reference types within the definition of the struct ? Pin
BillWoodruff11-Dec-11 15:51
professionalBillWoodruff11-Dec-11 15:51 
AnswerRe: self-initializing static list of instances of a struct within the definition of the struct: I though this was illegal Pin
Luc Pattyn11-Dec-11 16:14
sitebuilderLuc Pattyn11-Dec-11 16:14 
GeneralRe: self-initializing static list of instances of a struct within the definition of the struct Pin
BillWoodruff11-Dec-11 17:06
professionalBillWoodruff11-Dec-11 17:06 
AnswerRe: self-initializing static list of instances of a struct within the definition of the struct Pin
Luc Pattyn11-Dec-11 17:21
sitebuilderLuc Pattyn11-Dec-11 17:21 
AnswerRe: self-initializing static reference types within the definition of the struct ? Pin
DaveyM6911-Dec-11 22:52
professionalDaveyM6911-Dec-11 22:52 
BillWoodruff wrote:
interesting you cannot use the "==" operator on two instances of a struct

Yes and no...

A class can be automatically tested for equality by comparing the reference (pointer) to it, obviously a struct has no reference so has no always defined value.

The reference comparison for classes is pretty much useless without some kind of inlining in most cases so if equality needs to be tested then overloading the == and != operators, and bool Equals(object) and int GetHashCode() methods need to be overriden, something like:
C#
public class TestClass
{
    private int value;

    public TestClass(int value)
    {
        this.value = value;
    }

    public int Value
    {
        get { return value; }
    }

    public static bool operator ==(TestClass first, TestClass second)
    {
        bool result = false;
        if (object.ReferenceEquals(first, second))
            result = true;
        else
            if (!(object.ReferenceEquals(null, first) || object.ReferenceEquals(null, second)))
                result = first.value == second.value;
        return result;
    }
    public static bool operator !=(TestClass first, TestClass second)
    {
        return !(first == second);
    }
    public override bool Equals(object obj)
    {
        bool result = false;
        TestClass other = obj as TestClass;
        if (!object.ReferenceEquals(null, other))
            result = value == other.value;
        return result;
    }
    public override int GetHashCode()
    {
        return value;
    }
}

If you implement the same operators and override the same methods on a struct you can use == etc, in fact the implementation is easier as there is no need for null checking on value types! :
C#
public struct TestStruct
    {
        private int value;

        public TestStruct(int value)
        {
            this.value = value;
        }

        public int Value
        {
            get { return value; }
        }

        public static bool operator ==(TestStruct first, TestStruct second)
        {
            return first.value == second.value;
        }
        public static bool operator !=(TestStruct first, TestStruct second)
        {
            return !(first == second);
        }
        public override bool Equals(object obj)
        {
            bool result = false;
            if (obj is TestStruct)
                result = value == ((TestStruct)obj).value;
            return result;
        }
        public override int GetHashCode()
        {
            return value;
        }
    }

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
BillWoodruff12-Dec-11 11:41
professionalBillWoodruff12-Dec-11 11:41 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
BobJanova13-Dec-11 23:19
BobJanova13-Dec-11 23:19 
GeneralRe: self-initializing static reference types within the definition of the struct ? Pin
DaveyM6914-Dec-11 1:46
professionalDaveyM6914-Dec-11 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.