Saturday, June 21, 2014

Fun with Nuances (C# Attributes vs. Properties)!

An attribute is a way to link up data that and a target element of code (class, interface, methods, properties, or others..).

A property is a type of method (accessor) that are used like a public data member.. but they give you a way to read/write/evaluate some private field. This = safety + flexibility in your code.

Properties usually edit the attributes.

Just something new and fun. This is what we use today (some synonymous hybrid mix "thing"):

        // New auto-property after .NET 3.0
        public String Name { get; set; }

In a lot of ways, this is easier, shorter, and MUCH nicer to the eye!

This is what we used to use

        // Old-fashioned attribute (before .NET 3.0)
        private String _title;

        // Old-fashioned property (before .NET 3.0)
        public String Title
        {
            get { return _title; }
            set { _title = value;  }
        }

However, you lose the ability/flexibility to edit the individual parts of it when the attribute used to be wrapped around a layer of property! There are only rare instances you would need this.. but those times do exist!

Here is one for you! For instance.. if you are making a "dummy" database and seeding it -- let's say you were making a nice end to end ASP .NET MVC.. and you did not want to open up a SQL database just yet. This "dummy" database would help you bounce off your ideas for the MVC with its own little pseudo-database that is made just to get things up and running a little quicker on the front end! So.. you could not do this:

        private List _recipes = new List
            {
                new Recipe() {ID = 1, Title = "My yummy food", Directions = "Put directions here.", Ingredients = "Yummy, Delicious, Butter"},
                new Recipe() {ID = 2, Title = "My 2nd yummy food", Directions = "More directions here.", Ingredients = "Yummy, Delicious, Sugah"},
                new Recipe() {ID = 3, Title = "My 3rd yummy food", Directions = "All these directions!", Ingredients = "Yummy, Delicious, Synonym-of-Yummy-Delicious"},
            };

        // THIS WILL NOT WORK below!!! EQUALS SAD little code!!! 
        public List Recipes { get; set; }

.. for the reason stated above (you can't edit the specific attribute and property out!).

However.. this works as a whole!

        private List _recipes = new List
            {
                new Recipe() {ID = 1, Title = "My yummy food", Directions = "Put directions here.", Ingredients = "Yummy, Delicious, Butter"},
                new Recipe() {ID = 2, Title = "My 2nd yummy food", Directions = "More directions here.", Ingredients = "Yummy, Delicious, Sugah"},
                new Recipe() {ID = 3, Title = "My 3rd yummy food", Directions = "All these directions!", Ingredients = "Yummy, Delicious, Synonym-of-Yummy-Delicious"},
            };

        // HAPPY LITTLE CODE ;)
        public List Recipes
        {
            get { return _recipes; }
            set { _recipes = value; }
        }

See how the variable _recipes is actually used now??? This is one of those useful rare instances..