Assignment Operator Alternative

F

Funky

In the code snippet below, I have a base class (originally written in
C++) that byte swaps data when assigned or read from the class (thus,
always storing the data as big-endian).

As for those wondering how the data is set outside these 2 public
operations: This class is later used to declare attributes in other
classes and in these other classes, System.IO.Buffer is used copy bytes
from a socket.

So anyway, I see that C# does not support assignment operator
overloading. Nor does it support primitive type overloading. What are
my alternatives?


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

namespace TestApp {
class BaseClass {
/* Always stored in big-endian */
private ushort myValue;

private static ushort swap(ushort input) {
return((ushort)((0xFF00 & input) >> 8) | ((0x00FF & input) << 8));
}

public operator ushort () {
return (swap(myValue));
}

public BaseClass operator= (byte rhs) {
myValue = swap(rhs);
return(this);
}
}
}
 
M

Mattias Sjögren

So anyway, I see that C# does not support assignment operator
overloading. Nor does it support primitive type overloading. What are
my alternatives?

Conversion operators. See the implicit and explicit keywords.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top