Simple C# Programs

Here is some basic, simple, handy C# programs for you to enjoy and refresh your memory –

using GroundCSApp;
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace GroundCSApp
{
    public enum ShippingMethod
    {
        Regular = 1,
        Priority = 2,
        Snail = 3
    }
    class Program
    {
        static void Main(string[] args)
        {
 
            //-------------------------------------------------------
            //non -primitive/reference type eg: number 
            //array creation and assignment
            
            var numbers = new int[3];
            numbers[0= 23;
 
            var numbers2 = numbers;
 
            numbers[0= 24;
 
            Console.WriteLine("Value of variable numbers is changed 
and it is >>" + numbers[0]);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //non-primitive, immutable, string array creation assignment
            var names = new string[3] { "John""Luke""Mark" };
 
            //String Join example
            var joinedName = string.Join(",", names);
            Console.WriteLine("\n\n" + joinedName);
 
            var strArray = new string[] { "Text""Book""Light" };
 
            var strArray2 = strArray;
 
            //example for reference type where
            //original value is modified
            strArray2[0= "Candle";
 
            var comb = string.Join(",", strArray);
 
            Console.WriteLine(comb);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //simple object creation and method call
            //Example::1
            var John = new Person();
 
            John.FirstName = "John";
            John.LastName = "Jiss";
 
            John.Introduce();
 
            //Example::2
            var cal = new Calculator();
            Console.WriteLine(cal.Add(2334+ "" + numbers[0]);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Using Enums
            var method = ShippingMethod.Priority;
            Console.WriteLine((int)method);
 
            var methodExt = 3;
            Console.WriteLine((ShippingMethod)methodExt);
 
            var shippingstr = "Snail";
            var shippingM = (ShippingMethod)Enum.Parse
(typeof(ShippingMethod), shippingstr);
 
            Console.WriteLine(shippingM);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple Console Read and output
            while (true)
            {
                Console.WriteLine("\n\nEnter your name, we will 
display it :) >>");
 
                var input = Console.ReadLine();
 
                if (!string.IsNullOrWhiteSpace(input))
                {
                    Console.WriteLine("Your name is>>" + input);
                    break;
 
                }
 
                continue;
 
            }
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple program snippet to Find Mode
            var cnt3 = 0;
 
            for (var i = 1; i <= 100; i++)
            {
                if (i % 3 == 0)
                {
                    cnt3++;
                }
            }
            
            Console.WriteLine("\nCount of mod 3s in 100 is>>" + cnt3);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple Console program snippet to find sum of numbers
            Console.WriteLine("\n\nEnter your numbers with new line 
to find the sum. Press 'ok' to exit>>");
            var numsum = 0;
 
            while (true)
            {
                var s = Console.ReadLine();
 
                if (s == "ok")
                {
                    Console.WriteLine("\n\nYour sum is>> " + numsum);
                    break;
                }
                else
                {
                    numsum += Convert.ToInt32(s);
                }
 
            }
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple Console program snippet to find factorial
            Console.WriteLine("\n\nEnter number to find factorial>>");
            var factorial = Convert.ToInt32(Console.ReadLine());
            var factans = 1;
            for (var i = factorial; i >= 1; i--)
            {
                factans = factans * i;
            }
 
            Console.WriteLine("\n  " + factorial + "! = " + factans);
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple program snippet for array sort
            var numar = new[] { 234521 };
 
            Array.Sort(numar);
            foreach (var i in numar)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("\n\n");
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple program snippet for list operartions
            var numberslst = new List<int>() { 12345 };
 
            numberslst.Add(1);
 
            numberslst.AddRange(new int[3] { 678 });
 
            foreach (var numb in numberslst)
            {
                Console.WriteLine(numb);
            }
 
            Console.WriteLine(numberslst.IndexOf(1));
            Console.WriteLine(numberslst.LastIndexOf(1));
 
            for (var i = 0; i < numberslst.Count; i++)
            {
                if (numberslst[i] == 1)
                {
                    numberslst.Remove(numberslst[i]);
                }
            }
 
            Console.WriteLine("\n\n");
            foreach (var num in numberslst)
            {
                Console.WriteLine(num);
            }
            //End list operations
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple implementation of Facebook style Like status
 
            Console.WriteLine("\n\nPlease enter unique names to find 
your like status. Press ok to exit>>");
 
            var namlst = new List<string>();
            while (true)
            {
                var namelst = Console.ReadLine();
 
 
                if (namlst.Count > 0 && namlst.Contains(namelst))
                {
                    Console.WriteLine("\nPlease enter unique names>>");
                }
                else if (namelst != "ok")
                {
                    namlst.Add(namelst);
                }
                else
                {
                    if (namlst.Count > 0)
                    {
                        Console.WriteLine("******************\n\n");
                        for (var i = 0; i < namlst.Count; i++)
                        {
                            if (i == 0)
                            {
                                Console.WriteLine("You have likes from > ");
                            }
                            Console.WriteLine("[" + namlst[i] + (i + 1+ "] ");
                        }
                    }
                    else
                    {
                        Console.WriteLine("\nYou have no likes!");
                    }
                    break;
                }
 
            }
 
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple program snippet to Reverse String
            Console.WriteLine("\n\nEnter your name\n\n>>");
 
            var namarre = Console.ReadLine();
 
            var namarry = new string[namarre.Length];
 
            for (var i = 0; i < namarre.Length; i++)
            {
                namarry[i] = namarre[i].ToString();
            }
 
            Array.Reverse(namarry);
            Console.WriteLine("\nString reversed is as below>>\n\n");
            foreach (var nami in namarry)
            {
                Console.WriteLine(nami);
 
            }
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple program snippet to capture first 5 numbers, 
            //sort it and display it
            var numplLst = new List<int>();
            Console.WriteLine("\n\nEnter numbers, we will capture first 
5 numbers and will sort and display it >>\n");
            while (true)
            {
                var innum = Console.ReadLine();
 
                int numval = 0;
 
                try
                {
                    numval = Convert.ToInt32(innum);
                }
                catch (FormatException e)
                {
                    Console.WriteLine("\nThe input need to be a number");
                }
                catch (OverflowException e)
                {
                    Console.WriteLine("\nThe input cannot be fit in as a number");
                }
 
                if (numplLst.Count == 5)
                {
                    numplLst.Sort();
                    Console.WriteLine("\nHurray you have entered 5 numbers 
and here it is sorted>>\n\n");
                    foreach (var i in numplLst)
                    {
                        Console.WriteLine(i);
                    }
                    break;
                }
                else
                {
                    if (numplLst.Contains(numval))
                    {
                        Console.WriteLine("\nNumber already exist in the 
list, please enter unique value>>");
                    }
                    else
                    {
                        numplLst.Add(numval);
                    }
                }
            }
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple console program snippet to 
            //accept numbers and display unique numbers
            var innums = new List<int>();
 
            Console.WriteLine("\n\nEnter any numbers until 'Quit', we will 
display unique numbers>>");
 
            while (true)
            {
 
                var inm = Console.ReadLine();
 
 
                var ret = 0;
                try
                {
                    ret = Convert.ToInt32(inm);
                }
                catch (FormatException e)
                {
                    Console.WriteLine("\nPlease enter a number!");
                }
                catch (OverflowException e)
                {
                    Console.WriteLine("\nNumber cannot be fit into the stack!");
                }
 
                if (inm == "Quit")
                {
                    var reslst = innums.Distinct().ToList();
                    Console.WriteLine("\nYour unique list is>>\n\n");
                    foreach (var ls in reslst)
                    {
                        Console.WriteLine(ls);
                    }
                    break;
                }
 
                innums.Add(ret);
            }
            //--------------------------------------------------------
 
 
            //--------------------------------------------------------
            //Simple console program snippet to get numbers and then display 
            //first 3 lowest numbers
            Console.WriteLine("\\nEnter a list of numbers separated by comma, 
we will display the first 3 lowest numbers>>");
 
            while (true)
            {
                var intls = Console.ReadLine();
 
                var inlst = new string[] { };
                var inlstint = new int[5];
 
                inlst = intls.Split(',');
                var trybrk = false;
                var j = 0;
                foreach (var i in inlst)
                {
                    //Console.WriteLine(i);
                    try
                    {
                        var con = Convert.ToInt32(i);
                    }
                    catch (FormatException e)
                    {
                        trybrk = true;
                    }
                    Console.WriteLine(i);
                    inlstint[j++= Convert.ToInt32(i);
                }
                if (trybrk)
                {
                    Console.WriteLine("\nList can only contain numbers! 
Please re-enter the list>");
                }
                else
                {
                    if (inlst.Length == 5)
                    {
                        Array.Sort(inlstint);
 
                        Console.WriteLine("\n\nFirst 3 smallest numbers are>>\n");
                        for (var i = 0; i < inlstint.Length; i++)
                        {
                            if (i < 3)
                            {
                                Console.WriteLine(inlstint[i]);
                            }
                            else
                            {
                                break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        Console.WriteLine("\nPlease have 5 numbers in the list. 
Try again>>");
                        Console.WriteLine("\\nEnter a list numbers separated 
by comma>>");
                    }
                }
            }
        }
    }
}