Found 34494 Articles for Programming

Inheritance vs Composition in C#

Samual Sam
Updated on 22-Jun-2020 12:47:31

549 Views

InheritanceWith Inheritance, you can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.For example, A base class Shape has a derived classes like Circle, Square, Rectangle, etc.CompositionUnder Composition, if the parent object is deleted, then the child object also loses its status. Composition is a special type of Aggregation and gives a part-of relationship.For example, ... Read More

How to use string.Empty or String.Empty to initialize a string in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:48:03

556 Views

Set the string as empty using the string.Empty in C# −string myStr = string.Empty;To check whether it is a string or not, use the IsNullOrEmpty() method −if (string.IsNullOrEmpty(myStr)) {    Console.WriteLine("String is empty or null!"); }The following is an example −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          string myStr = string.Empty;          if (string.IsNullOrEmpty(myStr)) {             Console.WriteLine("String is empty or null!");          } else {             Console.WriteLine("String isn't empty or null!");          }       }    } }OutputString is empty or null!

How to make a C# program sleep for x milliseconds?

Samual Sam
Updated on 22-Jun-2020 12:32:53

1K+ Views

To make a C# program sleep for x milliseconds, use the Thread.Sleep() method.To set it for 1000 milliseconds −Thread.Sleep(1000);The following is the code showing how to set a counter for the thread and set it to sleep for 1000 milliseconds on every iteration of for loop −Example Live Demousing System; using System.Threading; namespace MultithreadingApplication {    public class ThreadCreationProgram {       public static void CallToChildThread() {          try {             Console.WriteLine("Child thread starts");             for (int counter = 0; counter

Does declaring an array create an array in C#?

karthikeya Boyini
Updated on 22-Jun-2020 12:33:45

58 Views

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.The following is a declaration and it will not create an array −int[] id;The following create an array of integers. The array is a reference type, so you need to use the new keyword to create an instance of the array −Int[] id = new int[5] {};Let us see an example −Example Live Demousing System; namespace ArrayApplication {    public class MyArray {       public static void Main(string[] args) {          int ... Read More

Comparison of double and float primitive types in C#

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

309 Views

Precision states the difference between float and double data type. Float is a single precision (32 bit) floating point data type. Double is a double precision (64 bit) floating point data type. Range of a float type − -3.4 x 1038 to + 3.4 x 1038 Range of a double type is − (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 Default value of a float type − 0.0F Default value of a double type − 0.0D

Initialization vs Instantiation in C#

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

4K+ Views

Initialization When you assign a value to a variable when it is declared, it is called Initialization. Here is an example − int val = 50; For array initialization, you may need a new keyword, whereas to initialize a variable, you do not need it. Instantiation When you create a new object in C# for a class using the new keyword, then it is called instantiation. Use the new operator to instantiate a class in C#. Here is an example showing two objects of Student class created using new keyword − Student s1 = new Student(); Student s2 = new Student();

How to truncate a file in C#?

Ankith Reddy
Updated on 22-Jun-2020 12:34:29

698 Views

To truncate a file in C#, use the FileStream.SetLength method.Here is the syntax −public override void SetLength (long value);Here, int64 = Length of the streamValue < current LengthIf the value is less than the current length of the stream: The stream is truncated. If the current position is greater than the new length, the current position is moved to the last byte of the stream.Value > current LengthThe stream is expanded, and the current position remains the same. If the stream is expanded, the contents of the stream between the old and the new length are undefined.The following is an ... Read More

File Objects in C#

George John
Updated on 22-Jun-2020 12:35:19

821 Views

To create a new file in C#, use the FileStream object.The following is the syntax −FileStream = new FileStream( , , , );Let us see an example for a file “test.dat”, which is created/ opened using File object −FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,FileAccess.ReadWrite);The following is an example −Exampleusing System; using System.IO; namespace FileIOApplication {    class Program {       static void Main(string[] args) {          FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,          FileAccess.ReadWrite);          for (int i = 1; i

How to inherit a class in C#?

Chandu yadav
Updated on 22-Jun-2020 12:36:17

103 Views

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.Let us see an ... Read More

Class and Static Variables in C#

Arjun Thakur
Updated on 22-Jun-2020 12:38:08

8K+ Views

Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.Example Live Demousing System; namespace StaticVarApplication {    class StaticVar {       public static int num;       public void count() {          num++;       }       public int getNum() {          return num;       }    }    class ... Read More

Advertisements