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.
Related
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.
This question already has answers here:
Casting a variable using a Type variable
(11 answers)
Closed 4 years ago.
I have a Type variable and I need to cast another object to it. (one which I know what the type is, but currently its an "object" type). I need to do this for reasons that aren't really important to the answer.
// Pseudocode
MyObjectClass myTypedVar = new MyObjectClass();
Type myKnownType = myTypedVar.GetType();
var anotherObject = (myKnownType) anObjectVarThatIsReallyMyObjectClass;
I've read this page Type Casting an Object using a "Type" Object in C# and I understand but I don't think it applies directly. I anticipate a solution using reflection, but I just haven't been able to figure it out myself.
If I understand you correctly, you could use the Convert.ChangeType method:
var anotherObject = Convert.ChangeType(anObjectVarThatIsReallyMyObjectClass, myKnownType);
You won't get any kind of compile-time checking when using a dynamic type like this though. Please refer to the following blog post for more information about this.
Generic type parameters and dynamic types in C#: https://blog.magnusmontin.net/2014/10/31/generic-type-parameters-and-dynamic-types-in-csharp/
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.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
how to make an variable accessible to other class(within same csproj)
I have a variable
int principleIndex = Principles.Instance.RowIndexToPrincipleIndex(hti.Row);
I want to use it in another class linq query:
principlesList.Select(p => p.GetInstanceForDatabase()).where(p=>p.principleIndex ).ToList()
but principleIndex is not accessible here.
How do I do it??? I tried to make it static and I also tried to use it inside a property, but does not work.
Is principleIndex defined in the same scope as your second line of code? If so perhaps your Where call should read something like .Where(forDBInstance => forDBInstance.Index == principleIndex)
This question already has answers here:
Is there a reasonable approach to "default" type parameters in C# Generics?
(6 answers)
Closed 9 years ago.
Is it possible to do something like
public class PriorityQueue<TValue, TPriority=int> where TPriority : IComparable
(note the =int) ?
Before you suggest it, yes, I know I can just add another line:
public class PriorityQueue<TValue> : PriorityQueue<TValue, int> { }
But I'm wondering if it's possible to do it as a param.
No. There is no option for default types on generic types in C#.
Your second example is often the "best" option available, if you need this behavior.