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.
Related
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.
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:
C# Field Naming Guidelines?
(17 answers)
Closed 9 years ago.
A question about naming local variables in a C# method that takes a parameter of the same name
Please see the code below
private int DoSomething(string activationCode)
{
...
int ??WhatNameToChooseHere?? = Convert.ToInt32(activationCode);
...
}
What could be a good strategy to apply in the above scenario
Note: method paramter and local variable only differ by type only
You cannot do that. A proper way of doing it is to name your variables with different names.
private int DoSomething(string activationCodeStoredInStringAndPassedAsAnArgument) {
int activationCodeThatDeclaredAsAnIntegerToStoreTheValueConvertedFromString = Convert.ToInt32(activationCodeStoredInStringAndPassedAsAnArgument);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why it is not posible to define generic indexers in .NET?
how to write a function to take any object with an index operator
I've never seen any usage like that. But I just wonder if it is possible to make an implementation like bleow. I know that it's not working. But I mean a similar usage if exist.
public T this<T>[T param]
{
get
{
....
}
}
No, generic properties, and indexers (a property), aren't possible.