Find out age from given date in C#

It’s really simple. Just follow the code

private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
   DateTime SD = DateTime.Parse(dateTimePicker1.Text);
   DateTime DOB = new DateTime(SD.Year,SD.Month,SD.Day);
   int y = 0, m = 0, d = 0;
   FindAge(DOB, out y, out m, out d);
   label1.Text = "Year: " + y.ToString() + "\n Month: " + m.ToString() + "\n Day: " + d.ToString();
}

private void FindAge(DateTime DOB, out int y, out int m, out int d) {
   DateTime CD = DateTime.Now;
   y = CD.Year - DOB.Year;
   m = CD.Month - DOB.Month;
   d = CD.Day - DOB.Day;

   if(d < 0) {
      d+= DateTime.DaysInMonth(DOB.Year, DOB.Month);
      m--;
   }
   if(m < 0){
      m+=12;
      y--;
   }
}
Advertisements

Deep copy in C#

Deep Copy: A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must use IClonable Interface.

// Program: Deep Copy
// Purpose: Training period
// By: H.M. Saiful Islam

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeepCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            Cloner mySource = new Cloner(5);
            Cloner myTarget = (Cloner)mySource.Clone();
            Console.WriteLine("value: {0}", myTarget.MyContent.Val);
            mySource.MyContent.Val = 10;
            Console.WriteLine("value: {0}", myTarget.MyContent.Val);
            Console.ReadKey();
        }

        public class Content
        {
            public int Val;
        }

        public class Cloner : ICloneable
        {
            public Content MyContent = new Content();

            public Cloner(int newVal)
            {
                MyContent.Val = newVal;
            }

            #region ICloneable Members

            public object Clone()
            {
                Cloner CC = new Cloner(MyContent.Val);
                return CC;
            }

            #endregion
        }

    }
}

Shallow copy in C#

Shallow Copy: A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied — the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.

// Program: Shallow Copy
// Purpose: Training period
// By: H.M. Saiful Islam

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShallowCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            Cloner mySource = new Cloner(5);
            Cloner myTarget = (Cloner)mySource.GetCopy();
            Console.WriteLine("value: {0}", myTarget.MyContent.Val);
            Console.ReadKey();
        }

        public class Content
        {
            public int Val;
        }

        public class Cloner
        {
            public Content MyContent = new Content();

            public Cloner(int newVal)
            {
                MyContent.Val = newVal;
            }

            public object GetCopy()
            {
                return MemberwiseClone();
            }
        }

    }
}