Found 2587 Articles for Csharp

Database Operations in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

The most common databases used in C# are Microsoft SQL Server and Oracle. The following is done to work with databases. Connect Set the Database Name, Optional Parameters and Credentials. The username and password is needed to set a connection to the database. The connection string would look somewhat like this. private static string _connectionString = "Data Source=.;Integrated Security=SSPI;Initial Catalog=test;Application Name=Demo;Connection Timeout2w00"; Above, the Application Name is Demo. Select Statement To fetch data from the database, the SELECT statement is used Insert The INSERT command is used to insert data in the database. Update The database ... Read More

Counters in C#

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

2K+ Views

Counters in C# are performance counters that lets you know about your applications performance. When you will build an application, whether it is a web app, mobile app or even desktop app, you would definitely need to monitor the performance. For performance counters in C#, use the System.Diagnostics.PerformanceCounter class. Set the instance of the PerformanceCounter class and work with the following properties: CategoryName, CounterName, MachineName and ReadOnly. To get the performance categories. var counter = PerformanceCounterCategory.GetCategories(); Now SET performance counters for the category processor. var counter = PerformanceCounterCategory.GetCategories() .FirstOrDefault(category => category.CategoryName == "Processor");

Date Class in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:49:22

14K+ Views

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.Let’s create a DateTime object.Example Live Demousing System; class Test {    static void Main() {       DateTime dt = new DateTime(2018, 7, 24);       Console.WriteLine (dt.ToString());    } }Output7/24/2018 12:00:00 AMLet us now get the current date and time.Example Live Demousing System; class Test {    static void Main() {       Console.WriteLine (DateTime.Now.ToString());    } }Output9/17/2018 5:49:21 AMNow using the method Add(), we will add days in a date with the ... Read More

Events vs Delegates in C#

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

524 Views

C# events are used to resolve the hassles in Delegates. One an easily override Delegate properties and that can eventually lead to errors in the code. To avoid this, C# uses Events and defines wrappers around Delegates. Events in C# To use Event, you should define delegate first. Event is a type of Delegate and an example of event can be when a key is pressed. public delegate voide Demo(String val); public event Test TestEvent; An event can hold a delegate like this. this.TestEvent += new Demo (DemoData); ... Read More

Enqueue and deque in Queue class in C#

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

928 Views

Queue collection class is a concept in C# that is included in the System.Collection namespace. The elements are stored in a QUEUE in FIFO. The first element added will be the first to go out like a queue of people outside a movie hall to buy tickets. It has two methods. Enqueue() method to add values Dequeue() method to retrieve values Enqueue Add items in the queue. Queue q = new Queue(); q.Enqueue(“Two”); q.Enqueue(“One”); Dequeue Return items from the queue. Queue q = new Queue(); q.Enqueue(“Two”); q.Enqueue(“One”); // remove elements while (q.Count > 0) ... Read More

Equivalent of Java Super Keyword in C#

Chandu yadav
Updated on 23-Jun-2020 09:37:08

8K+ Views

For super keyword in Java, we have the base keyword in C#.Super keyword in Java refers immediate parent class instance. It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass.C# base keyword is used to access the constructors and methods of base class. Use it within instance method, constructor, etc.Let us see an example of C# base.Example Live Demousing System; public class Animal {    public string repColor = "brown"; } public class Reptile: Animal {    string repColor = "green";   ... Read More

Delegation vs Inheritance in C#

Samual Sam
Updated on 23-Jun-2020 09:37:53

710 Views

Delegates in C#A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.To declare a delegate.delegate Delegation has run-time flexibility i.e. you can easily change it at runtime. The instance you create in Delegation is of a known class.Inheritance in C#Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.When creating a class, instead of writing completely new data members and ... Read More

Difference between Static Constructor and Instance Constructor in C#

George John
Updated on 23-Jun-2020 09:38:41

808 Views

Static ConstructorA static constructor is a constructor declared using static modifier. It is the first block of code executed in a class. With that, a static constructor executes only once in the life cycle of class.Instance ConstructorInstance constructor initializes instance data. Instance constructor is called when an object of class is created.The following example shows the difference between static and instance constructor in C#.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Difference {    class Demo {       static int val1;       int val2;       static Demo() {         ... Read More

Difference between IEnumerator and IEnumerable Interface in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:39:54

6K+ Views

IEnumerable and IEnumerator both are interfaces in C#.IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface.This works for readonly access to a collection that implements that IEnumerable can be used with a foreach statement.IEnumerator has two methods MoveNext and Reset. It also has a property called Current.The following shows the implementation of IEnumerable and IEnumerator.Exampleclass Demo : IEnumerable, IEnumerator {    // IEnumerable method GetEnumerator()    IEnumerator IEnumerable.GetEnumerator() {       throw new NotImplementedException();    }    public object Current {       get { throw new NotImplementedException(); }    }    // ... Read More

What is the scope of a public member variable of a class in C#?

Ankith Reddy
Updated on 23-Jun-2020 09:40:38

438 Views

Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.In the below example the variables length and width have been declared public. Now you can even access them outside the Main() method.The variables are accessed using the instance of the class.Rectangle r = new Rectangle(); r.length = 4.5; r.width = 3.5;Let us see the complete code.ExampleUsing System; namespace RectangleApplication {    class Rectangle {       // member variables       public double length;       public double ... Read More

Advertisements