Csharp Articles - Page 138 of 258

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

What is the IsReadOnly property of Hashtable class in C#?

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

150 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);

What is the 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

Different Methods to find Prime Numbers in C#

George John
Updated on 23-Jun-2020 09:21:11

591 Views

The following are the two ways through which you can find a prime number in C#.Check Prime Number using for loop Live Demousing System; namespace Program {    class Demo {       public static void Main() {          int n =7;          int a;          a = 0;          for (int i = 1; i

Differences between C++ and C#

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

463 Views

C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming. C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. The following are the differences between C++and C#. Memory Management C++ has manual memory management, whereas memory management is handled automatically in C#. Platforms C++ can be run on different platforms whereas C# is generally used only on Windows. Faster code C++ code ... Read More

Difference between String and StringBuilder in C#

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

2K+ Views

Strings in C# String is immutable in C# that would mean you couldn’t modify it after it is created. It creates a new object of string type in memory if you will perform any operation. string str1 = "Welcome!"; // creates a new string instance str1 += "Hello"; str1 += "World”; StringBuilder in C# StringBuilder is mutable in C#. This means that if an operation is performed on the string, it will not create a new instance every time. With that, it will not create new space in memory, unlike Strings. StringBuilder str1 = new StringBuilder(""); str1.Append("Welcome!"); ... Read More

Difference between Boxing and Unboxing in C#

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

1K+ Views

Boxing convert value type to an object type whereas unboxing converts object type to the value type. Let us see the difference between Boxing and Unboxing in C#. Storage In boxing, the value stored on the stack is copied to the object stored on heap memory, whereas unboxing is the opposite. In Unboxing, the object's value stored on the heap memory is copied to the value type stored on stack. Conversion Unboxing has explicit conversion whereas boxing has implicit conversion. Example int a = 10; object obj = a; // boxing int b = (int) ob; // unboxing

Difference between Abstract Class and Interface in C#

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

720 Views

Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. Abstract classes to some extent serve the same purpose, however, they are mostly used when only few methods are to be declared by the base class and the deriving class implements the functionalities. Let us now see the difference between Abstract class and Interface in C#. Inherit A class may inherit more than one interface, whereas a class may inherit only one abstract class. Member Field You ... Read More

Difference between == and .Equals method in c#

karthikeya Boyini
Updated on 23-Jun-2020 09:23:20

7K+ Views

The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string.The Equals() method compares only content.Example Live Demousing System; namespace ComparisionExample {    class Program {       static void Main(string[] args) {          string str = "hello";          string str2 = str;          Console.WriteLine("Using Equality operator: {0}", str == str2);          Console.WriteLine("Using equals() method: {0}", str.Equals(str2));          Console.ReadKey();       }    } }OutputUsing Equality operator: True Using equals() method: ... Read More

Delegates in C#

Chandu yadav
Updated on 23-Jun-2020 09:24:41

328 Views

A delegate in C# is a reference to the method. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.Let us see how to declare delegates in C#.delegate Let us see an example to learn how to work with Delegates in C#.Example Live Demousing System; using System.IO; namespace DelegateAppl {    class PrintString {       static FileStream fs;       static StreamWriter sw;       ... Read More

Advertisements