Priority Queues with Chash

karthikeya Boyini
Updated on 21-Jun-2020 16:01:25

299 Views

A priority queue is held information with a priority value. It is an extension of queue.The item with the highest property is removed first when you try to eliminate an item from a priority queue.Let us see how to set priority queue −public class MyPriorityQueue where T : IComparable { }Now let us add an item. In the below example the items get stored in info, which is a generic list.Examplepublic class MyPriorityQueue where T : IComparable {    private List info;    public MyPriorityQueue() {       this.info = new List ();    } }

Private Constructors and Singleton Classes in C#

Samual Sam
Updated on 21-Jun-2020 16:00:43

993 Views

A private constructor is used in classes containing only static member as shown below −class Demo {    // private constructor    private Demo() { }    public static a = 10; }A singleton class has normal methods and you can call it using an instance.To prevent multiple instances of the class, the private constructor is used.Let us see an example −Examplepublic class Singleton {    static Singleton a = null;    private Singleton() {    } }

Operator Functions in C#

Ankith Reddy
Updated on 21-Jun-2020 15:59:26

237 Views

Operator functions are overloaded operator, which are the functions with special names. To create it, the keyword operator is followed by the symbol for the operator being defined.Like any other function, an overloaded operator has a return type and a parameter list.For example −public static Box operator+ (Vehicle v1, Vehicle v2, Vehicle v3) { }The following is the complete example showing how operator functions are created and used in C# −Exampleusing System; namespace OperatorOvlApplication {    class Box {       private double length; // Length of a box       private double breadth; // Breadth of ... Read More

Optimization Tips for Hash Code

Arjun Thakur
Updated on 21-Jun-2020 15:57:01

849 Views

The following are the tips −Prefer ListUse List whenever necessary. Working with ArrayList for the same work can make the working of code slower. This is specially if you are storing multiple types of objects within the same list.Use multiplication-shift operationPrefer multiplication-shift operation instead of division operator, since the usage of division operator slows the code.Less code takes less memoryTry to get work done using operator to concise the code and make it work in a single line.Use operators like && that would allow you to mention all the conditions in a single line.

Streams and Byte Streams in C#

Samual Sam
Updated on 21-Jun-2020 15:55:37

2K+ Views

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.The type of streams includes −Byte Streams − It includes Stream, FileStream, MemoryStream and BufferedStream.Character Streams − It includes Textreader-TextWriter, StreamReader, StraemWriter and other streams.Byte streams have classes that consider data in the stream as byte.Stream class is the base for other byte stream classes. The following are the properties −CanRead − Whether stream supports readingCanWrite − Whether stream supports writingLength − Length of the streamThe System.IO namespace has ... Read More

Serialization and Deserialization in C#

karthikeya Boyini
Updated on 21-Jun-2020 15:54:49

1K+ Views

Serialization converts objects into a byte stream and brings it to a form that it can be written on stream. This is done to save it to memory, file or database.Serialization can be performed as −Binary SerializationAll the members, even members that are read-only, are serializedXML SerializationIt serializes the public fields and properties of an object into XML stream conforming to a specific XML Schema definition language document.Let us see an example. Firstly set the stream −FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter();Now create an object of the class and call the constructor which has three parameters −Employee ... Read More

Reverse a String in C#

Chandu yadav
Updated on 21-Jun-2020 15:54:35

595 Views

To reverse a string, use the Array. Reverse() method.We have set a method and passed the string value as “Henry” −public static string ReverseFunc(string str) {    char[] ch = str.ToCharArray();    Array.Reverse(ch);    return new string(ch); }In the above method, we have converted the string into character array −char[] ch = str.ToCharArray();Then the Reverse() method is used −Array.Reverse(ch);

String Format for DateTime in C#

Samual Sam
Updated on 21-Jun-2020 15:54:17

468 Views

Format DateTime using String.Format method.Let us see an example −Exampleusing System; static class Demo {    static void Main() {       DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);       Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));       Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));       Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));    } }Above we have first set the DateTime class object −DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);To format, we have used the String.Format() method and displayed date in different formats.String.Format("{0:y yy yyy yyyy}", d) String.Format("{0:M MM MMM MMMM}", d) String.Format("{0:d dd ddd dddd}", d

Reverse Words in a Given String in C#

Chandu yadav
Updated on 21-Jun-2020 15:53:26

700 Views

Let’s say the following is the string −WelcomeAfter reversing the string, the words should be visible like −emocleWUse the reverse() method and try the following code to reverse words in a string −Exampleusing System; using System.Linq; class Demo {    static void Main() {       string str = "Welcome";       // reverse the string       string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));       Console.WriteLine(res);    } }

Chash: Put Spaces Between Words Starting with Capital Letters

Arjun Thakur
Updated on 21-Jun-2020 15:52:44

4K+ Views

To place spaces in between words starting with capital letters, try the following example −Firstly, set the string.var str = "WelcomeToMyWebsite";As you can see above, our string isn’t having spaces before capital letter. To add it, use LINQ as we have done below −str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');The following is the complete code to place spaces between words beginning with capital letters −Exampleusing System; using System.Linq; class Demo {    static void Main() {       var str = "WelcomeToMyWebsite";       Console.WriteLine("Original String: "+str);     ... Read More

Advertisements