
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2587 Articles for Csharp

866 Views
These are used to import namespaces (or create aliases for namespaces or types).These go at the top of the file, before any declarations.using System; using System.IO; using WinForms = global::System.Windows.Forms; using WinButton = WinForms::Button;The using statement ensures that Dispose() is called even if an exception occurs when you are creating objects and calling methods, properties and so on. Dispose() is a method that is present in the IDisposable interface that helps to implement custom Garbage Collection. In other words, if we are doing some database operation (Insert, Update, Delete) but somehow an exception occurs, then here the using statement closes ... Read More

483 Views
To get the directories C# provides a method Directory.GetDirectoriesDirectory.GetDirectories returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.To get the files, C# provides a method Directory.GetFilesDirectory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectoriesTo get the file length , C# provides a property LengthExamplestatic void Main(string[] args) { string rootPath = @"C:\Users\Koushik\Desktop\TestFolder"; var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories); foreach (string file in files) { long size = ... Read More

901 Views
In synchronous C# code, the exceptions are propagated up the call stack until they reach an appropriate catch block that can handle the exception. However, exception handling in asynchronous methods is not as straightforward.An asynchronous method in C# can have three types of return value: void, Task, and Task. When an exception occurs in an async method that has a return type of Task or Task, the exception object is wrapped in an instance of AggregateException and attached to the Task object.If multiple exceptions are thrown, all of them are stored in the Task object.Example 1static async Task Main(string[] args) ... Read More

25K+ Views
To get the directories C# provides a method Directory.GetDirectories. The Directory.GetDirectories method returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.In the below example * is matches Zero or more characters in that position. SearchOption TopDirectoryOnly .Gets only the top directories and SearchOption AllDirectories .Gets all the top directories and sub directories.Note: The rootPath will be your systems rootPath so create a testfolder and use the rootPath accoridingly.Example 1static void Main (string[] args) { string rootPath = @"C:\Users\Koushik\Desktop\TestFolder"; string[] dirs = Directory.GetDirectories(rootPath, "*", SearchOption.TopDirectoryOnly); ... Read More

9K+ Views
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated.This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.List class represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace.List class can be used to create a collection of different types like integers, strings etc. List class also provides the methods to search, sort, and manipulate lists.Example 1static void Main(string[] args) { List list = new ... Read More

2K+ Views
MaxLength ValidatorEnsures that the length of a particular string property is no longer than the specified value.Only valid on string propertiesString format args:{PropertyName} = The name of the property being validated{MaxLength} = Maximum length{TotalLength} = Number of characters entered{PropertyValue} = The current value of the propertyMinLength ValidatorEnsures that the length of a particular string property is longer than the specified value.Only valid on string properties{PropertyName} = The name of the property being validated{MinLength} = Minimum length{TotalLength} = Number of characters entered{PropertyValue} = The current value of the propertyExamplestatic void Main(string[] args){ List errors = new List(); PersonModel ... Read More

4K+ Views
To specify a validation rule for a particular property, call the RuleFor method, passing a lambda expression that indicates the property that you wish to validateRuleFor(p => p.DateOfBirth)To run the validator, instantiate the validator object and call the Validate method, passing in the object to validate.ValidationResult results = validator.Validate(person);The Validate method returns a ValidationResult object. This contains two propertiesIsValid - a boolean that says whether the validation suceeded.Errors - a collection of ValidationFailure objects containing details about any validation failuresExample 1static void Main(string[] args) { List errors = new List(); PersonModel person = new PersonModel(); person.FirstName ... Read More

2K+ Views
FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logicTo make use of fluent validation we have to install the below packageExample 1static class Program { static void Main (string[] args) { List errors = new List(); PersonModel person = new PersonModel(); person.FirstName = ""; person.LastName = "S"; ... Read More

4K+ Views
To Copy a file, C# provides a method File. CopyFile. Copy has 2 overloadsCopy(String, String) -Copies an existing file to a new file. Overwriting a file of the same name is not allowed.Copy(String, String, Boolean) Copies an existing file to a new file. Overwriting a file of the same name is allowed.Directory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectories.Examplestatic void Main (string[] args) { string rootPath = @"C:\Users\Koushik\Desktop\TestFolder\TestFolderMain1"; var searchSourceFolder = Directory.GetFiles(rootPath, "*.*", SearchOption.TopDirectoryOnly); Console.WriteLine("-------------Source Folder-------------"); foreach (string file in searchSourceFolder){ ... Read More

2K+ Views
The process of injecting (converting) coupled (dependent) objects into decoupled (independent) objects is called Dependency Injection.Types of Dependency InjectionThere are four types of DI:1.Constructor Injection2.Setter Injection3.Interface-based injection4.Service Locator InjectionConstructor InjectionConstructor is used to interface parameter that exposed through the parameterized contractor.It injects the dependencies through a contractor method as object creation other classes.Setter InjectionGetter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}) and Setter(set(){}). TInterface InjectionInterface Injection is similar to Getter and Setter DI, the Getter and Setter DI uses default getter and setter but Interface Injection uses support interface a kind of ... Read More