'var' keyword in c# [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
C# ‘var’ keyword versus explicitly defined variables
Use of var keyword in C#
May I know where to use 'var' keyword and the purpose of using that there ?

Sure, just check one of the many links here : https://stackoverflow.com/search?q=c%23+var+keytword ;)

You can use the var keyword to infer the type of an object.
For example:
//without using the "var" keyword
Dictionary<List<string>,int> myCustomDictionary = new Dictionary<List<string>, int>();
//with the "var" keyword
var myCustomDictionary = new Dictionary<List<string>, int>();
Which is easier to read? :)

Related

Declaring List in C# [duplicate]

This question already has answers here:
C# declaring variables using var vs type [duplicate]
(2 answers)
Closed 2 years ago.
I was wondering if
var list = new List<t>();
exactly the same as
List<t> list = new List<t>()
Is this exactly the same or are there any differences?
Yes, this is totally the same. The compiler will internally replace the var with the actual type, inferred from right side of the expression.
In the C# 9 you can also use another type of record: List<t> list = new() and compiler will infer the type for the right part from the left one.

Is there a way to shorten field declaration in C#? [duplicate]

This question already has answers here:
Is there a way to abbreviate a custom class type declaration?
(4 answers)
Implicit typing; why just local variables?
(6 answers)
Closed 2 years ago.
Why do I have to write:
public VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName> field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Instead of:
public var field = new VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>();
Is there any way to shorten this ridiculous declaration?
Why do I have to write two identical types in a single line?
Upd.
I have found that using "dynamic" keyword instead of "var" perfectly solves this problem!
Please feel free to provide any info on the perfomance (or other) issues with this solution!
You can use a type alias. Documentation can be found here. Link to alias for generic class: here
using YourShortName = VeryLongClassNameThatHurtsMyEyes<AnotherVeryLongClassName>;
Usage
public YourShortName field = new YourShortName();
See here for why you can only use var for local variables.
And see here for an in depth look at dynamic and why it's not even remotely close to using var.

What does a '?' following a type mean with generics? [duplicate]

This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
Closed 7 years ago.
I have the following enigmatic declaration in C#:
Dictionary<string, DateTime?> badCameras = new Dictionary<string, DateTime?>();
This was written by a programmer no longer here & IS compiling! What ever does the '?' mean following DateTime object? Has it something to do with a struct? I've searched online & am finding nothing? BTW, this is .NET3.5.
Thanks
DateTime? is compiler sugar for Nullable<DateTime>. Effectively, nullable types allow for the use of null when dealing with value types that don't normally support null. See here for details.
The ? in that context is a shortcut for Nullable<T>. In your example, its equivalant to:
Dictionary<string, Nullable<DateTime>>
The main purpose is to allow value types to hold the null value.

What is the difference between var and dynamic c#? [duplicate]

This question already has answers here:
What's the difference between dynamic (C# 4) and var?
(14 answers)
Closed 7 years ago.
I'm not sure what is the exact different between both declarations.
When should I use var and when should I use dynamic.
Thanks a lot!
Don
var is type of variable decided by the compiler at compile time. Need to initialize at the time of declaration. All errors are caught on compile time.
dynamic is type of variable decided by the compiler at runtime time. No need to initialize at the time of declaration. All errors are caught at runtime.
dynamic variables can be used to create properties and return values from a function. var variables cannot be used for property or return values from a function. They can only be used as local variable in a function.

Why is implicit typing disallowed in non-local variable declarations? [duplicate]

This question already has answers here:
Implicit typing; why just local variables?
(6 answers)
Closed 9 years ago.
I changed this:
List<string> chapterNames = new List<string>();
...to this:
var chapterNames = new List<string>();
...and was jabbed with this message from the compiler:
"The contextual keyword 'var' may only appear within a local variable declaration"
The simple answer is because that's how Microsoft introduced the var keyword in the specification for .NET 3.0.
http://msdn.microsoft.com/en-us/library/bb384061.aspx

Categories

Resources