471,310 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Extend Types like instances?

Can one extend the type itself and not just an instance of the type?

So I have something, say, like

public static DateTime RawRead(this DateTime i, Stream s)
{
BinaryReader b = new BinaryReader(s);
i = DateTime.FromBinary(b.ReadInt64());
return i;
}
But how I have to use it is

DateTime d = new DateTime();
d = d.RawRead(s);

I'd like to be able to do

DateTime d = DateTime.RawRead(s);

or even

DateTime d = new DateTime();
d.RawRead(s);
In a sense I need something like

public static DateTime RawRead(this ref DateTime i, Stream s)
{
BinaryReader b = new BinaryReader(s);
i = DateTime.FromBinary(b.ReadInt64());
}

But of course that doesn't work. (Or even a way to extend the type itself)

Ultimately its not that big a deal but its a little messy to have to use the
instance itself to call the method. (and for the fact that it creates two
objects since I first have to instantiate it then return a new one)

Thanks,
Jon
Apr 3 '08 #1
1 1012
Also, is it possible to override an extension? (I doubt it but I'd like to
be able to override any extensions later on that I've created without having
the source)
Apr 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pete Shinners | last post: by
2 posts views Thread by Charles Fineman | last post: by
5 posts views Thread by Stephen Lamb | last post: by
22 posts views Thread by Ben Finney | last post: by
6 posts views Thread by gangesmaster | last post: by
9 posts views Thread by VK | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.