I came from C/C++, and used alot things like #define OBJ_STATE_INPROCESS 2, so that when coding actual logic you can have state = OBJ_STATE_INPROCESS;, and what it does become more obvious than state = 2;, which makes the code easier to maintain.
I wonder if there is some trick like this in C#
Though technically a different concept, in C# you can use contants and enums to avoid "magic numbers", e.g.
public static class Constants
{
public const string MyConst = "ThisIsMyConst";
}
public enum MyEnum
{
MyEnumValue1,
MyEnumValue2,
}
// Usage
var value = MyEnum.MyEnumValue2;
You can accomplish that using constant properties, for example:
static class Constants
{
public const int OBJ_STATE_INPROCESS = 2
}
class Program
{
static void Main()
{
Console.WriteLine(Constants.OBJ_STATE_INPROCESS ); //prints 2
}
}
Related
In Java, it's possible to have methods inside an enum.
Is there such possibility in C# or is it just a string collection and that's it?
I tried to override ToString() but it does not compile. Does someone have a simple code sample?
You can write extension methods for enum types:
enum Stuff
{
Thing1,
Thing2
}
static class StuffMethods
{
public static String GetString(this Stuff s1)
{
switch (s1)
{
case Stuff.Thing1:
return "Yeah!";
case Stuff.Thing2:
return "Okay!";
default:
return "What?!";
}
}
}
class Program
{
static void Main(string[] args)
{
Stuff thing = Stuff.Thing1;
String str = thing.GetString();
}
}
You can write an extension method for your enum:
How to: Create a New Method for an Enumeration (C# Programming Guide)
Another option is to use the Enumeration Class created by Jimmy Bogard.
Basically, you must create a class that inherits from his Enumeration. Example:
public class EmployeeType : Enumeration
{
public static readonly EmployeeType Manager
= new EmployeeType(0, "Manager");
public static readonly EmployeeType Servant
= new EmployeeType(1, "Servant");
public static readonly EmployeeType Assistant
= new EmployeeType(2, "Assistant to the Regional Manager");
private EmployeeType() { }
private EmployeeType(int value, string displayName) : base(value, displayName) { }
// Your method...
public override string ToString()
{
return $"{value} - {displayName}!";
}
}
Then you can use it like an enum, with the possibility to put methods inside it (among another things):
EmployeeType.Manager.ToString();
//0 - Manager
EmployeeType.Servant.ToString();
//1 - Servant
EmployeeType.Assistant.ToString();
//2 - Assistant to the Regional Manager
You can download it with NuGet.
Although this implementation is not native in the language, the syntax (construction and usage) is pretty close to languages that implement enums natively better than C# (Kotlin for example).
Nope. You can create a class, then add a bunch of properties to the class to somewhat emulate an enum, but thats not really the same thing.
class MyClass
{
public string MyString1 { get{ return "one";} }
public string MyString2 { get{ return "two";} }
public string MyString3 { get{ return "three";} }
public void MyMethod()
{
// do something.
}
}
A better pattern would be to put your methods in a class separate from your emum.
Since I came across, and needed the exact opposite of enum to string, here is a Generic solution:
static class EnumExtensions {
public static T GetEnum<T>(this string itemName) {
return (T) Enum.Parse(typeof(T), itemName, true);
}
}
This also ignores case and is very handy for parsing REST-Response to your enum to obtain more type safety.
Hopefully it helps someone
C# Does not allow use of methods in enumerators as it is not a class based principle, but rather an 2 dimensional array with a string and value.
Use of classes is highly discouraged by Microsoft in this case, use (data)struct(ures) instead; The STRUCT is intended as a light class for data and its handlers and can handle functions just fine. C# and its compiler don't have the tracking and efficiency capabilities as one knows from JAVA, where the more times a certain class / method is used the faster it runs and its use becomes 'anticipated'. C# simply doesn't have that, so to differentiate, use STRUCT instead of CLASS.
I'm thinking what is the best way to compare values from two different enums.
Example:
public enum ExampleEnumA
{
ExampleValue
}
public enum ExampleEnumB
{
ExampleValue
}
if (ExampleEnumA.ExampleValue.ToString() == ExampleEnumB.ExampleValue.ToString())
{
}
Comparing strings work, but I know that it's not the most efficient and eligent way. How it can be done better?
EDIT:
Maybe it's a design flaw, but it's problem from a real project, not my incorrect understanding of enums. This is how it looks like and there was no time to refactor whole approach.
public interface IProvider
{
Enum SectionType { get; }
}
public class FirstProvider : IProvider
{
public Enum SectionType
{
get { return ExampleEnumA.ExampleValue; }
}
}
public class SecondProvider : IProvider
{
public Enum SectionType
{
get { return ExampleEnumB.ExampleValue; }
}
}
public class Program
{
public void TmpMethod(Enum sectionType)
{
var provider = GetFromIoC...
if (provider.SectionType == sectionType)
{
//...
}
}
}
Enumerations are like an abstraction layer on top a regular class of integer-based constants.
That abstraction includes evaluating false even if two enumeraiton values are the same integer but belong to different enumeration types.
What's the best way of comparing two different enumeration types with same underlying value? I would answer that it should be a design flaw if you need to perform this evaluation.
For example, let's say we've implemented these enumerations:
public enum States
{
Open = 1,
Closed
}
public enum SpecialFolders
{
ProgramFiles86 = 1,
ProgramFiles64
}
Would make sense something like States.Open == SpecialFolders.ProgramFiles86? Potentially, they seem to be equal (they won't) because both enumeration values have an underlying value of 1, but 1 doesn't mean the same if the enumeration type isn't the same.
It's like saying that...
Bread
Meat
...is the same as:
Steal (Bread == Steal????????????)
Wood
Maybe...
...you can defeat the purpose of typing constants as enumerations casting them to int:
if ((int)ExampleEnumA.ExampleValue == (int)ExampleEnumB.ExampleValue)
{
}
...if the underlying type is int. It could also be long:
public enum SomeEnum : long
{
}
...and you would need to cast left and right part of your evaluation to long, and so on.
Anyway, I insist that you shouldn0t go this way. Maybe you should use a regular class of constants instead and you'll avoid 2 casts per evaluation:
public static class States
{
public const int Open = 1;
public const int Closed = 2;
}
public static class Materials
{
public const int Steel = 1;
public const int Wood = 1;
}
// true! but not that true... I can't understand why these constants equal...
if(States.Open == Materials.Wood)
{
}
BTW, I still consider this a design flaw and you should avoid not using enumerations to workaround a bad design decision.
How do I declare a variable so that every class (*.cs) can access its content, without an instance reference?
In C# you cannot define true global variables (in the sense that they don't belong to any class).
This being said, the simplest approach that I know to mimic this feature consists in using a static class, as follows:
public static class Globals
{
public const Int32 BUFFER_SIZE = 512; // Unmodifiable
public static String FILE_NAME = "Output.txt"; // Modifiable
public static readonly String CODE_PREFIX = "US-"; // Unmodifiable
}
You can then retrieve the defined values anywhere in your code (provided it's part of the same namespace):
String code = Globals.CODE_PREFIX + value.ToString();
In order to deal with different namespaces, you can either:
declare the Globals class without including it into a specific namespace (so that it will be placed in the global application namespace);
insert the proper using directive for retrieving the variables from another namespace.
You can have static members if you want:
public static class MyStaticValues
{
public static bool MyStaticBool {get;set;}
}
First examine if you really need a global variable instead using it blatantly without consideration to your software architecture.
Let's assuming it passes the test. Depending on usage, Globals can be hard to debug with race conditions and many other "bad things", it's best to approach them from an angle where you're prepared to handle such bad things. So,
Wrap all such Global variables into a single static class (for manageability).
Have Properties instead of fields(='variables'). This way you have some mechanisms to address any issues with concurrent writes to Globals in the future.
The basic outline for such a class would be:
public class Globals
{
private static bool _expired;
public static bool Expired
{
get
{
// Reads are usually simple
return _expired;
}
set
{
// You can add logic here for race conditions,
// or other measurements
_expired = value;
}
}
// Perhaps extend this to have Read-Modify-Write static methods
// for data integrity during concurrency? Situational.
}
Usage from other classes (within same namespace)
// Read
bool areWeAlive = Globals.Expired;
// Write
// past deadline
Globals.Expired = true;
A useful feature for this is using static
As others have said, you have to create a class for your globals:
public static class Globals {
public const float PI = 3.14;
}
But you can import it like this in order to no longer write the class name in front of its static properties:
using static Globals;
[...]
Console.WriteLine("Pi is " + PI);
I have two .cs files (Hex2Bin.cs and Program.cs) and I want to pass the variable end_addr from Program.cs to Hex2Bin.cs
My code in Program.cs:
class Program
{
enum to_exit {
exit_ok = 0,
exit_invalid_args,
exit_to_few_args,
exit_invalid_input_file,
exit_invalid_args_file,
exit_permission_denied,
exit_unexpected_eof
};
// class value holders
static String args_file_name = "";
static String in_u1_name = "";
static String in_u22_name = "";
static String out_name = "";
static short end_addr = 0x0000; // 4-digit Hexadecimal end address
static Byte[] version_code = { 0, 0, 0, 0 }; // 3 bytes version, 1 for extra info
}
Is there anyway I could do this? I know how to do it in c, but I'm very new to c#. Thanks.
C# doesn't work like C with respect to static variables. You can make the variable end_addr available outside the Program class by making it a public field. By default, fields are private.
public static end_addr = 0x0000;
And then it can be accessed like so:
var x = Program.end_addr;
However, I would recommend that you spend a little more time familiarizing yourself with C# idioms and conventions. It seems like your still thinking about C# in terms of C, and they are very different.
if you declare the variable like this:
public static short end_addr = 0x0000;
then from another class you can use it like this:
Program.end_addr
but don't do this, is not object oriented!
if your class Hex2Bin is used/invoked by the Main method of Program class, you should be able to pass your variables as input parameters of the methods you call or set them as properties of the classes/objects you use...
It's enough to mark end_addr as public like so
public static short end_addr = 0x0000;
Then you can access it from anywhere like this
Program.end_addr
It's a better practice though to use properties rather than fields for exposing data.
// Property
public static short end_addr { get; private set; }
// Constructor
public Program()
{
// Initialize property value.
end_addr = 0x0000;
}
You're talking about 'files' but what you really want to do is to pass data from your program's entry point (Program.cs) to a an object of a class (or method of static class) that will process the data, am I right?
If so, this should be pretty simple. You either have to modify your Program.cs and create an instance of the class (the one from Hex2Bin.cs) like this
...
Hex2Bin hex2bin = new Hex2Bin( end_addr );
...
I assume that the Hex2Bin is as follows:
public class Hex2Bin
{
private short endAddress;
public Hex2Bin( short endAddress )
{
this.endAddress = endAddress;
}
}
this will allow you to use the value of end_addr from Program.cs
Another approach is to pass it directly to the method that will make use of it:
Hex2Bin.Method(end_addr);
and in the Hex2Bin file:
public static void Method(short endAddress)
{
//... do the work here
}
Given your background in C, I think you may be mixing runtime with compile time issues.
However, in Hex2Bin.cs, you can create a static method that updates a static variable.
class Hex2Bin
{
static short end_addr = 0x0000;
static void updateEndAddr(short endAddr)
{
end_addr = endAddr;
}
}
I'd like to offer a list of constants within my DLL.
Example usage:
MyDLL.AddHouse( HouseName, MyDll.HOUSETYPES.Big)
MyDLL.AddHouse( HouseName, MyDll.HOUSETYPES.Small)
Tried:
public static readonly string[] HOUSETYPES =
{
"Big", "Small"
};
But that only gets me:
MyDLL.AddHouse( HouseName, MyDll.HOUSETYPES.ToString())
Any ideas? Thanks.
Try using an enumeration. In C# this is the best option.
As the enumerations are strongly typed, instead of having an API that takes a string, your api will take a value of the type of your enumeration.
public enum HouseTypes
{
Big,
Small
}
MyDll.Function(HouseTypes Option)
{
}
You can then call this code via the enum
{
MyDll.Function(HouseTypes.Big)
}
FYI as a coding style all caps in C# is reserved for constants only.
public static class HouseTypes
{
public const string Big = "Big";
public const string Small = "Small";
}
It is a good idea to follow .NET naming standards for naming your classes and variables. E.g. class will be called HouseTypes (Pascal Case) and not HOUSETYPES (Upper case).