score:1
In some sense, C# enums are similar to a object-oriented class hierarchy. If you declare enumeration Choice
with alternatives One
and Two
, it is a bit similar to declaring a class hierarchy with base class Choice
and two derived types (One : Choice
and Two : Choice
).
One difference is that enumerations are not extensible (if you need to add a case, you need to modify the declaration, while if you need to add a case to a class hierarchy, you simply define a new inherited type).
Second difference is that options of an enumeration cannot carry additional data - if you're declaring class hierarchy, you can store other fields in the
One
case (for example).
Without these two "limiations", you could consider using enumerations to represent entities in an application (in fact, functional languages have construct similar to these "more powerful" enumeration and use it for these kind of things).
However, since C# enumerations are very simple, they are mostly used to represent values independent of the specific domain, or values that are needed more generally in programming. For example:
enum TriState { True, False, Undecided }
This could be quite useful type, but it isn't directly related to any specific domain.
score:2
I use enums for database values that should never be created or altered by a user and if there is special logic based on the value. Currencies are not a good example unless you have every currency in your enumeration and accept that you will have to recompile your app should a new currency come along or one is deprecated. OrderStatus might be a good example if you have logic in your system based on the OrderStatus. Regardless, it should be noted that if you do create an enumeration to represent a database value, that the application must be recompiled should a new value in this table need to be created and thus this practice should be used with great caution.
In situations where I'm going to mimic a database value with an enumeration, I use the name of the enumerated value as the primary key of the table and as value passed to the foreign key. I.e., when I save to the database, I'm going to pass myEnumVal.ToString()
. I have a couple of reasons for this. First, it requires that adding a value is more deliberate. If I were to use the numeric value as the primary key, there is an implication that I'm using a surrogate key and that additional values can easily be added. When other developers see a column using a name as the primary key, there is a higher chance that they'll think that the value is not arbitrary. Second, it makes database code significantly easier to read. You get queries like Where OrderStatus = 'Active'
instead of Where OrderStatus = 1
.
Source: stackoverflow.com
Related Query
- Do you use enums in your data applications and how?
- What are Expression Trees, how do you use them, and why would you use them?
- How to bind data from mutiple tables to datagridview using Entity Framework and use CRUD operations?
- How to get and use source of grouping in LINQ?
- How to use Linq to pull certain data from XmlNodeList and load it into a dictionary in C#
- How can you force Entity Framework to use string instead of char for CHAR, NCHAR, VARCHAR(1) and NVARCHAR(1) columns?
- How do you populate a parent child data structure from XML where generic List<> objects are in parent and child structures using one LINQ query?
- How do you access data from your I Series in ASP.Net?
- How to use my view model class and passing data from view model to View on the page?
- How to use linq query on list to find last data by name and number c#?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to assign LINQ Query to a variable and then use it later in the code
- How to use Descendants and LINQ to get complex XML data to two classes
- How to use pagelist with data source as multiple tables, to display table values in mvc?
- How do you get data across multiple tables with EF and linq?
- How do you use the Group Statement on your SQL to Linq queries?
- What does this C# code with an "arrow" mean and how is it called?
- Can you use LINQ types and extension methods in IronPython?
- How do you use LINQ to find the duplicate of a specific property?
- How do you use LINQ with Sqlite
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- How can I use Nhibernate to retrieve data when the "WHERE IN()" have thousands of values? (too many parameters in the sql)
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- How to union two data tables and order the result
- How do you use FirstOrDefault with Include?
- How to convert PropertyInfo to property expression and use it to invoke generic method?
- How to use SqlAzureExecutionStrategy and "Nolock"
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- How to use Enums with Dynamic Linq?
- How to use IEnumerable.Cast<> and .ToArray() to convert int array to an enum array?
More Query from same tag
- Datatable and LINQ Parent / Child relationship
- Linq Looping through Data
- C# Explanation about a lambda expression
- LINQ, to check if this characters contains in my some tables in my DB
- Map parts of an aggregate to a List<T>
- Is there an easy way to parse a (lambda expression) string into an Action delegate?
- translate sql to linq extension method inner join with select group by
- Type member support in LINQ-to-Entities?
- How to write aggregate query in LINQ reusing part of the select code
- Read Anonymous Type from DataItem
- What alternatives does Java provide for C# expressions?
- EF: How to use custom selectors to project related entities?
- Better way to check if a list of IDs is valid (EF Core)
- LINQ query optimization
- Linq Outer Join on object array
- syntax in LINQ IEnumerable<string>
- How to define this 'var' type formally?
- Determine the root object of a MemberExpression in a LINQ Expression Tree
- LINQ: How to convert the nested hierarchical object to flatten object
- How do you avoid Client Evaluation Errors in EF Core when Ordering by a Child property?
- C# equivalent of C++ std::string find
- LINQ SingleOrDefault() equivalent
- InvalidCastException from very simple linq query
- How to get object field for LINQ cached object?
- String was not recognized as a valid DateTime - Whats wrong?
- Getting Value using Linq to Xml
- Attribute of LINQ result is NULL but relationship is working
- Deadlock executing Query in Sql Server
- How do I construct a nested, strongly typed object from a flat ("non-normalized") array?
- sort string as number in linq to entity