Denjiki

Member
So I'm sure anyone using resharper with VS has seen this plenty of times -- it keeps wanting me to use 'var' for my types all the time on almost everything. It gives the message "Use 'var' (built-in types)".

What's the deal? Why does it want you to use 'var' instead of even simple stuff like int, bool, double, etc? Don't know much about C# and I wasn't sure if this was something it specifically suggests in context of ServUO solution or what. Does it have something to do with code efficiency?
 
It is really up to the specific developer or the coding rules they are supposed to use in their team.

Using var allows better readability but using types lets you see at a glance what the type actually is.

It really is up to whether or not you would like to read:

Dictionary<key,pair> dict = new Dictionary<key,pair>;

or:

var dict = new Dictionary<key,pair>;

In the end, it is all interpreted the same by the framework.
 
There are some advantages to using var:

Var does not require initialization.
The compiler chooses how to store the variable in memory (more efficient).
Var can be used with generics to allow types to be mixed within a list.

Note that var can only be used at the method level and not for class properties, etc.
 
There are some advantages to using var:

Var does not require initialization.
The compiler chooses how to store the variable in memory (more efficient).
Var can be used with generics to allow types to be mixed within a list.

Note that var can only be used at the method level and not for class properties, etc.

So for example:
var total = this.Caster.Skills[SkillName.Magery].Value;

instead of
double total = this.Caster.Skills[SkillName.Magery].Value;

When declaring 'total' as a var instead of double, it can later take on other forms other than just a double whereas declaring it a double means it would have to be explicitly converted to another type?
 
There are some advantages to using var:

Var does not require initialization.
The compiler chooses how to store the variable in memory (more efficient).
Var can be used with generics to allow types to be mixed within a list.

Note that var can only be used at the method level and not for class properties, etc.

I don't agree. Using var or the type both compile to the exact same MSIL code.

At compile the compiler decides what type the data is based on what data is actually contained in it. var num = 42 == int num =42.
 
Last edited:
Sorry, I should have more clear. I did not mean to imply more efficiency by the compiler but rather the coder.

Much more efficient for me to type

var myList =

Instead of

IEnumerable<int> myList =
 
Sorry, I should have more clear. I did not mean to imply more efficiency by the compiler but rather the coder.

Much more efficient for me to type

var myList =

Instead of

IEnumerable<int> myList =

Ohh, yeah. I agree. I would use var where possible when programing in c#.
 
You ever have one of those moments when it makes sense in your head when you type it and then read it later and notice there is no way someone can read your train of thought... :D
 
var in C# is not equal to var in JavaScript, etc.

var is syntactic sugar, it is short for variable.
var does not change the functionality of the code it is used in.
var is a visual mask for the real Type.
var is interpreted by the compiler as its real Type.
var always requires initialization - you can't do 'var myVariable;'

There is no Type to defer from the assignment in the above case, so var is just a mask without a wearer.

You can hover-over the var keyword in Visual Studio to reveal its real Type in a tool-tip, if the Type that is deferred can not be easily assumed by the developer.

var is extremely useful if it is used in code that could be changed a lot, for example;
C#:
List<int> values = new List<int>( );

Dictionary<string, int> myDictionary = new Dictionary<string, int>( );

foreach(KeyValuePair<string, int> o in myDictionary)
{
     values.Add( o.Value );
}

May be updated to;
C#:
List<int> values = new List<int>( );

int[] myDictionary = new int[100];

foreach(int o in myDictionary)
{
     values.Add( o );
}

That required quite a few edits, but if we use var;
C#:
var values = new List<int>( );

var myDictionary = new Dictionary<string, int>( 100 );

foreach(var o in myDictionary)
{
     values.Add( o.Value );
}

It is much easier to update the implementation;
C#:
var values = new List<int>( );

var myDictionary = new int[100];

foreach(var o in myDictionary)
{
     values.Add( o );
}

Basically, we don't have to rewrite every Type declaration for the variables we use, they are automatically interpreted as the Type of the assignment ( the value on the right-hand side of the = )

In summary, var doesn't affect the compiled code, it doesn't affect performance at run-time (when you run the exe).
It reduces file size, increases readability and improves compatibility.

var is encouraged, but not essential; depending on style, a developer may use var explicitly for every declaration, or they may use it for non-standard Types only (built-in Types like byte, int, bool, etc. are left as they are). Or they may use neither.

I personally prefer to use var for everything, simply because I write a high volume of code that is ever-changing; if I had to manually replace the Typed variable declarations every time I updated the return Type of a method (or whatever), then VNc would be on a much longer development cycle.

---------

C# is a strongly-typed language.
var usually appears in loosely-typed languages such as JavaScript, where var can be mutated and assigned any Type.
C# does however have an object Type that allows a pseudo-loosely-typed object to be created; the 'dynamic' (DynamicObject) type.
It is rarely used, but worth looking in to if you want to write C# without knowing what members your Type will have. (Ex; parsing Json in to a dynamic object where properties of that object may or may not exist).
 
You are correct, using directives are not necessary when using var, because the compiler defers the fully qualified namespace for the assignment Type; so technically speaking, and for examples' sake; if you were to use var in place of List<int>, var will be deferred and interpreted as System.Collections.Generic.List<System.Int32>.

Did you know that you can manually mask type names with the using directive?
C#:
using ShorterName = System.Collections.Generic.List<System.Int32>;
C#:
ShorterName myList = new ShorterName( 100 );

myList.Add( 123 );

Or var'd;
C#:
var myList = new ShorterName( 100 );

myList.Add( 123 );

It is especially useful if you have to use a Tuple<Tn> for data storage;
C#:
using MyStructure = System.Tuple<System.String, System.Int32, System.Object>;
C#:
MyStructure data = new MyStructure("Hello World", 123, new Object( ));

string item1 = data.Item1;
int item2 = data.Item2;
object item3 = data.Item3;

There are a lot of neat language features that are not widely used in RunUO software, which is a shame.
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back