Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

The Evil of Eval() in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.89/5 (22 votes)
19 Jul 2013CPOL3 min read 83.6K   21   20
"Eval is evil". If you have any dignity at all, as a programmer, you are now compelled to banish it forever.

I’ve come across this one several times when working with ASP.NET. When you want to put something from .NET onto your ASP.NET page, it is common to do one of these:

ASP.NET
<%# Eval("FirstName") %>
<%# Bind("FirstName") %> //Bind is just a wrapper around Eval()

As opposed to something like this:

ASP.NET
<%-- In C# --%>
<%# ((DataRowView)Container.DataItem)["FirstName"] %>

<%-- In VB.NET --%>
<%# CType(Container.DataItem, System.Data.DataRowView)("FirstName") %>

The second one seems like more typing. Otherwise they seem pretty close. So, a lot of developers figure: go with fewer words. It’s the kiss principle (keep it simply simple). Guess what? This simple way is a performance killer.

The Eval() command uses reflection to evaluate this expression. In case you are not familiar with reflection, it is bad. It is a notorious performance hog and you should avoid it whenever possible. Microsoft even warns you in the MSDN article about Eval (http://msdn.microsoft.com/en-us/library/4hx47hfe.aspx#remarksToggle read the section under “Note”).

Think of it like this: Eval takes about 1/20th of a second per call, and the other way takes about 1/1000th of a second per call. On a grid with 5 columns and 8 rows, Eval would take 2 seconds and the other would take under 1 second. Nobody would notice the difference. However, change that grid to 10 columns and 50 rows. You now have a difference of 25 seconds. It is the kind of thing that might not matter very much, most of the time but when it does, it makes a big difference.

Once, I had a discussion with a seasoned developer about this topic and he asked “If Eval() is so bad, why would Microsoft leave it in there and refer to it in so many of their docs and online examples?” The reason is: these docs are for newbies and rookies. People who are just starting-out seem to prefer the simplest ways of doing things.

Do you think you should give a rookie the task of building a demanding web page that needs to scale really well or requires peak performance? Someday, yes, but at first, the rookie just wants to get his stuff to run. He needs to crawl before he walks/runs. Knowing how to tune your apps to perform-well is a craft and it comes after experience. Some consider it a bit of a right-of-passage. The day you turn from your Eval() ways, is the day you become a man in the .NET world. In contrast, if you are still using Eval, then it is time for you to move out of your mom’s basement and start using strong-types. You don’t need reflection to do your heavy-lifting any more. You know what Type that databinder is using. So get it together and type-cast it like a senior developer and stop wasting all of those CPU ticks.

If you would like to read more, let me recommend the following articles:

So always remember: "Eval is evil". If you have any dignity at all, as a programmer, you are now compelled to banish it forever. If you ever see it in any code, anywhere, light a torch or grab a pitchfork and run it out of town like the monster that it is.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
I have been programming for 30 years(22 professionally). I mostly work with the Microsoft product stack (.NET, SQL Server, IIS, ASP.NET, Dynamics, TFS, etc) however, I have worked with a wide variety of others too (IBM DB2, Oracle, Java, PHP, ColdFusion, Perl, etc). I have taught classes at community colleges, and other speaking engagements. During the day, I am a consultant, programming and fixing programs.

Comments and Discussions

 
Bug[My vote of 1] 1 star for misleading and/or incomplete information and wasting my time (senior web dev here) Pin
Member 1338778831-Aug-17 7:04
Member 1338778831-Aug-17 7:04 
QuestionSome newbie-ish issue Pin
Member 1168765013-May-15 3:59
Member 1168765013-May-15 3:59 
AnswerRe: Some newbie-ish issue Pin
Tim Golisch15-May-15 2:35
Tim Golisch15-May-15 2:35 
GeneralMy vote of 4 Pin
R.Hari Prasath26-Jul-13 0:10
R.Hari Prasath26-Jul-13 0:10 
Nice Info. HAve been using Eval() for so long without knowing under the hoods. Thanks for the light shown.
GeneralRe: My vote of 4 Pin
Tim Golisch26-Jul-13 3:10
Tim Golisch26-Jul-13 3:10 
QuestionCode review Pin
RonDsz23-Jul-13 5:21
RonDsz23-Jul-13 5:21 
QuestionName 'DataRowView' is not declared. Pin
rogerperkins23-Jul-13 4:49
rogerperkins23-Jul-13 4:49 
AnswerRe: Name 'DataRowView' is not declared. Pin
Tim Golisch24-Jul-13 1:13
Tim Golisch24-Jul-13 1:13 
AnswerRe: Name 'DataRowView' is not declared. Pin
Tim Golisch9-May-14 5:55
Tim Golisch9-May-14 5:55 
GeneralMy vote of 5 Pin
Anurag Gandhi22-Jul-13 22:49
professionalAnurag Gandhi22-Jul-13 22:49 
QuestionType the result? Pin
timh9999922-Jul-13 11:01
timh9999922-Jul-13 11:01 
AnswerRe: Type the result? Pin
KP Lee23-Jul-13 18:01
KP Lee23-Jul-13 18:01 
GeneralRe: Type the result? Pin
timh9999924-Jul-13 1:46
timh9999924-Jul-13 1:46 
QuestionWhy Not disucss the Security? Pin
CdnSecurityEngineer22-Jul-13 5:33
professionalCdnSecurityEngineer22-Jul-13 5:33 
GeneralMy vote of 5 Pin
hypermellow22-Jul-13 3:02
professionalhypermellow22-Jul-13 3:02 
GeneralMy vote of 5 Pin
itaitai21-Jul-13 22:35
professionalitaitai21-Jul-13 22:35 
GeneralMy vote of 5 Pin
MB Seifollahi21-Jul-13 6:19
professionalMB Seifollahi21-Jul-13 6:19 
GeneralRe: watch the Wall Pin
Tim Golisch21-Jul-13 16:46
Tim Golisch21-Jul-13 16:46 
QuestionMy vote of 5 Pin
blachsmith19-Jul-13 14:49
blachsmith19-Jul-13 14:49 
GeneralMy vote of 5 Pin
drewman515019-Jul-13 10:57
professionaldrewman515019-Jul-13 10:57 

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.