Server Side Programming Articles - Page 2421 of 2646

Delegation vs Inheritance in C#

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

759 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

855 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

470 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

616 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

786 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

408 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

Comparing dates using C#

Samual Sam
Updated on 23-Jun-2020 09:29:16

331 Views

To compare dates in C#, you need to first set two dates to be compared using the DateTime object. We will use the DateTime class in C#.Date 1DateTime date1 = new DateTime(2018, 07, 20); Console.WriteLine("Date 1 : {0}", date1);Date 2DateTime date2 = new DateTime(2018, 07, 25); Console.WriteLine("Date 2 : {0}", date2);Now let us compare both the dates in C#. The following is an example to compare dates in C#.Example Live Demousing System; namespace Program {    class Demo {       static int Main() {          DateTime date1 = new DateTime(2018, 07, 20);         ... Read More

Enum with Customized Value in C#

George John
Updated on 23-Jun-2020 09:29:44

2K+ Views

Enum is Enumeration to store a set of named constants like year, product, month, season, etc.The default value of Enum constants starts from 0 and increments. It has fixed set of constants and can be traversed easily. However you can still change the start index and customize it with the value of your choice.In the following example, I have set the customized value to be 20 instead of the default 0.Example Live Demousing System; public class Demo {    public enum Vehicle { Car =20, Motorcycle, Bus, Truck }    public static void Main() {       int a = ... Read More

Advertisements