Posts

Showing posts with the label C#

Hidden things about Constructor in C#

Image
  What's constructor? It's a special method used to initialize an object or struct, limit access to object or control object or structs by set default values, In brief it's like the key to the door, let's say you want to run your car what do you need to do this? of sure you need the Key to run, so the Constructor in this case to run function of the Car has a Parameter called Key, without the Key you cannot run public class Car { string _key; public Car(string key){ // the constructor, accepts string key as parameter _key=key; } } Parameterless constructors: If you didn't create any constructors in your class, then C# creates the default P arameterless Constructor that initialize C# types default values, and the same on Structs, and if you have Static Fields then C# by default creates Static Constructor to initialize those Fields into default value   ...

Double VS Decimal In C#

Image
If you are using double or float in financial arithmetic operations, please stop ⛔  this !! the basic difference between double and decimal is that decimal and float is a numeric type with base 2 that means only number with base 2 are expressible in these types while in our daily basis we use numbers with base 10, 0-9 numbers Sample code: decimal dcm = 4m / 12m; // 0.3333333333333333333333333333 double dbl = 4.0 / 12.0;// 0.333333333333333 dcm + dcm + dcm + dcm + dcm + dcm; //1.9999999999999999999999999998 dbl + dbl + dbl + dbl + dbl + dbl; //2 As you can see in the upper code there's a rounding error,  the correct value should be 1.9  while in double it was 2 References: Microsoft

C# Integral Types That Lack Arithmetic Operations

Image
In C# there's some types that lack arithmetic operations These Integral types are 8 and 16 Bit so it's converted implicitly to the largest types, below is the list of these types : byte sbyte short ushort Sample of code: short a=10,b=10; short sum = a+b; // compile time error The solution: as mentioned a and b   variables   are implicitly  converted to the largest type "Int" , to solve this we have to use the explicit casting on the total result: short a=10,b=10; short sum = (short)(a+b); // compiled successfully References: Microsoft

C# Numeric types Overflow

Image
will this code be compiled? int x= int.MaxValue; x=x+1; The answer is, yes it will be compiled  I will tell you how, at runtime arithmetic operations on Integral types can overflow, and this happens silently no exceptions will be thrown, and the results exhibits wraparound behavior that means  int x= int.MaxValue; x= x+1; (x == int.MinValue) // True and if we do also the below expressions it will not give you the right result int a = 80000;  int b = 80000;  int c = a * b;  // 2105032704 while the right result is  6,400,000,000 So to avoid this issue we will use Checked Operator int a = 80000;  int b = 80000;  int c = checked(a * b);  // Run-time exception (line 9): Arithmetic operation resulted in an overflow Checked: The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic ...

Convert JSON to C# Class In Visual Studio

Image
I was working on Integration with one of the social media This social media provider was returning me a big complex  JSON object I wanted to deserialize this object, and if I want to convert the JSON object into C# class then this will take sometime from me,  So I found a way to do this from Visual Studio,  follow me to know how to do it 👇 Sample JSON data: {"widget": { "debug": "on", "window": { "title": "Sample Konfabulator Widget", "name": "main_window", "width": 500, "height": 500 }, "image": { "src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center" }, "text": { "data": "Click Here", "size": 36, ...

Sitecore Multi list Code Data Source

Image
Recently, in my project, I had a requirement where, I had to show Items in a list dynamically based on its parent, and we are currently using Multi list with search, and I found it's better to use a custom data source as the query and fast query doesn't work for my case, so, I used the "code" Data source as it's the best solution for my case Below is an example of how I implemented this approach: Case: My case was to map the department with it's company, so I put the path of the company on the parent of the department Code: firstly, you have to add reference for  Sitecore.Bucket.dll as we will implement IDatasource interface public class DepartmentCustomDataSource : IDataSource { public Item[] ListQuery(Item item) { // department company Item parent = item.Parent...