Skip to main content

Posts

Showing posts from 2022

Entity Framework 6.0 Logging and database Operations Interception

  Entity Framework 6.0 Logging and database Operations Interception Introduction Entity Framework 6.0 introduced a new feature called "Logging SQL". While working with Entity Framework, it sends commands or an equivalent SQL query to the database to do a CRUD operation and this command can be intercepted by application code of Entity Framework. How to enable the SQL logging The Database Log property of DbContext can be set to a delegate of any method that accepts a string as the parameter. Using this method, all SQL generated by the current context will be logged. Syntax:             public DbContextEntities () : base ( "name=EntitiesConnections" )         {             Database.Log = delegate for method which accepts string as parameter;         }   Example: For instance, if ...

TimeSpan in C#

  TimeSpan in C# The struct TimeSpan in C# represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. The TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.   Creating TimeSpan   TimeSpan struct has the following overloaded forms:   The following code snippet crates a TimeSpan from days, hours, and minutes. The common method of creating a TimeSpan is finding the difference between two DateTime objects, The following code snippet is example of getting the interval between two DateTime objects. TimeSpan Properties   C# TimeSpan class properties are Days, Hours, Minutes, Seconds, Milliseconds, and Ticks that returns days, hours, minutes, seconds, and milliseconds in a TimeSpan object. The TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds properies return the totals of them on an object.   The following code snippet...

Record in C#

  Record in C# Introduction With the release of C# 9, Microsoft introduced records. The record keyword gives a reference type new superpowers like immutability declared with positional records (or by using init-only properties), equality comparisons that mimic value types, and with-expressions that allows you to create a new record instance with the same property values, the properties you need to change. This drastically simplifies the process to copy objects.   The release of C# 10 brings records structs. They’ll carry a lot of the advantages of C# 9 records (which are reference types, like classes), but there are differences with structs because, structs are different from classes! In this article, we will see what a record struct is, and why a record class doesn’t behave like a record struct. Syntax First of all, Microsoft has made an improvement to record classes. With C# 9, to declare a record you replaced the “class” keyword with “record.” To avoid confusion...

Yield Return in C# Made easy

  Yield Return in C# Made easy The CSharpNaija bring yet another important keyword in C# that is, yield return type. In the article below we will learn some programming techniques that revolve around C# yield return keywords. What MSDN has about Yield "The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement. The yield keyword is also used with break to signal the end of iteration." Operation of yield When combined with the return mechanism allowing creation, the function returns a value that handles certain information in the loop, and puts an end to certain conditions before it. This is contrary to the case of a norm...