Skip to main content

Posts

Showing posts with the label C#

Bubble Sort in C-Sharp

Bubble Sort is a simple sorting algorithms that iterates through a list and compares adjacent item and swaps them in the order if they are in the wrong order.  The iteration is completed after the  items bubble to there specific position and the list is sorted ascending or descending. Bubble sort is simple but too slow and impractical in most of the cases. Figure : Algorithm: for all elements in Array if Array [ i ] > Array [ i + 1 ] swap ( Array [ i ], Array [ i + 1 ]) end if end for return Array CODE :

Reading and Writing to a file text file in C#

Lets start by taking the below  code as an example:    private void WriteToAFile(){             string data = "hi ! what's up";             string path = "data.txt";             if (!File.Exists(path))             {                 File.Create(path);             }             using (StreamWriter writer = new StreamWriter(path, append: true))             {                 writer.WriteLine(data);             } } Writing to a text file can be achieved by using StreamWriter class . To use this class we need to use System.IO namespace. if we are writing to a file then exception might occur if the file doesn't exists. So firstly ...

Classes and Objects In C#

Class  is a collection of fields, properties and methods. It depicts real life entities and is a fundamental concept in Object Oriented Programming.  A class is a user-defined blueprint  from which objects are created . A class is a user defined data type of  reference type. Lets take a example of a Car . It has different attributes like color,maximum speed,no of gears on with a built in audio player  and so on. public class Car {     private string  _color; //a variable and a field with data type string     // a variable and  a property       public string Color       {         get         {             return _color;         }         set         {             _color= value;       ...