C# - TimeSpan.Add() Method with Example

In this tutorial, we will learn about the C# TimeSpan.Add() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 30, 2023

TimeSpan.Add() Method

The TimeSpan.Add() method is used to return an object that contains the sum of the specified TimeSpan object with the current TimeSpan object.

Syntax

TimeSpan TimeSpan.Add(TimeSpan obj);

Parameter(s)

  • obj : An object whose value to be added with a value of the current object.

Return Value

This method returns the addition of values of the specified object and current object.

Exception(s)

  • System.OverflowException

C# Example of TimeSpan.Add() Method

The source code to demonstrate the use of Add() method of TimeSpan structure is given below. The given program is compiled and executed successfully.

using System;

class TimeSpanDemo {
  //Entry point of Program
  static public void Main() {
    //Here we create timespan with day,hour,minutes and seconds
    TimeSpan timespan1 = new TimeSpan(4, 2, 20, 0);
    TimeSpan timespan2 = new TimeSpan(2, 2, 10, 0);
    TimeSpan timespan3;

    timespan3 = timespan1.Add(timespan2);

    //Sum of timespan1 and timespan2 
    Console.WriteLine("Sum of timespan1 and timespan2 : " + timespan3);
  }
}

Output

Sum of timespan1 and timespan2 : 6.04:30:00
Press any key to continue . . .

C# TimeSpan Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.