This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enum “Inheritance”
I've been trying to do something like inheritance in Enum. I wanted a base Enum with multiple values from different Enums.
the best approach will be answered below.
Enum is a value type and consequently sealed, i.e. cannot be inherited.
See also Enum “Inheritance”
Related
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:
What's the difference between [Something] and [SomethingAttribute] [duplicate]
(3 answers)
Closed 7 years ago.
I hope this wasn't asked already. But i found nothing. If something exists, thanks for the note.
The title says it all i think.
I've seen these two variants. But in my opinion it does the same. And why can i use both. Thanks for education.
// variant 1
[ContentProperty("Text")]
// variant 2
[ContentPropertyAttribute("Text")]
You can omit the word "Attribute" when writing attributes over something. The actual class is called ContentPropertyAttribute. Both of your lines do exactly the same and use the exact same attribute class.
This question already has answers here:
Can you add to an enum type in run-time
(7 answers)
Closed 8 years ago.
I have this enum function with some elements:
public enum TrackingTypeEnum
{
None,
Start,
PageView,
Foreground,
Background,
Push,
}
And I want to add some elements there, but not manually in the function, i want to use "new" command but dosn't work.
I've tried this:
TrackingTypeEnum Custom = new TrackingTypeEnum;
Any solution?
Thanks!
According to official documentation, you can't use enum keywork in such a way. In sort, an enum is a list of constants you can assign values to.
Read: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx
That's not what enums are for- you should only use them to store every possible state of an object.
If you really do want to use enums, you can store those additional values in a dictionary, like it's covered in this SO answer:
C#: can you add to an enum type in run-time
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.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Try to describe polymorphism as easy as you can
What is polymorphism?
Please read MSDN which covers it in reference to c#,
Basically a derived class inherits from another class it gets all its methods,events and properties, and every type is polymorphic in .NET since they all have Object as their base class.