Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2007

Tip: Looking for answers? Try searching our database.

Named anonymous types??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William Stacey [C# MVP] - 17 Oct 2007 02:29 GMT
Maybe an Oxymoron, but this would be very useful.  Anonomous types are great
for doing projections (especially in Linq), but not being able to pass or
return them is a bummer. Why couldn't they add something like an "AS"
modifier like:

var v = new {Name="", Age=0} as MyType;
var v2 = Change(v);

void MyType Change(MyType mt)
{
     return new {mt.Name + "2", mt.Age += 1} as MyType;
}

In this case, MyType can only be "declared" the same way over the whole
assembly. So you can use "as MyType" in multiple projections with
compile-time checking. However, the compiler flags an error if MyType is not
declared the same way each time, so the first declaration "sets" the type by
name. In this case, it is not really anonymous anymore, however it adds
ability to strongly type projections and ref them from across functions and
assemblies.

Signature

William Stacey [C# MVP]

Nicholas Paldino [.NET/C# MVP] - 17 Oct 2007 02:42 GMT
This makes sense, to a degree, but I think it would be easier to just
have a refactoring option in the IDE which would take an anonymous type and
create a known type, and then replace the anonymous type in the code with
the new known type.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Maybe an Oxymoron, but this would be very useful.  Anonomous types are
> great
[quoted text clipped - 20 lines]
> and
> assemblies.
William Stacey [C# MVP] - 17 Oct 2007 03:26 GMT
Interesting.  That might work, however I think there are few ways that could
break down:

1) It becomes an IDE feature instead of a language feature, so people not
using VS are out of luck (i.e. notepad, vi, etc).
2) How would you do things like List<T> in other parts of your code before
the replacement?:  List<new{Name="", Age=1}> .  That would get difficult.
With the other way, you could do List<MyType> as normal.
3) You may have two anymous types with same "signature", but different uses.
How would the IDE disambiguate them?  For example, you may have different
extentions methods on the two types.  Using the "as" method, it would be
explicit.

Probably other issues.  But heck, if they could work it out, I might like it
over having no feature.

Signature

William Stacey [C# MVP]

|    This makes sense, to a degree, but I think it would be easier to just
| have a refactoring option in the IDE which would take an anonymous type and
[quoted text clipped - 25 lines]
| > and
| > assemblies.
Jon Skeet [C# MVP] - 17 Oct 2007 07:37 GMT
> Maybe an Oxymoron, but this would be very useful.  Anonomous types are great
> for doing projections (especially in Linq), but not being able to pass or
[quoted text clipped - 16 lines]
> ability to strongly type projections and ref them from across functions and
> assemblies.

I'd rather make it easy to declare the same kinds of types in code.
Automatically implemented properties go *some* of the way to this, but
they don't give the read-only nature that anonymous types have.

Personally, if I'm going to need a name for the type, I'd rather have
it described explicitly rather than somewhere in the middle of a
method.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.