Delegation vs Inheritance in C#

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

721 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

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

Scope of an Internal Variable of a Class in C#

Samual Sam
Updated on 23-Jun-2020 09:35:44

1K+ Views

Internal variable is set using the internal access specifier.internal double length; internal double width;Any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.Example Live Demousing System; namespace RectangleApplication {    class Rectangle {       //member variables       internal double length;       internal double width;       double GetArea() {          return length * width;       }       public void Display() {          Console.WriteLine("Length: {0}", length);          Console.WriteLine("Width: ... Read More

IsReadOnly Property of Hashtable Class in C#

Chandu yadav
Updated on 23-Jun-2020 09:34:34

138 Views

The IsReadOnly property of Hashtable class is used to get a value indicating whether the Hashtable is read-only.Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          Hashtable ht = new Hashtable();          ht.Add("One", "Amit");          ht.Add("Two", "Aman");          ht.Add("Three", "Raman");          Console.WriteLine("IsReadOnly = " + ht.IsReadOnly);          Console.ReadKey();       }    } }OutputIsReadOnly = FalseAbove we have set a Hashtable with three elements.ht.Add("One", "Amit"); ht.Add("Two", "Aman"); ht.Add("Three", "Raman");After that, we have checked using the IsReadOnly property.Console.WriteLine("IsReadOnly = " + ht.IsReadOnly);

Difference Between IComparable and IComparer Interface in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:33:14

2K+ Views

IComparable Interface in C#Use the IComparable Interface in C# to sort elements. It is also used to compare the current instance with another object of same type.It provides you with a method of comparing two objects of a particular type. Remember, while implementing the IComparable interface, CompareTo() method should also be implemented.Let us see an example −int IComparable.CompareTo(object ob) {    Vehicle v=(Vehicle)ob;    return String.Compare(this.make, v.make); }IComparer interface in C#The IComparer interface is used to sort elements that compare two objects and provides additional comparison method.Exampleprivate class sortYearAscendingHelper : IComparer {    int IComparer.Compare(object ob1, object ob2) {   ... Read More

Division Operator in C#

Samual Sam
Updated on 23-Jun-2020 09:32:12

7K+ Views

Division operator is used in C# to divide numerator by denominator, for example 9/ 3The division operator comes under Arithmetic Operators in C#. Let us see a complete example to learn how to implement Arithmetic operators in C#, wherein we will see how to work with division operator.result = num1 / num2; Console.WriteLine("Division: Value is {0}", result);Above we have used division operator on num1 and num2.The following is the complete example.Example Live Demousing System; namespace Sample {    class Demo {       static void Main(string[] args) {          int num1 = 50;         ... 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

Comparing Dates Using C#

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

313 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

C# Program to Display the Factors of the Entered Number

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

386 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

Constructors in C#

karthikeya Boyini
Updated on 23-Jun-2020 09:27:50

617 Views

A Constructor in C# gets invoked automatically when a object gets created. The constructor has the same name as that of the class, for example −public class Department {    public Department () {       Console.WriteLine("Default Constructor! ");    } }The following is the code that shows the usage of constructor in C#.Example Live Demousing System; public class Department {    public Department () {       Console.WriteLine("Constructor Invoked");    }    public static void Main(string[] args) {       Department dept1 = new Department ();    } }OutputConstructor Invoked

Advertisements