This question already has answers here:
What are generics in C#? [closed]
(3 answers)
Understanding C# generics much better
(5 answers)
Closed 9 months ago.
In C# Documentations, there was an example under constructed types where they use class Queue , what is TElement and what does it mean /represent?
It's a placeholder for whatever type you wish to specify.
It could be Queue<string> or Queue<int> or Queue<List<int> or Queue<SomeComplicatedClass> - whatever you like.
It's called a "generic type parameter" (the bit between the < and the >).
The docs page has the following example:
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
You can read more about generic type parameters here.
Related
This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.
Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better
This question already has answers here:
What does the [Flags] Enum Attribute mean in C#?
(14 answers)
What does square bracket [] mean in the below code?
(2 answers)
what is [] brackets in .net? [duplicate]
(7 answers)
Closed 6 years ago.
Sorry for the silly question, but I came across the following C# code and I'm wondering what the [Flags] portion is and what it does.
[Flags]
public enum UserFlags
{
//...
}
Thank you in advance.
It's a class attribute. There are also method attributes for example.
You can even write your own
They are usually used to define meta information or behaviour about a class or method and can be read using reflection.
This question already has answers here:
Optional return in C#.Net
(13 answers)
Closed 8 years ago.
Java 8 has Optional<T> which is nice way to declare optional types as described here.
Is there an equivalent way to that in C# ?
As per this answer You could make a struct that mimics this this type of functionality.
This question already has answers here:
How do arrays in C# partially implement IList<T>?
(6 answers)
Closed 8 years ago.
I have a class constructor that takes a IList<IElement> as an argument.
When creating a new instance of the class I'm able to pass a IElement[] instead of the IList<IElement> how is that posible?
An array with element type T derives from IList<T>.
This is not visible in the meta-data in mscorlib.dll, but the inheritance relationship is created at runtime in the CLR. C# and the CLR are aware of the array type and treat it specially.
This question already has answers here:
When do you use the "this" keyword? [closed]
(31 answers)
In C#, is "this" keyword required? [duplicate]
(6 answers)
Closed 9 years ago.
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables?
Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.