Breaking

Friday, May 26, 2017

Instructions to actualize a sort safe enum design in C#

The specifically enum example to get around the inadequacies of the enum sort and give a sort protected, exquisite and adaptable approach to speak to enums in your applications.


An enum is an esteem sort. Esteem sorts in .Net are by and large put away in the stack. You regularly utilize enums to speak to named constants in your application. There are two sorts of enums: basic enums and banner enums. While the previous sort is utilized to speak to a shut arrangement of qualities, the last is utilized to offer help for bitwise operations utilizing the enum values. 

This article displays a dialog on enums, what they are, the reason they are valuable, and the plan imperatives when utilizing enums in applications and how to actualize a sort safe enum design with code cases wherever suitable. 

Why would it be a good idea for us to utilize enums? How are they useful? 

Enums are useful as you can set your area particular catchphrases that would be dealt with as number constants - these all help you to compose perfect, clear code in your application. You can utilize enums in switch articulations also. You can utilize enums in lieu of static constants in your application. Here's a decent article from MSDN that layouts the plan rules that ought to be clung to when working with enums: https://blogs.msdn.microsoft.com/kcwalina/2004/05/18/outline rules refresh enum-plan/ 

Outline and utilization limitations 

While enums are incredible in helping you to compose perfect, clear code in your applications, they do have certain requirements too. The .Net system speaks to enums as whole numbers. In this way, regardless of the possibility that you announce an enum as having certain whole number qualities, it is highly unlikely you can keep your engineer from allotting some other whole number an incentive to the enum you have pronounced. The other plan imperative in utilizing enums is that enums are not extendable. Here's precisely where the Strongly Typed Enum configuration design acts the hero. 

The specifically enum design 

The specifically enum design or the sort safe enum design as it is called, can be utilized to moderate the outline and use limitations we talked about in the before segment when working with enums. This example ensures that the sort is extensible and you can utilize it much like an enum. 

Envision that you were to assemble a custom lumberjack that can log information to different arranged log targets. Such log targets can be a content document, database or the occasion log. To plan such a system you might need to exploit an enum to pronounce the different log focuses on that the structure ought to offer help for. Since we definitely know the outline and utilization requirements of an enum, how about we figure out how we can manufacture a sort – safe portrayal of the same. Allude to the LogTarget class given beneath. 

open fixed class LogTarget 


open static readonly LogTarget Database = new LogTarget("Database"); 

open static readonly LogTarget EventLog = new LogTarget("EventLog"); 

open static readonly LogTarget File = new LogTarget("File"); 

open readonly string TargetName; 

private LogTarget(string targetName) 


TargetName = targetName; 



Take note of that the class has been set apart as "fixed" to forestall facilitate legacy. The constructor is private to anticipate instantiation as an occasion of this class is not required. You simply have a couple of open readonly fields that compare to a specific LogTarget. 

Expect now that you have executed a class named CustomeLogger as demonstrated as follows. Take note of this is only for delineation purposes just - it is only a spurious class and is deficient. 

open class CustomLogger 


/Define the individuals from the class here 

open static void Log(string information, logTarget) 


/Write the important code here to log information to the proper log target 



The accompanying code bit indicates how you can utilize the LogTarget class we characterized before and call the Log technique on the CustomLogger class. 

string information = "Information to be logged."; 

CustomLogger.Log(data, LogTarget.File); 

The provisos 

Yet the way that this plan is adaptable, there are sure provisos in utilizing this example. You ought to know this is nullaable (not at all like an enum) and the qualities (the log focuses on that we have characterized in the LogTarget class) can't be utilized as a part of "switch - case" articulations. Indeed, even determination and serialization of items that execute the example we examined in this post would require you to keep in touch with some additional code.



No comments:

Post a Comment