Sunday 4 April 2021

C# How to Copy Tuple List Values into Integer Array

 void Main()
{
    List<Tuple<int,string>> ab = new List<Tuple<int,string>>();
    ab.Add(Tuple.Create(1,"hello1"));
    ab.Add(Tuple.Create(2,"hello2"));
    ab.Add(Tuple.Create(3,"hello3"));
    ab.Add(Tuple.Create(4,"hello4"));
    ab.Add(Tuple.Create(5,"hello5"));
    ab.Add(Tuple.Create(6,"hello6"));
    ab.Add(Tuple.Create(7,"hello7"));
    //ab.Dump();
    
    string g = "";
    
    int[] x = new int[ab.Count()];    
    int counter = 0;
    foreach(var r in ab)
    {
        if(r.Item1%2==0)
        {
            x[counter] = r.Item1;    
            counter++;
        }
    }
    x.Dump();
    
    var result = ab.Where(p=>x.Contains(p.Item1)).ToList();
    result.Dump();
}

// Define other methods and classes here


Output:-

C# How to Insert Values in Tuple List

 void Main()
{
    List<Tuple<int,string>> ab = new List<Tuple<int,string>>();
    ab.Add(Tuple.Create(1,"hello1"));
    ab.Add(Tuple.Create(2,"hello2"));
    ab.Add(Tuple.Create(3,"hello3"));
    ab.Add(Tuple.Create(4,"hello4"));
    ab.Add(Tuple.Create(5,"hello5"));
    ab.Add(Tuple.Create(6,"hello6"));
    ab.Add(Tuple.Create(7,"hello7"));

}

Monday 29 March 2021

C# find number in array using foreach loop

 Program.cs

 void Main()
{
    int val = 3;
    int? result = null;
    int[] arr = new int[] {1,2,3,4,5,6,7};
    foreach(int r in arr)
    {
        if(val==r)
        {
            result = int.Parse("0"+r);
            break;
        }
    }
    result.Dump();    
}

output:-

C# how to calculate size / length of array

 void Main()
{
    int[] arr = new int[] {1,2,3,4,5,6,7};
    string ab = arr.Length.ToString();
    ab.Dump();
}

C# How to get Current Date Time from System

 

 void Main()
{
    string ab = Convert.ToString(DateTime.Now);
    ab.Dump();
}

How to Sort Time 12Hrs Format in C# using List

 Declare Class List 

public class TypeData
{
    public string Times {get;set;}
}

 

Write Down Main Function inside Console Application

void Main()
{
    List<TypeData> data = new List<TypeData>();
    data.Add(new TypeData {Times = "01:00 PM"});
    data.Add(new TypeData {Times = "02:00 PM"});
    data.Add(new TypeData {Times = "03:00 PM"});
    data.Add(new TypeData {Times = "04:00 PM"});
    data.Add(new TypeData {Times = "05:00 PM"});
    data.Add(new TypeData {Times = "06:00 PM"});
    data.Add(new TypeData {Times = "07:00 PM"});
    data.Add(new TypeData {Times = "08:00 PM"});
    data.Add(new TypeData {Times = "09:00 PM"});
    data.Add(new TypeData {Times = "10:00 PM"});
    data.Add(new TypeData {Times = "11:00 PM"});
    data.Add(new TypeData {Times = "12:00 PM"});
    data.Add(new TypeData {Times = "01:00 AM"});
    data.Add(new TypeData {Times = "02:00 AM"});
    data.Add(new TypeData {Times = "03:00 AM"});
    data.Add(new TypeData {Times = "04:00 AM"});
    data.Add(new TypeData {Times = "05:00 AM"});
    data.Add(new TypeData {Times = "06:00 AM"});
    data.Add(new TypeData {Times = "07:00 AM"});
    data.Add(new TypeData {Times = "08:00 AM"});
    data.Add(new TypeData {Times = "09:00 AM"});
    data.Add(new TypeData {Times = "10:00 AM"});
    data.Add(new TypeData {Times = "11:00 AM"});
    data.Add(new TypeData {Times = "12:00 AM"});
    //data.Dump();
    
    var datafinal_AM = data.Where(x=>x.Times.Contains("AM")).ToList();
    var datafinal_PM = data.Where(x=>x.Times.Contains("PM")).ToList();
    
    List<TypeData> finalR = new List<TypeData>();
    
    foreach(var rows in datafinal_AM)
    {
        finalR.Add(new TypeData {Times = rows.Times});
    }
    foreach(var rows in datafinal_PM)
    {
        finalR.Add(new TypeData {Times = rows.Times});
    }
    

}

 

 Output :-