Tag: C#
How to make a Type Sortable on Different Criteria in C# ?
IComparable interface is a great way to sort your objects by using the sorting routines of the List. There is a another interface called IComparer<T> which allows the objects to be sorted based on different criteria. How to make a Type Sortable on Different Criteria in C# ? For example , if you wanted to sort the Employee objects by Id , you can create…
Asp.net GridView example using C#
Below is a sample code snippet for Asp.net GridView example using C#. The code snippet shows how to populate the GridView with a List of Employees. Asp.net GridView example using C# ASP.NET WebForms <asp:GridView ID=”GridView1″ runat=”server”> </asp:GridView> C# protected void Page_Load(object sender, EventArgs e) { List<Employee> employees = new List<Employee>(); employees.Add(new Employee { Name = “Test1”, Designtaion = “Software Engineer” }); employees.Add(new Employee { Name…
How to Find if an Integer Is Even using Bitwise Operator in C# ?
Below is a sample code snippet that demonstrates how to find if an Integer is even using Bitwise Operator in C#. How to Find if an Integer Is Even using Bitwise Operator in C# ?
How do Specify the Exit Code of Console Application in .NET?
There are times when you want to specify the exit code from the Main function for example when working on the Console Application in .NET . How do Specify the Exit Code of Console Application in .NET? You can use the Environment.Exit to specify the exit code in the Console Application.
How to Notify Clients when there is a change using C# ?
There are times when you want the users who use the class wants some kind of indication or notification when there is a change in data inside it . One of the solutions for this is to make the class implement INotifyPropertyChanged interface which is located in the namespace System.ComponentModel. One of the primary use of the INotifyPropertyChanged is in the WPF / Silverlight /…
How to Catch an Exception in C# ?
Every time when the exception is thrown , it is necessary that it be handled . In this case , one needs to wrap the code with the try catch block as shown below. How to Catch an Exception in C# ?
Example for Documentation Tags
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { #region testregion /// <summary> /// This is a test Class Form1 /// </summary> public partial class Form1 : Form { /// <summary> /// Constructor for the Form1 /// </summary> public Form1() { InitializeComponent(); } /// <summary> /// Form Load Event of the…
Json.NET & C# – How to Serialize a Collection?
Do you want to serialize an collection in your C# application?. Json.NET supports this functionality with ease. The Collection can be an Array , Dictionary or List. You need to simply pass collection to the JsonConvert.SerializeObject static method which would serialize the collection and return you the Json string. How to Serialize a Collection in C# using JSON.NET ? For example , assume that you…
How to use Find method with Lambda Expressions in C#?
Below is a sample source code demonstrating how to use Find method with Lambda Expression in C# How to use Find method with Lambda Expressions in C#?
Object Initializer in C#
Below is a sample sourecode demonstrating the Object Initializer in C# Object Initializer in C#
How to make a Type Sortable in C# using IComparable interface?
Assume that you have a type which would be stored in a List. You would want to use List.Sort method to sort your data in the List. For example , assume that you have to store objects of type Employee in List<Employee> and sort the List by Age using the Sort method. To do this , we can use he IComparable<T> interface. How to make…
Example of Delegates in C#
Delegates in C# is simply a reference/pointer to a function . One of the typical usage of the delegates is the event handlers in .NET. Example of Delegates in C# Below is a sample code snippet demonstrating the usage of delegates to add and subtract 2 numbers.
How to Calculate MD5 checksum for a file in C# ?
There are times when you want to calculate MD5 checksum for a file in C# and you can easily do that using System.Security.Cryptography.MD5 You can use the ComputeHash method of the MD5 instance by passing the stream. You can later it to Hex using the BitConverter so that you can represent it as string for conversion. How to Calculate MD5 checksum for a file in…
How to Sort dictionary by value in C#?
The Dictionary contains the KeyValuePair and the below is a sample code snippet that demonstrates the sorting of the dictionary entries by value in C#. How to Sort dictionary by value in C#?
Example to demonstrate Collection Initializers in C#
Below is a sample code snippet demonstrating the usage of Collection Initializers in C#. Example to demonstrate Collection Initializers in C#
How to Format a Type with ToString () in C#?
The ToString method of a type will show the type’s name. For example , If the Object is the Employee type , then calling the ToString will display the Type Name along with the name space as shown in the below screenshot. You can override the ToString method to provide you own way to display the data when ToString is called. How to Format a…
How to List Odd Numbers from a List of Integers using LINQ Query in C#?
Are you looking out for a simple logic to retrieve the odd numbers from the list of integers using LINQ Query in C#? Below is a sample code snippet demonstrating how to do it. How to List Odd Numbers from a List of Integers using LINQ Query in C#?
Main function in c# and return type
Below is a sample source code demonstrating the Main function with no return type and return type. Example for return type of void class Program { static void Main(string[] args) { Console.WriteLine(“Return type is void”); } } Example for return type of int static int Main(string[] args) { Console.WriteLine(“Return type is int”); return 0; …
How to Clone a Generic List in C#?
Below is a sample code snippet that demonstrates how to clone a generic list in C#. How to Clone a Generic List in C#?
How to Convert Integer to Hexadecimal number in C# ?
Below is a sample sourecode demonstrating the conversion of the integer value to Hexadecimal number in c# with just one line of code. How to Convert Integer to Hexadecimal number in C# ?