Difference between "var" and "object" in C# [duplicate]
Asked Answered
B

6

45

Is the var type an equivalent to Variant in VB? When object can accept any datatype, what is the difference between those two?

Beatriz answered 12/10, 2009 at 5:34 Comment(0)
H
53

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

var isn't object

You should definitely read this : C# 3.0 - Var Isn't Object

Hildie answered 12/10, 2009 at 5:36 Comment(0)
M
13

Well documented from codeproject

For more details have a look at http://www.codeproject.com/Tips/460614/Difference-between-var-and-dynamic-in-Csharp

Mayes answered 25/8, 2014 at 9:49 Comment(0)
W
7

Nope - var just means you're letting the compiler infer the type from the expression used to assign a value to the variable.

It's just syntax sugar to let you do less typing - try making a method parameter of type "var" and see what happens :]

So if you have an expression like:

var x = new Widget();

x will be of type Widget, not object.

Woodhead answered 12/10, 2009 at 5:36 Comment(1)
It does more than to save typing - var is the only way to deal with anonymous types.Feucht
P
6

The other answers are right on, I'd just like to add that you can actually put your cursor on the 'var' keyword and hit F12 to jump to the inferred type declaration.

Psychomotor answered 12/10, 2009 at 5:49 Comment(2)
Or you can just leave the cursor on "var" a little bit longer and see the infered type in the tooltip ;o) – Cédric 1 min agoLysenko
Hi, When i put the cursor on var and hit F12, I'm navigated to the System.Object, so am i navigated when i put the cursor on object and hit F12. Any explanations on this... I hope that both are sameGoon
A
1

Adding to the post.

Parent p = new Parent(); 
Child c  = new Child();//Child class derives Parent class
Parent p1 = new Child();

With above you can only access parent (p1) properties eventhough it holds child object reference.

var p= new Parent();
var c= new Child();
var p1 = new Child();

when using 'var' instead of class, you have access to both the parent and child class properties. it behaves like creating object for child class.

Ascot answered 6/5, 2016 at 6:6 Comment(0)
S
-1

one difference is Boxing and Unboxing with Object.

Swifter answered 12/12, 2016 at 4:54 Comment(1)
Concept of boxing and unboxing relates to casting value types to reference types and vice-versa e.g. while storing integer value in an array list. var has nothing to do with objects or boxing/unboxing. This is just a compiler trick to do away with lot of fluff involved inside methods due to type names. e.g. Now instead of ApplicationInstallerForBase obj = new ApplicationInstallerForBase(); I simply write var obj = new ApplicationInstallerForBase();. ApplicationInstallerForBase is a class in my application. Note: I haven't downvoted your post.Holbert

© 2022 - 2024 — McMap. All rights reserved.