Skip to main content

Posts

Showing posts with the label C# Fundamentals

Web API crud operation in .Net 6

To create a Web API in .NET 6, you will need to perform the following steps: 1. Install the .NET 6 SDK: To install the .NET 6 SDK, you can use the following command: dotnet new -i Microsoft.AspNetCore.Blazor. Templates :: 6.0 . 0 -preview. 7.20480 . 1 2. Create a new Web API project: To create a new Web API project, you can use the following command: dotnet new webapi -o MyWebApi This will create a new Web API project in a directory called "MyWebApi". Add the necessary dependencies: To add the necessary dependencies for your Web API, you can use the following command: dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer Set up the database: To set up the database for your Web API, you will need to create a model class that represents the data you want to store. For example, you might create a model class called "Product" with properties like "Name" and "Price". Next, you will need to c...

Extended property patterns in c#

In C#, extended property patterns allow you to use the is operator to check if an object has a particular property and access the value of that property. This can be useful when working with objects that have many properties and you only need to access a specific property. Here's an example of how to use extended property patterns in C#: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } Person person = new Person { FirstName = "John", LastName = "Doe" }; if (person is { FirstName: string firstName }) { Console.WriteLine($"First name: {firstName}"); } In this example, the if statement uses an extended property pattern to check if the person object has a FirstName property of type string. If the person object has a FirstName property, the value of the property is assigned to the firstName variable, which is then used to print the first name to the console. You can also use extended property patterns ...

Constant interpolated strings in c#

In C#, you can use constant interpolated strings to create a string that contains constant values. Constant interpolated strings are similar to regular interpolated strings, but they are marked with the const keyword and can only contain constant values. Here's an example of how to use a constant interpolated string in C#: Copy code const int num = 5; const string message = $"The value of num is {num}"; Console.WriteLine(message); This will output the string "The value of num is 5" to the console. Note that you can only use constant values in a constant interpolated string. If you try to use a non-constant value, you will get a compile-time error. int nonConstant = 10; const string message = $"The value of nonConstant is {nonConstant}"; // Error: nonConstant is not a constant Constant interpolated strings can be useful when you want to create a string that contains constant values and you want to ensure that the string cannot be modified at runtime....

IndexOutOfRangeException in C#

The IndexOutOfRangeException is a type of exception that is thrown when you try to access an index of an array or a collection that is outside its bounds. This can happen if you try to access an element at a negative index, or if you try to access an element at an index that is greater than or equal to the length of the array or collection. Here is an example of how this exception might be thrown: int[] numbers = { 1, 2, 3 }; try { int x = numbers[3]; } catch (IndexOutOfRangeException ex) { Console.WriteLine("An index out of range exception occurred."); Console.WriteLine(ex.Message); } In this example, the numbers array has a length of 3, so the valid indices for the array are 0, 1, and 2. However, we are trying to access the element at index 3, which is outside the bounds of the array. This will cause the IndexOutOfRangeException to be thrown. To prevent this exception from being thrown, you can use bounds checking to ensure that you are not trying to access an ...

Difference between static, readonly, and constant in C#

In C#, the static keyword is used to declare a static field, which is a field that is shared by all instances of a class. A static field has a single value that is common to all instances of a class, and it is initialized when the class is first loaded. The readonly keyword is used to declare a read-only field, which is a field that can only be initialized once, either at the time of declaration or in the class's constructor. A read-only field can be a static field or an instance field, and it can be accessed from any instance of the class. The const keyword is used to declare a constant field, which is a field whose value is set at compile time and cannot be modified at runtime. A constant field can only be of one of the following types: bool, char, string, int, long, float, or double. Here is an example of how these keywords can be used in C#: public class MyClass { // A static field that is shared by all instances of the class public static int s_staticField; // A r...

Working with Date and Time in C#

 In C#, the System.DateTime structure represents an instant in time, typically expressed as a date and time of day. You can use the DateTime structure to perform a variety of tasks, such as comparing dates, performing arithmetic with dates, and formatting dates for display. Here are some examples of common tasks you might perform with DateTime in C#: Getting the current date and time: DateTime currentTime = DateTime.Now; Creating a DateTime object for a specific date and time: DateTime newYear = new DateTime(2022, 1, 1, 0, 0, 0); Performing arithmetic with dates: DateTime oneWeekFromNow = currentTime.AddDays(7); DateTime oneWeekAgo = currentTime.AddDays(-7); Comparing dates: bool isNewYear = (newYear > currentTime); Formatting dates for display: string formattedDate = currentTime.ToString("MM/dd/yyyy HH:mm:ss"); There are many more features and capabilities of the DateTime structure in C#. You can find more information in the Microsoft documentation.