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.
Related
This question already has answers here:
A list of multiple data types?
(6 answers)
Closed 2 years ago.
I would like to store 2 variables of different types, of which one is a class, and the next is a Guid.
May I know if it can be done using List?
Desired output:
List<myClass> = new List<myClass>();
myClass.add(class1, "01-0001");
I'd just use a Tuple<myclass, string> as your type for the list generic parameter:
List<Tuple<myclass, string>> list = new List<Tuple<myclass, string>>();
list.Add(Tuple.Create(class1, "01-0001"));
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.
This question already has answers here:
Why is there no ForEach extension method on IEnumerable?
(21 answers)
Lambda Expression using Foreach Clause [duplicate]
(1 answer)
Closed 7 years ago.
For example, i have array of int and i need increase each element by 2. In modern c# with linq and extension methods the obvious solution is:
var array = new[] {1, 2, 3};
array.ForEach(p => p += 2);
But unfortunately, code will not compile, because ForEach is not extension method of IEnumerable, but its a member if List. So i need to use classic foreach loop or cast array to list, but this solutions not so elegant and simple.
Can anybody tell me, why Microsoft design ForEach method as member of List, but not as extension method of IEnumerable?
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Generic Method Executed with a runtime type [duplicate]
(5 answers)
Closed 8 years ago.
I want to be able to take in a string and call GetType() and find the type. Which I do here and it works perfectly fine.
Type TypeToUse = typeof(someclass).Assembly.GetType("MyProj.Data.Stuff.MyClass");
But when I use the type I found TypeToUse to create my observable collection it says the namespace can't be found.
ObservableCollection<TypeToUse> MyList = new ObservableCollection<TypeToUse>();
How do I fix this?
I'm actually trying to do this with a generic repository I'm assuming I have to add public virtual Type MakeGenericType(params Type[] typeArguments); to the Repository class?
Type elementType = typeof(ColorFilter).Assembly.
GetType("Photometrics.Data.Model.Entity.PhantomModels.Chamber");
Type CoreRepoType = typeof(CoreRepository<>).Assembly.
GetType("Photometrics.Data.Sql.Repository.CoreRepository");
Type combinedType = CoreRepoType.MakeGenericType(elementType);
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? :)