C# Truncate Method

George John
Published on 31-Aug-2018 14:58:30
Use the Truncate method in C# to remove all the numbers after decimal places. Let’s say the following is our number − 20.35M To remove the numbers after decimal places, use Truncate() − decimal.Truncate(20.35M) Let us see the comple code − Example using System; using ... Read More

How to check the Existence of a File using C#?

Chandu yadav
Published on 31-Aug-2018 14:56:21
Let’s say we need to find the following file − E:\new.txt To check the existence of the above file, use the Exists() method − if (File.Exists(@"E:\new.txt")) { Console.WriteLine("File exists..."); } Here is the complete code to check the existence of a file ... Read More

Local Inner Class in C#

karthikeya Boyini
Published on 31-Aug-2018 14:37:46
A nested class is a class declared in another enclosing class and it has inner as well as outer class. It is a member of its enclosing class and the members of an enclosing class have no access to members of a nested class Let us see an example code ... Read More

Inheritance vs Composition in C#

Samual Sam
Published on 31-Aug-2018 14:36:20
Inheritance With 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 ... Read More

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

karthikeya Boyini
Published on 31-Aug-2018 14:34:30
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 ... Read More

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

Samual Sam
Published on 31-Aug-2018 14:32:58
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 ... Read More

Does declaring an array create an array in C#?

karthikeya Boyini
Published on 31-Aug-2018 14:30:32
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 ... Read More

Comparison of double and float primitive types in C#

Samual Sam
Published on 31-Aug-2018 14:28:58
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 ... Read More

Initialization vs Instantiation in C#

Arjun Thakur
Published on 31-Aug-2018 13:10:51
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 ... Read More

How to truncate a file in C#?

Ankith Reddy
Published on 31-Aug-2018 13:09:34
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 stream Value < current Length If the value is less than the current length of the stream: The stream is truncated. ... Read More