Found 2587 Articles for Csharp

Difference Between dispose() and finalize() in C#

AmitDiwan
Updated on 24-Mar-2021 12:47:38

1K+ Views

In this post, we will understand the difference between the methods ‘dispose’, and ‘finalize’ in C#.DisposeThis method is defined in the IDisposable interface.It has to be invoked by the user.Whenever it is invoked, it helps free the unmanaged resources.It can be implemented whenever a close() method is present.It is declared as public method.It is quick, and instantly disposes an object.Since it performs instantaneously, it doesn’t affect performance.FinalizeIt is a method that is defined in the java.lang.object class.It is invoked by the garbage collector.It helps free the unmanaged resources just before the object is destroyed.It is implemented to manage the unmanaged ... Read More

Difference Between C# and C++

AmitDiwan
Updated on 02-Mar-2021 04:56:56

581 Views

Let us first learn about C# and C++ −C# is a general-purpose object-oriented programming language.It is considered as a pure object-oriented programming language.It is pronounced as 'C sharp'.It was developed by Anders Hejlsberg and his team at Microsoft.Memory Management is done automatically by the garbage collector.It is the language's duty to automatically delete the object once its objective is completed.It is windows specific, i.e. it can't be used on all systems.It doesn't support multiple inheritance.The pointers in C# can only be used in the unsafe mode.It is considered as a high-level language.Once the code is compiled, it gets converted into ... Read More

How to perform a left outer join using linq extension methods in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:45:26

2K+ Views

With INNER JOIN only the matching elements are included in the result set. Non-matching elements are excluded from the result set.With LEFT OUTER JOIN all the matching elements + all the non-matching elements from the left collection are included in the result set.Let us understand implementing Left Outer Join with an example. Consider the following Department and Employee classes. Notice that, Employee Mary does not have a department assigned. An inner join will not include her record in the result set, where as a Left Outer Join will.Examplestatic class Program{    static void Main(string[] args){       var result = Employee.GetAllEmployees() ... Read More

How to display methods and properties using reflection in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:43:53

285 Views

Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. There are numerous classes of System.Reflection but the most commonly used ones are Assembly, AssemblyName, ConstructorInfo, MethodInfo, ParameterInfo, EventInfo, PropertyInfo, and MemberInfo.Examplestatic void Main(string[] args){    TypeInfo myType = typeof(TextInfo).GetTypeInfo();    IEnumerable properties = myType.DeclaredProperties;    IEnumerable methods = myType.DeclaredMethods;    Console.WriteLine(myType);    Console.WriteLine(properties);    Console.WriteLine(methods);    StringBuilder strBuilder = new StringBuilder();    Console.WriteLine();    strBuilder.Append("The properties are:");    foreach (PropertyInfo p ... Read More

How to implement dependency injection using Interface-based injection in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:42:43

4K+ 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 −Constructor InjectionSetter InjectionInterface-based injectionService Locator InjectionInterface InjectionInterface Injection is similar to Getter and Setter DI, the Getter, and Setter DI use default getter and setter but Interface Injection uses support interface a kind of explicit getter and setter which sets the interface property.Examplepublic interface IService{    string ServiceMethod(); } public class ClaimService:IService{    public string ServiceMethod(){       return "ClaimService is running";    } } public class AdjudicationService:IService{    public string ServiceMethod(){       ... Read More

How to implement Dependency Injection using Property in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:41:09

1K+ 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 −Constructor InjectionSetter InjectionInterface-based injectionService Locator InjectionSetter InjectionGetter and Setter Injection injects the dependency by using default public properties procedure such as Gettter(get(){}) and Setter(set(){}). Examplepublic interface IService{    string ServiceMethod(); } public class ClaimService:IService{    public string ServiceMethod(){       return "ClaimService is running";    } } public class AdjudicationService:IService{    public string ServiceMethod(){       return "AdjudicationService is running";    } } public class BusinessLogicImplementation{    private IService _client;    public IService Client{   ... Read More

How to implement Open Closed principle using C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:39:48

523 Views

Software entities like classes, modules and functions should be open for extension but closed for modifications.Definition − The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.ExampleCode Before Open Closed Principleusing System; using System.Net.Mail; namespace SolidPrinciples.Open.Closed.Principle.Before{    public class Rectangle{       public int Width { get; set; }     ... Read More

How to implement Single Responsibility Principle using C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:37:34

359 Views

A class should have only one reason to change.Definition − In this context, responsibility is considered to be one reason to change.This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and if in the future we need to make one change we are going to make it in the class which handles it. When we need to make a change in a class having more responsibilities the change might affect the other functions related to the other responsibility of ... Read More

How to get formatted JSON in .NET using C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:35:03

4K+ Views

Use Namespace Newtonsoft.Json.Formatting Newtonsoft.Json.Formatting provides formatting options to Format the JsonNone − No special formatting is applied. This is the default.Indented − Causes child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.Examplestatic void Main(string[] args){    Product product = new Product{       Name = "Apple",       Expiry = new DateTime(2008, 12, 28),       Price = 3.9900M,       Sizes = new[] { "Small", "Medium", "Large" }    };    string json = JsonConvert.SerializeObject(product, Formatting.Indented);    Console.WriteLine(json);    Product deserializedProduct = JsonConvert.DeserializeObject(json);    Console.ReadLine(); } class Product{    public String[] Sizes ... Read More

How to run multiple async tasks and waiting for them all to complete in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:33:01

3K+ Views

The Task.WaitAll blocks the current thread until all other tasks have completed execution.The Task.WhenAll method is used to create a task that will complete if and only if all the other tasks have complete. In the 1st example, we could see that when using Task.WhenAll the task complete is executed before the other tasks are completed. This means that Task.WhenAll doesn’t block the execution. And in the 2nd example, we could see that when using Task.WaitAll the task complete is executed only after all the other tasks are completed. This means that Task.WaitAll block the execution.Examplestatic void Main(string[] args){   ... Read More

Advertisements