Found 34494 Articles for Programming

Enqueue and deque in Queue class in C#

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

641 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

6K+ 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

503 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

613 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

5K+ 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

335 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

Difference between namespace in C# and packages in Java

Samual Sam
Updated on 23-Jun-2020 09:41:31

3K+ Views

Packages in JavaPackages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.Define a Package as −package package_name;Restrict the access of classes (or class members) to the classes within the same package, but in C# with namespaces you cannot achieve this.Namespace in C#A namespace is designed for providing a way ... Read More

Comparison between C# and .NET Framework

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

437 Views

C# is a programming language and .NET framework is a software framework developed by Microsoft. .NET has Common Language Runtime (CLR), which is a virtual component of .NET framework. And framework is a large class of library. .NET not only has C#, but through it, you can work with VB, F#, etc. Programs written for .NET Framework execute in Common Language Runtime. .NET Framework supports development in C#. C# is a part of .NET and has the following features − Boolean Conditions Automatic Garbage Collection Standard Library Assembly Versioning Properties and Events Delegates and Events Management

C# program to Count words in a given string

karthikeya Boyini
Updated on 23-Jun-2020 09:42:41

529 Views

Let’s say we want to count the number of words in the following string −str1 = "Hello World!";Now you need to loop though till string length and increment the variable count on finding “ “, , \t as shown below −if(str1[a]==' ' || str1[a]=='' || str1[a]=='\t') {    count++; }You can try to run the following code to count words in a given string in C#.Example Live Demousing System; public class Demo {    public static void Main() {       string str1;       int a, count;       str1 = "Hello World!";       a = 0;       count = 1;       while (a

C# Program to display the Factors of the Entered Number

Chandu yadav
Updated on 23-Jun-2020 09:28:42

240 Views

Firstly, let us enter the number.Console.WriteLine("Enter a Number"); n = int.Parse(Console.ReadLine());Now loop through and find the mod of the entered number with i = 1 that increments after every iteration. If its 0, then print it, since it would be our factor.for (i= 1; i

Advertisements