Why can't I pass an anonymous type as a parameter to a function?

Someone asked on Stack Overflow:

I was trying to do something like below but it doesn’t work. Why won’t .NET let me do this?

private void MyFunction(var items)
{
 //whatever
}

I posted the following answer, which was chosen as the accepted answer and received 7 upvotes:

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:

var i = 10; // implicitly typed
int i = 10; //explicitly typed

In otherwords, var keyword is only allowed for locally scoped variables.

Source.

A little bit more info here. Basically, when using var you must also initialize the variable to a value on the same line so that the compiler knows what type it is.

Notable comments

Nate (1 upvotes): @VincentVancalbergh I think that dynamic would provide for the use that OP was after.


Originally posted on Stack Overflow — 7 upvotes (accepted answer). Licensed under CC BY-SA.

ASP.NET Custom Error Page Not Displayed for Abnormal URLs

Someone asked on Stack Overflow:

A vulnerability scanning service regularly tests our site for PCI scan compliance. It has just started trying to access URLs with abnormal formatting, such as:

http://www.mydomain.com/ShoppingCart.aspx//ErrorPage.aspx%3fid%3d2?

We have a Custom Error Page set which works for everything except this. Is there any way to force IIS to display it for this type of URL?

The Error: Runtime Error - An application error occurred on the server…

We’re using:

  • ASP.NET 2.0 (Framework 3.5)
  • IIS 7.0 (Windows Server Web 2008)

I’ve tried to debug this, but I can’t reproduce this on IIS 6.0.

I posted the following answer, which was chosen as the accepted answer and received 1 upvote:

There might be a more simple solution, but if you’re on IIS7 you can use URL Rewrite to match those type of URLs and map them back to your error page.


Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.

Minify inline css code before its written to database, then unminify it when editing

Someone asked on Stack Overflow:

My theme has a custom css code block where I allow the site owner to add any custom css they need directly to the head section of the theme. This inserts whatever they’ve placed in this block into the wordpress database as a custom option insert.

I then retrieve this content into header.php and output it between an inline style tag like so…

<style type="text/css">
.test h1 {}
.testcss2, .somecss {}
</style>

This works perfectly fine, however, I would like to clean up and minify the markup when its written to the database. I suppose a regex is needed to do this? If so, what would that be?

The result I’m looking for, when the code is written into the page’s markup is…

<style type="text/css">.test h1{}.testcss2,.somecss{}</style>

I’d also like to reverse the minified markup when its presented back to the user to edit in my theme options. In that view, I just want to reformat the minified css code so that each directive is back on its own line.

I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:

It depends what you want achieve by doing this. I agree with @thomasfedb that you will likely messup the style of most peoples CSS by doing this transformation and will likely cause more trouble than its worth.

It’s my suggestion that you keep the data exactly as the user entered it, and then ‘minify’ it when you render it to the page.

This will not save you and storage in your database, and it will increase your CPU usage per page render, but it will save you the bandwidth of all the extra new-line characters.

Another option, presuming database storage is not much of a concern, is to store the data twice, once where the user edits, and once minified. Then simply minify and copy the user-editable field into the minified field whenever the user makes any changes.


Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.

Switching from Linq-To-SQL to Entity Framework for WCF Data Services Issue with FK &quot;Properties&quot;

Someone asked on Stack Overflow:

So, In my old Linq-To-SQL I would write code like this

var tableItem = new Table
{
    Prop1 = var1
    Prop2 = var2,
    Prop3 = var3,
    ParentTableID = parentRecordID
};

db.Table.InsertOnSubmit(tableItem);
db.SubmitChanges();

After converting my working code from Linq-To-SQL to Entity Framework (v3.5) the ParentTableID property is no longer there and I’m at a loss as to how I can create this related child record.

I’m not sure what I should change for my entity framework version, besides the obvious call to SaveChanges(); instead of SubmitChanges() :(

I posted the following answer, which was chosen as the accepted answer:

I ended up using the solution here: Entity Framework: Setting a Foreign Key Property.


Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.

Is it possible to write MyGeneration Templates in F# and how?

Someone asked on Stack Overflow:

I want to write a MyGeneration Template File in F#. I know you can target various languages etc VB.NET and C# however for my own selfish benefit I would like to write it in F#.

Anyone one if and how this is possible?

I posted the following answer, which was chosen as the accepted answer and received 1 upvote:

In visual studio you can use T4 templating to generate any type of output using a psudo “asp classic” style syntax, where you intermix your code (F#) and the “output.”

Heres a good place to get started: http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx


Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.

Windows Server 2008 R2 and VMWare or?

Someone asked on Server Fault:

I want to use Windows Server 2008 R2 as a host, and load RedHat as a guest OS. Should I use VMWare, or does Windows have something built in that competes with VMWare?

I posted the following answer, which was chosen as the accepted answer:

Windows Server 2008 has Hyper-V built in, and Windows Server 2008 R2 also includes a newer version Hyper-V which supports much better backup and integration with System Center Virtual Machine Manager (a must have if you are going to be managing more than a handful of virtual host machines).

While it may not be “supported” many other OSes work under Hyper-V, they simply don’t get the integration tools installed. Its a trade-off, but I haven’t seen too many issues with it.

That said, we get very good density out of our Hyper-V machines, although we have very low per-VM usage, we see 20+ VMs on a host machine, with 6 drive RAID-10, 32GB Memory, and two quad-core CPUs.


Originally posted on Server Fault — 0 upvotes (accepted answer). Licensed under CC BY-SA.

How to support leagcy urls(Web forms type) in asp.net mvc routing

Someone asked on Stack Overflow:

We have recently shifted to asp.net mvc, but we still need to support some legacy urls. What is the best way to handle this situation. Is it Application_PreRequestHandlerExecute() event in global.asax, that I need to use or is there any better way?

I posted the following answer, which was chosen as the accepted answer and received 1 upvote:

You can use the URL Rewrite module for IIS7. Scott Hanselman has a good post on using URL rewrite to to handle legacy URLs here.

Another option, I believe you can simply add a route that matches your old url syntax.


Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.

How to correctly refactor a control&#39;s namespace in ASP.NET

Someone asked on Stack Overflow:

I have this situation which is rather frustrating… I have some user controls where I would like to rename their namespace. However, when I do this my build fails because the .designer.cs file for pages that use the controls is still generating control declarations with the OLD namespace. Is this coming from the assembly? I can’t rebuild because this is obviously creating a compile-time error (those controls no longer exist in the old name). I’ve tried wiping out the entire file, but it is always regenerated with the old namespace.

How do I get around this without manually editing every .designer.cs file???

I posted the following answer, which was chosen as the accepted answer:

In the past I have used these steps with some success, but I’ve also had to monkey around a lot, so I maybe missing some steps. (its been a while since I had to deal with this issue)

  1. Close Visual Studio
  2. Delete all designer.cs files (backup just in case ;))
  3. Fire up Visual Studio again and try a rebuild

Originally posted on Stack Overflow — 0 upvotes (accepted answer). Licensed under CC BY-SA.

webex interferes with web application

Someone asked on Stack Overflow:

I received a complaint about buttons disappearing and odd formatting of a web application that I support. Upon troubleshooting, it seemed that the only thing new about the environment (which was previously working fine) was the installation of webex on the client machine.

Uninstalling webex resolved the issue. Please mind that webex was not being used during the issue, but it was still causing odd displays and formatting of Internet Explorer pages.

I’ve always disliked webex because it required reboot to use and the client felt very invasive (gotomeeting can accomplish the same if not better features without being nearly as invasive).

OK, I guess the first few sentences were a frustrated rant. The question is, what does webex do to mangle Internet Explorer pages (even when webex is not actively being used).

Examples of the mangling are: non appearing buttons; completely overridden css; and javascript errors (when the app has no javascript errors).

I posted the following answer, which was chosen as the accepted answer and received 1 upvote:

Did you check Internet Explorer “add-ons”? I’ll bet they’ve installed an add-on during the installation of their client, which is possibly embedding its own HTML into every page.


Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.

Organization of simple project

Someone asked on Stack Overflow:

I want to create a simple mvc application. It’s typical task and very simular with phpmyadmin. I have a table and a few operations delete, edit, create new row.

                Name  Gender Age   
[delete] [edit] Alex  Male   20 
[delete] [edit] Elza  Female 23 
                         [New person] 

When edit or New person clicked by user application show the following page

Name   [........]
Gender [........]
Age    [........]
            [Save]

I’m very new in asp.net and mvc, can anyone suggest a right project organization or give links to simular applications?

I posted the following answer, which was chosen as the accepted answer and received 1 upvote:

A few guide lines I like to stick with:

  1. Entities should map to database tables
    1. You may want to create ViewModels to show denormalized data.
  2. Actions should be verbs that you perform on your entities
  3. Always use Strongly Typed views, always
  4. Try to keep your controllers lean, less controller code = better
    1. If you need, move code into a “service” class that your controller consumes

Depending on your database choice, I’d probably use Linq-To-SQL to quickly generate my entities and I’d probably wrap them up in a simple repository class. If you using something besides SQL Server, I’d try Entity Framework.

If your scope starts getting bigger, you may want to swap out your generated entities and insert your own DTOs inbetween your MVC app and the database layer.


Originally posted on Stack Overflow — 1 upvotes (accepted answer). Licensed under CC BY-SA.

signed letter b

Dad. Geek. Gamer. Software developer. Cloud user. Old Car enthusiast.  Blogger.


Top Posts


profile for Nate on Stack Exchange, a network of free, community-driven Q&A sites
a proud member of the blue team of 512KB club
Thoughts, opinions, and ideas shared here are my own. © 2026 Nate Bross.