Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:44:11+00:00 2026-05-27T09:44:11+00:00

I would like to ask a theoretical question. If I have, for example, the

  • 0

I would like to ask a theoretical question. If I have, for example, the following C# code in Page_load:

cars = new carsModel.carsEntities();

var mftQuery = from mft in cars.Manufacturers 
               where mft.StockHeaders.Any(sh=> sh.StockCount>0) 
               orderby mft.CompanyName 
               select new {mft.CompanyID, mft.CompanyName};
               // ...

Questions:

  1. This code uses the var keyword. What is the benefit of this construct?
  2. What is the key difference between the implementation of var in Javascript and C#?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:44:11+00:00Added an answer on May 27, 2026 at 9:44 am

    JavaScript is a dynamically typed language, while c# is (usually) a statically typed language. As a result, comparisons like this will always be problematic. But:

    JavaScript’s var keyword is somewhat similar to C#’s dynamic keyword. Both create a variable whose type will not be known until runtime, and whose misuse will not be discovered until runtime. This is the way JavaScript always is, but this behavior is brand new to C# 4.

    dynamic foo = new DateTime();
    foo.bar();  //compiles fine but blows up at runtime.
    

    JavaScript has nothing to match C#’s var, since JavaScript is a dynamically typed language, and C#’s var, despite popular misconception, creates a variable whose type is known at compile time. C#’s var serves two purposes: to declare variables whose type is a pain to write out, and to create variables that are of an anonymous type, and therefore have no type that can be written out by the developer.

    For an example of the first:

    var conn = new System.Data.SqlClient.SqlConnection("....");
    

    Anonymous type projections from Linq-to-Sql or Entity Framework are a good example of the second:

    var results = context.People.Where(p => p.Name == "Adam")
                                .Select(p => new { p.Name, p.Address });
    

    Here results is of type IQueryable<SomeTypeTheCompilerCreatedOnTheFly>. No matter how much you might like to write out the actual type of results, instead of just writing var, there’s no way to since you have no knowledge of the type that the compiler is creating under the covers for your anonymous type—hence the terminology: this type is anonymous

    In both cases the type is known at compile time, and in both cases, subsequently saying either

    conn = new DateTime();
    

    or

    results = new DateTime();
    

    would result in a compiler error, since you’re setting conn and results to a type that’s not compatible with what they were declared as.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask probably simple question. Consider following php page: <p>Name of
I would like to ask a basic iphone question. I have many TextFields in
What I would like ask is best illustrated by an example, so bear with
I would like to ask for a reccomended solution for this: We have a
I would like to ask if I have a table of events on my
I would like to ask a theoritical question about how some web sites work.
I would like to ask something about query using mysql I have this table
I use c#, linq and EF4 I would like ask your help. My question:
I would like to ask you a question about implementing mutual authentication with Kerberos,
I would like to ask a theoritical question. Assuming that an application is using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.