
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

7K+ Views
A Unix timestamp is mainly used in Unix operating systems. But it is helpful for all operating systems because it represents the time of all time zones.Unix Timestamps represent the time in seconds. The Unix epoch started on 1st January 1970.So, Unix Timestamp is the number of seconds between a specific dateExampleto get the Unix Timestamp Using DateTime.Now.Subtract().TotalSeconds Methodclass Program{ static void Main(string[] args){ Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp); Console.ReadLine(); } }Output1596837896Exampleto get the Unix Timestamp Using DateTimeOffset.Now.ToUnixTimeSeconds() ... Read More

2K+ Views
Yes it is possible to force garbage collector in C# to run by calling Collect() methodThis is not considered a good practice because this might create a performance over head. Collect() Forces an immediate garbage collection of all generations.Collect(Int32)Forces an immediate garbage collection from generation 0 through a specified generation.Example Live Demousing System; class MyGCCollectClass{ private const int maxGarbage = 1000; static void Main(){ // Put some objects in memory. MyGCCollectClass.MakeSomeGarbage(); Console.WriteLine("Memory used before collection: {0:N0}", GC.GetTotalMemory(false)); // Collect all generations of memory. ... Read More

752 Views
We can create an array with non-default values using Enumerable.Repeat(). It repeated a collection with repeated elements in C#. Firstly, set which element you want to repeat and how many times.Example 1class Program{ static void Main(string[] args){ var values = Enumerable.Repeat(10, 5); foreach (var item in values){ System.Console.WriteLine(item); } Console.ReadLine(); } }Output10 10 10 10 10Example 2class Program{ static void Main(string[] args){ int[] values = Enumerable.Repeat(10, 5).ToArray(); foreach (var item in values){ System.Console.WriteLine(item); } Console.ReadLine(); } }Output10 10 10 10 10

164 Views
We are creating two instances of the Employee class, e and e1. The e is assigned to e1. Both objects are pointing to the same reference, hence we will get true as expected output for all the Equals.In the second case we can observe that, even though properties values are same. Equals returns false. Essentially, when arguments are referring to different objects. Equals don’t check for the values and always returns false.Example 1class Program{ static void Main(string[] args){ Employee e = new Employee(); e.Name = "Test"; e.Age = 27; ... Read More

3K+ Views
The Take operator is used to return a given number of elements from an array and the Skip operator skips over a specified number of elements from an array.Skip, skips elements up to a specified position starting from the first element in a sequence.Take, takes elements up to a specified position starting from the first element in a sequence.Example 1class Program{ static void Main(string[] args){ List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 5, 6, 7, ... Read More

1K+ Views
Shallow Copy −A shallow copy of an object copies the "main" object, but doesn’t copy the inner objects.The "inner objects" are shared between the original object and its copy.The problem with the shallow copy is that the two objects are not independent. If you modify the one object, the change will be reflected in the other object.Deep Copy −A deep copy is a fully independent copy of an object. If we copied our object, we would copy the entire object structure.If you modify the one object, the change will not be reflected in the other object.Exampleclass Program{ static void ... Read More

1K+ Views
Action filters are used to add extra logic before or after action methods execution. The OnActionExecuting and OnActionExecuted methods are used to add our logic before and after an action method is executed.Let us create create a LogAttribute that implemets ActionFilterAttribute which logs some information before and after action method execution.LogAttribute −Exampleusing System; using System.Diagnostics; using System.Web.Http.Controllers; using System.Web.Http.Filters; namespace DemoWebApplication.Controllers{ public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext){ Debug.WriteLine(string.Format("Action Method {0} executing at {1}", actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()), ... Read More

295 Views
Media types allow an API to inform the client how to interpret the data in the payload. In the HTTP protocol, Media Types are specified with identifiers like text/html, application/json, and application/xml, which correspond to HTML, JSON, and XML respectively, the most common web formats. There are other more APIspecific Media Types as well, such as application/vnd.api+json.Below are versions that needs to be send in media types.application/vnd.demo.students.v1+json StudentsV1Controller application/vnd.demo.students.v2+json StudentsV2ControllerAdding our own CustomControllerSelector will fix the above error.CustomControllerSelector −Exampleusing System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{ public class CustomControllerSelector : DefaultHttpControllerSelector{ ... Read More

631 Views
The Accept header tells the server in what file format the browser wants the data. These file formats are more commonly called as MIME-types. MIME stands for Multipurpose Internet Mail Extensions.Versioning can be send in Headers like below.Version=1 StudentsV1Controller Version=2 StudentsV2ControllerSince we have not handled the version in accept headers, we are getting 404 not found error as we only have StudentV1 and StudentV2 controller. Let us add our own CustomControllerSelector which implements the DefaultHttpControllerSelector class.CustomControllerSelector −Exampleusing System.Linq; using System.Net.Http; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{ public class CustomControllerSelector : DefaultHttpControllerSelector{ private HttpConfiguration _config; ... Read More

458 Views
An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception. The HttpResponseException type is a special case, because it is designed specifically for returning an HTTP response.Exception filters implement the System.Web.Http.Filters.IExceptionFilter interface. The simplest way to write an exception filter is to derive from the System.Web.Http.Filters.ExceptionFilterAttribute class and override the OnException method.Below is a filter that converts NotFiniteNumberException exceptions into HTTP status code 416, Requested Range Not Satisfiable.ExceptionFilterAttribute −Exampleusing System; using System.Net; using System.Net.Http; using System.Web.Http.Filters; namespace DemoWebApplication.Controllers{ public class ExceptionAttribute : ExceptionFilterAttribute{ public override void OnException(HttpActionExecutedContext ... Read More