Use the BinaryReader class if you want to read binary information from the stream.The BinaryReader class is in System.IO namespace.The following is an example showing using the BinaryReader class to read from a file −static void WriteMe() { using (BinaryWriter w = new BinaryWriter(File.Open("C:\abc.txt", FileMode.Create))) { w.Write(25.9); w.Write("DEMO DATA"); } } static void ReadMe() { using (BinaryReader r = new BinaryReader(File.Open("C:\abc.txt", FileMode.Open))) { Console.WriteLine("Value : " + r.ReadDouble()); Console.WriteLine("Value : " + r.ReadString()); } }The above method is called in the Main() method ... Read More
The following are the access specifiers supported by C#.NET −Public Access SpecifierIt allows a class to expose its member variables and member functions to other functions and objects.Private Access SpecifierPrivate access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members.Protected Access SpecifierProtected access specifier allows a child class to access the member variables and member functions of its base class.Internal Access SpecifierInternal access specifier allows a class to expose its member variables and member functions to other functions and objects in ... Read More
A stream for file operations such as read and write is provided by the FileStream class.Create an object like thisFileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate);Above we have used FileMode.OpenOrCreate so that the file or opened or created if it does not already exist.The following is n example showing how to use the FileStream class in C# −using System; using System.IO; public class Demo { public static void Main(string[] args) { FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); // write into the file fstream.WriteByte(90); // close the file fstream.Close(); } }
To calculate the size of a folder in C#, use the Directory.EnumerateFiles Method and get the files.To get the sub- directories, use the EnumerateDirectories method. Our folder is set using DirectoryInfo class −DirectoryInfo info = new DirectoryInfo(@"D:/new");Now find the size −long totalSize = info.EnumerateFiles().Sum(file => file.Length); For the directories, use −info.EnumerateDirectories()Other manipulations you can perform on Directories in C# are:MethodDescriptionCreateDirectory(String)Creates all directories and subdirectories in the specified path unless they already exist.CreateDirectory (String, DirectorySecurity)Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.Delete(String)Deletes an empty directory from a specified path.Delete(String, Boolean)Deletes the specified ... Read More
To calculate power of a number using recursion, try the following code.Here, if the power is not equal to 0, then the function call occurs which is eventually recursion −if (p!=0) { return (n * power(n, p - 1)); }Above, n is the number itself and the power reduces on every iteration as shown below −Example Live Demousing System; using System.IO; public class Demo { public static void Main(string[] args) { int n = 5; int p = 2; long res; res = power(n, p); Console.WriteLine(res); } static long power (int n, int p) { if (p!=0) { return (n * power(n, p - 1)); } return 1; } }Output25
To define a custom method in C#, use the following syntax − (Parameter List) { Method Body }To call a custom method, try to run the following code. It has the checkPalindrome() method which is called to checker whether the binary representation is Palindrome or not −Example Live Demousing System; public class Demo { public static long funcReverse(long num) { long myRev = 0; while (num > 0) { myRev = 1; } return myRev; } public static ... Read More
To calculate fractional power in C#, use the Math.Pow method.The following sets 5 to the power 3.7 −double res = Math.Pow(5, 3.7);The following is the complete example showing how to calculate fractional power in C# −Example Live Demousing System; class Program { static void Main() { double res = Math.Pow(5, 3.7); Console.WriteLine("Result = {0}", res); Console.ReadLine(); } }OutputResult = 385.646164200006
The increment operator is ++ operator. If used as postfix on a variable, the value of the variable is first returned and then gets incremented by 1. It is called Postfix increment operator. In the same way, the decrement operator works but it decrements by 1.For example,a++;The following is an example showing how to work with postfix operator −Example Live Demousing System; class Program { static void Main() { int a, b; a = 10; Console.WriteLine(a++); b = a; Console.WriteLine(a); Console.WriteLine(b); } }Output10 11 11
The ICollection interface in C# defines the size, enumerators, and synchronization methods for all nongeneric collections. It is the base interface for classes in the System.Collections namespace.The following are the properties of ICollection interface −Sr.No.Property Name & Description1CountThe number of elements in the ICollection2SyncRootGets an object that useful to synchronize access to the ICollection.The following are the methods of ICollection interface −Sr.No.Method Name & Description1CopyTo(Array^,Int32)The method copies the elements of the ICollection to an Array.2GetEnumerator()The GetEnumerator() method returns an enumerator that iterates through a collection
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated.This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.It has a single method −GetEnumerator() − This method returns an enumerator that iterates through a collection.The following is the implementation of the GetEnumerator() method of the IEnumerable interface in C# −IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator(); }The following are the extension methods of the IEnumerable interface in C# −Sr.NoMethod Name & Description1AsParallel()Enables parallelization of a ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP