Skip to main content

Playing with a .NET types definition

In the last few days I spent some time trying to unify structure of one of the project I`m currently working on. Most of the changes were about changing variable types because it`s were not used right way. That is why in this post I want to share my observations and practices with you.

First of all we need to understand what 'variable definition' is and how it`s different from 'variable initialization'. This part should be pretty straightforward:
  •  variable definition consist of data type and variable name only <data_type> <variable_name>; for example int i; . It`s important to understand how variable definition affects your code because it behaves differently depends weather you work with value or reference types. In the case of value types after defining variable it always has default value and it`s never null value. However after defined reference type variable without initializing it has null value by default.
  • variable initialization is a process of assigning value to the variable - variable_name = value; it can be merged with the definition into one line expression <data_type> <variable_name> = value;
Code Snippet
  1. public class Test
  2. {
  3.     public Test()
  4.     {
  5.         // variable definition for value type.
  6.         int i; // i == 0 as this stage.
  7.  
  8.         // variable definition for reference type.
  9.         string s; // s == null at this stage.
  10.  
  11.         // value type variable initialization
  12.         i = 10; // i == 10.
  13.  
  14.         // referece type variable initialization
  15.         s = "Test";
  16.     }
  17. }

Now we can go forward and compare a different types of a variable definition. As many of you remember in the past, in .NET Framework 2.0, only one type of variable definition exists. Here I mean classic, strongly typed, combination of type and variable name (example below line 13.). It was very popular (because at this time there was nothing else to replace it) but this approach requires creating separated type for each problem. This cause in many project multiple very similar types exists so it was not a perfect solution.

Code Snippet
  1. public class Test
  2. {
  3.     public class SomeType
  4.     {
  5.         internal void DoSomething()
  6.         {
  7.         
  8.         }
  9.     }
  10.  
  11.     public Test()
  12.     {
  13.         SomeType type = new SomeType();
  14.         type.DoSomething();
  15.     }
  16. }

Since .NET Framework 3.0 developers was  able to switch their mind into different kind of thinking about defining variables because a new var keyword became in use. A var keyword  is separated type of defining variables, which are still strongly typed but it`s resolved by the compiler based on value assigned to it during initialization (a var variable have to be defined and initialized in the same line of code). Why is so cool to use it? This is a  very good question and a lot discussions around that already exists in the Internet. First of all variables defined with var keyword in general have more descriptive name because in other case even developer might have a problem with reading own code. Let see an example:

Code Snippet
  1. using System.Collections.Generic;
  2. public class Test
  3. {
  4.     public class SomeType
  5.     {
  6.         internal void DoSomething()
  7.         {
  8.         
  9.         }
  10.     }
  11.  
  12.     public Test()
  13.     {
  14.  
  15.         // SomeType someType = new SomeType();
  16.         var someType = new SomeType(); // equivalent to line above.
  17.         var someTypeList= new List<SomeType>();
  18.     }
  19. }

However the real power of using var keyword ins`t naming convention but something which is called anonymous types. It`s really powerful tool in developers hands!! Anonymous types are generated by the compiler, are strongly typed and not exists physically as a class or structure in a project code but provide a convenient way to encapsulate a set of read-only properties into a single object. Let`s see example of using it:
Code Snippet
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System;
  4. public class Test
  5. {
  6.     public class User
  7.     {
  8.         public int Id { get; set; }
  9.         public string Name { get; set; }
  10.         public string Surname { get; set; }
  11.         public string Department { get; set; }
  12.         public byte[] HugeArray { get; set; }
  13.     }
  14.  
  15.     public Test()
  16.     {
  17.         var userList = new List<User>();
  18.  
  19.         // code to populate the list.
  20.  
  21.         // Creating a new anonymous type with HrId and FullName properties.
  22.         var userInHrDepartment = from user in userList
  23.                                  where user.Department == "HR"
  24.                                  select new { HrId = user.Id, FullName = user.Name + " " + user.Surname };
  25.  
  26.         // Tterate through collection of anonymous type.
  27.         foreach (var hrUser in userInHrDepartment)
  28.         {
  29.             Console.WriteLine(hrUser.FullName);
  30.         }
  31.     }
  32. }

As you can see by using a very simple LINQ query I`m extracting only variable I really want to process, and without creating strong type explicit I`m using anonymous type to iterate through the collection and accessing specific properties.

Last type of a variable definitions comes with .NET Framework 4.0 which introduce a new dynamic type.
This is completely different type of thinking for developers because this type of object behaves very similar to the well know object type.  However the difference between those two is huge because object is so powerful because of inheritance while dynamic type is not strongly typedlate-bound and exists only at run-time. Basically it`s a bag where we can put everything and expect everything but we can`t be sure that something exist there. Let see dynamic type in action.

Code Snippet
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System;
  4. public class Test
  5. {
  6.     public class User
  7.     {
  8.         public int Id { get; set; }
  9.         public string Name { get; set; }
  10.         public string Surname { get; set; }
  11.         public string Department { get; set; }
  12.         public byte[] HugeArray { get; set; }
  13.     }
  14.  
  15.     public Test()
  16.     {
  17.         var user = new User();
  18.         dynamic userModyfied = user;
  19.  
  20.         // Adding extra property to the User type.
  21.         userModyfied.AdditionalProperty = "Test value";
  22.         Console.WriteLine(userModyfied.AdditionalProperty);
  23.  
  24.         dynamic userModyfied2 = new User();
  25.         userModyfied2.AdditionalProperty2 = "Test value 2";
  26.  
  27.         Console.WriteLine(userModyfied.AdditionalProperty2); // value exists
  28.         Console.WriteLine(userModyfied.AdditionalProperty); // value doesn`t exist
  29.     }
  30. }

As you can see in the example above. This is very strange type and might bring a lot or troubles when used incorrectly. Additionally usage it  hurts performance  of our solution so use it wisely only if necessary.

Thank you

Popular posts from this blog

Full-Text Search with PDF in Microsoft SQL Server

Last week I get interesting task to develop. The task was to search input text in PDF file stored in database as FileStream. The task implementation took me some time so I decided to share it with other developers. Here we are going to use SQL Server 2008 R2 (x64 Developers Edition), external driver from Adobe, Full-Text Search technology and FileStream technology.Because this sems a little bit comlicated let`s make this topic clear and do it step by step. 1) Enable FileStream - this part is pretty easy, just check wheter You already have enabled filestream on Your SQL Server instance - if no simply enable it as in the picture below. Picture 1. Enable filestream in SQL Server instance. 2) Create SQL table to store files  - mainly ther will be PDF file stored but some others is also be allright. Out table DocumentFile will be created in dbo schema and contain one column primary key with default value as sequential GUID. Important this is out table contains FileStream

Persisting Enum in database with Entity Framework

Problem statement We all want to write clean code and follow best coding practices. This all engineers 'North Star' goal which in many cases can not be easily achievable because of many potential difficulties with converting our ideas/good practices into working solutions.  One of an example I recently came across was about using ASP.NET Core and Entity Framework 5 to store Enum values in a relational database (like Azure SQL). Why is this a problem you might ask... and my answer here is that you want to work with Enum types in your code but persist an integer in your databases. You can think about in that way. Why we use data types at all when everything could be just a string which is getting converted into a desirable type when needed. This 'all-string' approach is of course a huge anti-pattern and a bad practice for many reasons with few being: degraded performance, increased storage space, increased code duplication.  Pre-requirements 1. Status enum type definition