c# alternative for enum to get intellisense advantages
I have a base class
abstract public class ComponentBase
{
public List<string> Actions { get; set; }
protected abstract void RegisterActions();
}
and its child
public class VideoBase : ComponentBase
{
protected override void RegisterActions()
{
base.Actions.Add("Start video");
base.Actions.Add("Pause video");
base.Actions.Add("Rewind video");
}
}
But to make things easier i also create enum type
public enum Actions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO,
}
What i want is to force every child of ComponentBase to have its own enum Actions but it seems its not easy to do. Alternatively i though about changing Actions List to Dictionary<string, string>
but it doesn't give me intellisense advantage. I want for user of this class to easily get "list" of actions in intellisense, instead of checking what string value they have to put, any suggestions?
c#
add a comment |
I have a base class
abstract public class ComponentBase
{
public List<string> Actions { get; set; }
protected abstract void RegisterActions();
}
and its child
public class VideoBase : ComponentBase
{
protected override void RegisterActions()
{
base.Actions.Add("Start video");
base.Actions.Add("Pause video");
base.Actions.Add("Rewind video");
}
}
But to make things easier i also create enum type
public enum Actions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO,
}
What i want is to force every child of ComponentBase to have its own enum Actions but it seems its not easy to do. Alternatively i though about changing Actions List to Dictionary<string, string>
but it doesn't give me intellisense advantage. I want for user of this class to easily get "list" of actions in intellisense, instead of checking what string value they have to put, any suggestions?
c#
2
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05
add a comment |
I have a base class
abstract public class ComponentBase
{
public List<string> Actions { get; set; }
protected abstract void RegisterActions();
}
and its child
public class VideoBase : ComponentBase
{
protected override void RegisterActions()
{
base.Actions.Add("Start video");
base.Actions.Add("Pause video");
base.Actions.Add("Rewind video");
}
}
But to make things easier i also create enum type
public enum Actions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO,
}
What i want is to force every child of ComponentBase to have its own enum Actions but it seems its not easy to do. Alternatively i though about changing Actions List to Dictionary<string, string>
but it doesn't give me intellisense advantage. I want for user of this class to easily get "list" of actions in intellisense, instead of checking what string value they have to put, any suggestions?
c#
I have a base class
abstract public class ComponentBase
{
public List<string> Actions { get; set; }
protected abstract void RegisterActions();
}
and its child
public class VideoBase : ComponentBase
{
protected override void RegisterActions()
{
base.Actions.Add("Start video");
base.Actions.Add("Pause video");
base.Actions.Add("Rewind video");
}
}
But to make things easier i also create enum type
public enum Actions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO,
}
What i want is to force every child of ComponentBase to have its own enum Actions but it seems its not easy to do. Alternatively i though about changing Actions List to Dictionary<string, string>
but it doesn't give me intellisense advantage. I want for user of this class to easily get "list" of actions in intellisense, instead of checking what string value they have to put, any suggestions?
c#
c#
asked Jan 2 at 12:41
FakeFake
506
506
2
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05
add a comment |
2
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05
2
2
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05
add a comment |
2 Answers
2
active
oldest
votes
How about using generics in your base class?
abstract public class ComponentBase<T> where T : struct
{
//Example property.
public T Action { get; set; }
}
As InBetween mentioned, in C# 7.3 you can add an additional constraint to force the generic type to be an enum:
abstract public class ComponentBase<T> where T : struct, System.Enum
That way you get to specify which enum to use when inheriting ComponentBase
in your child class:
public class VideoBase : ComponentBase<VideoActions>
{
...your code...
}
public enum VideoActions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO
}
VideoBase video = new VideoBase();
//video.Action will now be of type VideoActions.
video.Action = VideoActions.START_VIDEO;
3
In the latest language version you can actually constraintT
toenum
.
– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
add a comment |
In case you want to have this enum
-like implementation to restrict the value to a specific subset of string values, there is a solution for this. You could create a class with private constructor and static fields:
public class Action
{
/// <summary>
/// Start video
/// </summary>
public static readonly Action START_VIDEO = new Action("Start video");
/// <summary>
/// Pause video
/// </summary>
public static readonly Action PAUSE_VIDEO = new Action("Pause video");
/// <summary>
/// Rewind video
/// </summary>
public static readonly Action REWIND_VIDEO = new Action("Rewind video");
private readonly string _value;
private Action(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static implicit operator string(Action action)
{
return action._value;
}
}
And whenever you restrict any input to Action
, like your list for example
public List<Action> Actions { get; set; }
or something like
public void RegisterAction(Action action) { }
Which will restrict the value to one of those declared in the Action
class rather than allowing any string
to be used.
You can then do the Actions.Add(<intellisense suggestions show up>)
and still use it as string
within your code if needed:
// Implicit conversion
string someValue = Action.START_VIDEO;
// .ToString() override
Console.WriteLine("Action: " + Action.START_VIDEO);
In both cases the code will use the string representation declared in the class instead of the name as it would be in enum
case.
Furthermore, by providing the description in <summary>
you also get the advantage of seeing the actual value when hovering mouse over reference to Action
.
1
This way you can use it both asAction
and asstring
, that is, you can have both. AList<Action>
and aList<string
> are absolutely not the same thing and one can not be converted to the other.List<T>
is invariant inT
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.
– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is thatActions.Add(Action.START_VIDEO)
will work in both casesList<Action>
andList<string>
. In the latter case, implicit cast would be used which I declared in the example.
– Imantas
Jan 2 at 13:15
1
But if the ultimate goal is to register inList<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?
– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted asAction
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.
– Imantas
Jan 2 at 13:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006586%2fc-sharp-alternative-for-enum-to-get-intellisense-advantages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How about using generics in your base class?
abstract public class ComponentBase<T> where T : struct
{
//Example property.
public T Action { get; set; }
}
As InBetween mentioned, in C# 7.3 you can add an additional constraint to force the generic type to be an enum:
abstract public class ComponentBase<T> where T : struct, System.Enum
That way you get to specify which enum to use when inheriting ComponentBase
in your child class:
public class VideoBase : ComponentBase<VideoActions>
{
...your code...
}
public enum VideoActions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO
}
VideoBase video = new VideoBase();
//video.Action will now be of type VideoActions.
video.Action = VideoActions.START_VIDEO;
3
In the latest language version you can actually constraintT
toenum
.
– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
add a comment |
How about using generics in your base class?
abstract public class ComponentBase<T> where T : struct
{
//Example property.
public T Action { get; set; }
}
As InBetween mentioned, in C# 7.3 you can add an additional constraint to force the generic type to be an enum:
abstract public class ComponentBase<T> where T : struct, System.Enum
That way you get to specify which enum to use when inheriting ComponentBase
in your child class:
public class VideoBase : ComponentBase<VideoActions>
{
...your code...
}
public enum VideoActions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO
}
VideoBase video = new VideoBase();
//video.Action will now be of type VideoActions.
video.Action = VideoActions.START_VIDEO;
3
In the latest language version you can actually constraintT
toenum
.
– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
add a comment |
How about using generics in your base class?
abstract public class ComponentBase<T> where T : struct
{
//Example property.
public T Action { get; set; }
}
As InBetween mentioned, in C# 7.3 you can add an additional constraint to force the generic type to be an enum:
abstract public class ComponentBase<T> where T : struct, System.Enum
That way you get to specify which enum to use when inheriting ComponentBase
in your child class:
public class VideoBase : ComponentBase<VideoActions>
{
...your code...
}
public enum VideoActions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO
}
VideoBase video = new VideoBase();
//video.Action will now be of type VideoActions.
video.Action = VideoActions.START_VIDEO;
How about using generics in your base class?
abstract public class ComponentBase<T> where T : struct
{
//Example property.
public T Action { get; set; }
}
As InBetween mentioned, in C# 7.3 you can add an additional constraint to force the generic type to be an enum:
abstract public class ComponentBase<T> where T : struct, System.Enum
That way you get to specify which enum to use when inheriting ComponentBase
in your child class:
public class VideoBase : ComponentBase<VideoActions>
{
...your code...
}
public enum VideoActions
{
START_VIDEO,
PAUSE_VIDEO,
REWIND_VIDEO
}
VideoBase video = new VideoBase();
//video.Action will now be of type VideoActions.
video.Action = VideoActions.START_VIDEO;
edited Jan 2 at 13:26
answered Jan 2 at 12:59
Visual VincentVisual Vincent
15.5k52052
15.5k52052
3
In the latest language version you can actually constraintT
toenum
.
– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
add a comment |
3
In the latest language version you can actually constraintT
toenum
.
– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
3
3
In the latest language version you can actually constraint
T
to enum
.– InBetween
Jan 2 at 13:01
In the latest language version you can actually constraint
T
to enum
.– InBetween
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
@InBetween : Thanks! Didn't know that!
– Visual Vincent
Jan 2 at 13:01
add a comment |
In case you want to have this enum
-like implementation to restrict the value to a specific subset of string values, there is a solution for this. You could create a class with private constructor and static fields:
public class Action
{
/// <summary>
/// Start video
/// </summary>
public static readonly Action START_VIDEO = new Action("Start video");
/// <summary>
/// Pause video
/// </summary>
public static readonly Action PAUSE_VIDEO = new Action("Pause video");
/// <summary>
/// Rewind video
/// </summary>
public static readonly Action REWIND_VIDEO = new Action("Rewind video");
private readonly string _value;
private Action(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static implicit operator string(Action action)
{
return action._value;
}
}
And whenever you restrict any input to Action
, like your list for example
public List<Action> Actions { get; set; }
or something like
public void RegisterAction(Action action) { }
Which will restrict the value to one of those declared in the Action
class rather than allowing any string
to be used.
You can then do the Actions.Add(<intellisense suggestions show up>)
and still use it as string
within your code if needed:
// Implicit conversion
string someValue = Action.START_VIDEO;
// .ToString() override
Console.WriteLine("Action: " + Action.START_VIDEO);
In both cases the code will use the string representation declared in the class instead of the name as it would be in enum
case.
Furthermore, by providing the description in <summary>
you also get the advantage of seeing the actual value when hovering mouse over reference to Action
.
1
This way you can use it both asAction
and asstring
, that is, you can have both. AList<Action>
and aList<string
> are absolutely not the same thing and one can not be converted to the other.List<T>
is invariant inT
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.
– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is thatActions.Add(Action.START_VIDEO)
will work in both casesList<Action>
andList<string>
. In the latter case, implicit cast would be used which I declared in the example.
– Imantas
Jan 2 at 13:15
1
But if the ultimate goal is to register inList<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?
– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted asAction
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.
– Imantas
Jan 2 at 13:35
add a comment |
In case you want to have this enum
-like implementation to restrict the value to a specific subset of string values, there is a solution for this. You could create a class with private constructor and static fields:
public class Action
{
/// <summary>
/// Start video
/// </summary>
public static readonly Action START_VIDEO = new Action("Start video");
/// <summary>
/// Pause video
/// </summary>
public static readonly Action PAUSE_VIDEO = new Action("Pause video");
/// <summary>
/// Rewind video
/// </summary>
public static readonly Action REWIND_VIDEO = new Action("Rewind video");
private readonly string _value;
private Action(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static implicit operator string(Action action)
{
return action._value;
}
}
And whenever you restrict any input to Action
, like your list for example
public List<Action> Actions { get; set; }
or something like
public void RegisterAction(Action action) { }
Which will restrict the value to one of those declared in the Action
class rather than allowing any string
to be used.
You can then do the Actions.Add(<intellisense suggestions show up>)
and still use it as string
within your code if needed:
// Implicit conversion
string someValue = Action.START_VIDEO;
// .ToString() override
Console.WriteLine("Action: " + Action.START_VIDEO);
In both cases the code will use the string representation declared in the class instead of the name as it would be in enum
case.
Furthermore, by providing the description in <summary>
you also get the advantage of seeing the actual value when hovering mouse over reference to Action
.
1
This way you can use it both asAction
and asstring
, that is, you can have both. AList<Action>
and aList<string
> are absolutely not the same thing and one can not be converted to the other.List<T>
is invariant inT
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.
– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is thatActions.Add(Action.START_VIDEO)
will work in both casesList<Action>
andList<string>
. In the latter case, implicit cast would be used which I declared in the example.
– Imantas
Jan 2 at 13:15
1
But if the ultimate goal is to register inList<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?
– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted asAction
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.
– Imantas
Jan 2 at 13:35
add a comment |
In case you want to have this enum
-like implementation to restrict the value to a specific subset of string values, there is a solution for this. You could create a class with private constructor and static fields:
public class Action
{
/// <summary>
/// Start video
/// </summary>
public static readonly Action START_VIDEO = new Action("Start video");
/// <summary>
/// Pause video
/// </summary>
public static readonly Action PAUSE_VIDEO = new Action("Pause video");
/// <summary>
/// Rewind video
/// </summary>
public static readonly Action REWIND_VIDEO = new Action("Rewind video");
private readonly string _value;
private Action(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static implicit operator string(Action action)
{
return action._value;
}
}
And whenever you restrict any input to Action
, like your list for example
public List<Action> Actions { get; set; }
or something like
public void RegisterAction(Action action) { }
Which will restrict the value to one of those declared in the Action
class rather than allowing any string
to be used.
You can then do the Actions.Add(<intellisense suggestions show up>)
and still use it as string
within your code if needed:
// Implicit conversion
string someValue = Action.START_VIDEO;
// .ToString() override
Console.WriteLine("Action: " + Action.START_VIDEO);
In both cases the code will use the string representation declared in the class instead of the name as it would be in enum
case.
Furthermore, by providing the description in <summary>
you also get the advantage of seeing the actual value when hovering mouse over reference to Action
.
In case you want to have this enum
-like implementation to restrict the value to a specific subset of string values, there is a solution for this. You could create a class with private constructor and static fields:
public class Action
{
/// <summary>
/// Start video
/// </summary>
public static readonly Action START_VIDEO = new Action("Start video");
/// <summary>
/// Pause video
/// </summary>
public static readonly Action PAUSE_VIDEO = new Action("Pause video");
/// <summary>
/// Rewind video
/// </summary>
public static readonly Action REWIND_VIDEO = new Action("Rewind video");
private readonly string _value;
private Action(string value)
{
_value = value;
}
public override string ToString()
{
return _value;
}
public static implicit operator string(Action action)
{
return action._value;
}
}
And whenever you restrict any input to Action
, like your list for example
public List<Action> Actions { get; set; }
or something like
public void RegisterAction(Action action) { }
Which will restrict the value to one of those declared in the Action
class rather than allowing any string
to be used.
You can then do the Actions.Add(<intellisense suggestions show up>)
and still use it as string
within your code if needed:
// Implicit conversion
string someValue = Action.START_VIDEO;
// .ToString() override
Console.WriteLine("Action: " + Action.START_VIDEO);
In both cases the code will use the string representation declared in the class instead of the name as it would be in enum
case.
Furthermore, by providing the description in <summary>
you also get the advantage of seeing the actual value when hovering mouse over reference to Action
.
edited Jan 2 at 13:46
answered Jan 2 at 13:04
ImantasImantas
1,103918
1,103918
1
This way you can use it both asAction
and asstring
, that is, you can have both. AList<Action>
and aList<string
> are absolutely not the same thing and one can not be converted to the other.List<T>
is invariant inT
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.
– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is thatActions.Add(Action.START_VIDEO)
will work in both casesList<Action>
andList<string>
. In the latter case, implicit cast would be used which I declared in the example.
– Imantas
Jan 2 at 13:15
1
But if the ultimate goal is to register inList<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?
– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted asAction
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.
– Imantas
Jan 2 at 13:35
add a comment |
1
This way you can use it both asAction
and asstring
, that is, you can have both. AList<Action>
and aList<string
> are absolutely not the same thing and one can not be converted to the other.List<T>
is invariant inT
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.
– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is thatActions.Add(Action.START_VIDEO)
will work in both casesList<Action>
andList<string>
. In the latter case, implicit cast would be used which I declared in the example.
– Imantas
Jan 2 at 13:15
1
But if the ultimate goal is to register inList<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?
– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted asAction
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.
– Imantas
Jan 2 at 13:35
1
1
This way you can use it both as
Action
and as string
, that is, you can have both. A List<Action>
and a List<string
> are absolutely not the same thing and one can not be converted to the other. List<T>
is invariant in T
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.– InBetween
Jan 2 at 13:13
This way you can use it both as
Action
and as string
, that is, you can have both. A List<Action>
and a List<string
> are absolutely not the same thing and one can not be converted to the other. List<T>
is invariant in T
; first because its a class, not an interface, and second because the type parameter can`t be covariant or contravariant.– InBetween
Jan 2 at 13:13
@InBetween I didn't say it's the same, what I meant is that
Actions.Add(Action.START_VIDEO)
will work in both cases List<Action>
and List<string>
. In the latter case, implicit cast would be used which I declared in the example.– Imantas
Jan 2 at 13:15
@InBetween I didn't say it's the same, what I meant is that
Actions.Add(Action.START_VIDEO)
will work in both cases List<Action>
and List<string>
. In the latter case, implicit cast would be used which I declared in the example.– Imantas
Jan 2 at 13:15
1
1
But if the ultimate goal is to register in
List<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?– InBetween
Jan 2 at 13:22
But if the ultimate goal is to register in
List<string> Actions
then why all this unnecessary boiler plate? What does this provide that simple constant strings declared in the derived type don't?– InBetween
Jan 2 at 13:22
@InBetween that's actually a good point. If it's not restricted as
Action
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.– Imantas
Jan 2 at 13:35
@InBetween that's actually a good point. If it's not restricted as
Action
anywhere in the code, then there's no advantage of this example in comparison with string constants except for maybe having control of values if they are referenced outside of the library which I don't really think is needed in this case anyways.– Imantas
Jan 2 at 13:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006586%2fc-sharp-alternative-for-enum-to-get-intellisense-advantages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Unless you know in the base class every possible action the derived types might need, you can't really enforce what you want. But why do you want to force anyone to do anything? Let the implementors of the derived types do whatever they decide is comfortable for them. No one is stopping them from creating a static private class with all the required strings as constants, an enum or whatever if they wish.
– InBetween
Jan 2 at 12:53
If you've access to the base class you may want to try a class with constant properties.
– Abdullah Leghari
Jan 2 at 13:05
Documentation Comments is the closest thing you can do to get the list's info.
– Abdullah Leghari
Jan 2 at 13:05