Members of datatype and DateTime, TimeSpan

In this article we will see members of C# datatypes.

Members of datatype

Members of numerical datatype

Experimenting with the intrinsic C# data types, understand that the numerical types of .NET support MaxValue and MinValue properties that provide information regarding the range a given type can store. In addition to the MinValue/MaxValue properties, a given numerical system type may define further useful members. For example, the System.Double type allows you to obtain the values for epsilon and infinity which might be of interest to those of you with a mathematical flare.

Lets understand that by a simple program:

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Members of numerical datatype:");
        Console.WriteLine("Max value of Int: {0}",int.MaxValue);
        Console.WriteLine("Min value of Int: {0}",int.MinValue);
        Console.WriteLine("Max value of Float: {0}", float.MaxValue);
        Console.WriteLine("Min value of Float: {0}", float.MinValue);
        Console.WriteLine("Max value of Double: {0}", double.MaxValue);
        Console.WriteLine("Min value of Double: {0}", double.MinValue);
        Console.WriteLine("Epsilon: {0}",double.Epsilon);
        Console.WriteLine("Positive infinity: {0}",double.PositiveInfinity);
        Console.WriteLine("Negative infinity: {0}",double.NegativeInfinity);
        Console.ReadLine();
    }
}
  
  

Output


Members of numerical datatype:
Max value of Int: 2147483647
Min value of Int: -2147483648
Max value of Float: 3.402823E+38
Min value of Float: -3.402823E+38
Max value of Double: 1.79769313486232E+308
Min value of Double: -1.79769313486232E+308
Epsilon: 4.94065645841247E-324
Positive infinity: Infinity
Negative infinity: -Infinity
  

Members of System.Boolean

Consider the System.Boolean data type. The only valid assignment a C# bool can take is from the set {true | false}. Given this point, it should be clear that System.Boolean does not support a MinValue/MaxValue property set but rather TrueString/FalseString (which yields the string "True" or "False", respectively). Here’s an example program:

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        //Members of System.Boolean
        Console.WriteLine("true string: {0}",bool.TrueString);
        //GetType() method returns datatype of variable
        Console.WriteLine("type of TrueString: {0}",bool.TrueString.GetType());
        Console.WriteLine("false string: {0}",bool.FalseString);
        Console.WriteLine("type of FalseString: {0}",bool.FalseString.GetType());
        Console.ReadLine();
    }
}
  
  

Output


true string: True
type of TrueString: System.String
false string: False
type of FalseString: System.String
  

Members of System.Char

C# textual data is represented by the string and char keywords, which are simple shorthand notations for System.String and System.Char, both of which are Unicode under the hood. As you might already know, a string represents a contiguous set of characters (e.g., "Hello"), while the char can represent a single slot in a string (e.g., 'H').

The System.Char type provides you with a great deal of functionality beyond the ability to hold a single point of character data. Using the static methods of System.Char, you are able to determine whether a given character is numerical, alphabetical, a point of punctuation, or whatnot. Consider the following program:

How to know that given character is digit or letter in C#?

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        //Members of System.Char
        char MyChar = 'H';
        Console.WriteLine("Is MyChar digit? => {0}",char.IsDigit(MyChar));
        Console.WriteLine("Is MyChar letter? => {0}",char.IsLetter(MyChar));
        Console.ReadLine();
    }
}
  
  

Output


Is MyChar digit? => False
Is MyChar letter? => True
  

Parsing values from string data

The .NET data types provide the ability to generate a variable of their underlying type given a textual equivalent (e.g., parsing). This technique can be extremely helpful when you want to convert some user input data (such as a selection from a GUI-based, drop-down list box) into a numerical value. Consider the following parsing logic:

How to parse data from string to other datatype?

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        //Parsing data from string
        //string to bool
        bool b = bool.Parse("True");
        Console.WriteLine("b: {0}",b);
        //string to double
        double d = double.Parse("96.656");
        Console.WriteLine("d: {0}",d);
        //string to int
        int i = int.Parse("987");
        Console.WriteLine("i: {0}",i);
        //string to char
        char c = char.Parse("c");
        Console.WriteLine("c: {0}",c);
        Console.ReadLine();
    }
}
  
  

Output


b: True
d: 96.656
i: 987
c: c
  

System.DateTime

The System namespace defines a few useful data types for which there are no C# keywords, such as the DateTime and TimeSpan structures.

The DateTime type contains data that represents a specific date (month, day, year) and time value, both of which may be formatted in a variety of ways using the supplied members.

Find day of week from DateTime instance in C#


DateTime dt = new DateTime(1997,09,27);
Console.WriteLine("day is {0}",dt.DayOfWeek);
  

How to extract day, month or year from DateTime instance?


DateTime dt = new DateTime(1997,09,27);
Console.WriteLine("Day is {0}",dt.Day);
Console.WriteLine("month is {0}",dt.Month);
Console.WriteLine("year is {0}",dt.Year);
  

How to add day, month or year in DateTime instance?


DateTime dt = new DateTime(1997,09,27);
dt = dt.AddDays(2);
dt = dt.AddMonths(1);
dt = dt.AddYears(4);
  

Sample program of DateTime


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        DateTime dt = new DateTime(1997,09,27);
        Console.WriteLine("day of week is {0}",dt.DayOfWeek);
        Console.WriteLine("day of year is {0}",dt.DayOfYear);
        Console.WriteLine("date is {0}",dt.Date);
        //Extract day, month, year form DateTime
        Console.WriteLine("Day is {0}",dt.Day);
        Console.WriteLine("month is {0}",dt.Month);
        Console.WriteLine("year is {0}",dt.Year);
        //Add day, month, year form DateTime
        dt = dt.AddDays(2);
        dt = dt.AddMonths(1);
        dt = dt.AddYears(4);
        Console.WriteLine("After adding date is: {0}",dt.Date);
        Console.WriteLine("Is day light saving time: {0}",dt.IsDaylightSavingTime());
        Console.ReadLine();

    }
}
  
  

Output


day of week is Saturday
day of year is 270
date is 09/27/1997 00:00:00
Day is 27
month is 9
year is 1997
After adding date is: 10/29/2001 00:00:00
Is day light saving time: False
  

what is DayLightSavingTime() in C#?

It ndicates whether this instance of System.DateTime is within the daylight saving time range for the current time zone.

It returns true if the value of the System.DateTime.Kind property is System.DateTimeKind.Local or System.DateTimeKind.Unspecified and the value of this instance of System.DateTime is within the daylight saving time range for the local time zone; false if System.DateTime.Kind is System.DateTimeKind.Utc. To know more about what daylight saving time commonly know as DST go to
To know more about properties and methods of System.DateTime go to:

System.TimeSpan

The TimeSpan structure allows you to easily define and transform units of time using various members.

How to add or subtract time is TimeSpan instance ?


TimeSpan ts = new TimeSpan(8, 15, 0);
//to add or subtract time in TimeSpan instance
Console.WriteLine(ts.Add(new TimeSpan(2,30,0)));
  
TimeSpan.Add() and TimeSpan.Subtract(): returns A new object that represents the value of this instance plus/minus the value of ts.

How to extract hour, minute or second from TimeSpan instance ?


TimeSpan ts = new TimeSpan(8, 15, 0);
Console.WriteLine("Hour: {0}",ts.Hours);
Console.WriteLine("Minutes: {0}", ts.Minutes);
Console.WriteLine("Seconds: {0}", ts.Seconds);
  

Sample program of System.TimeSpan


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        TimeSpan ts = new TimeSpan(8, 15, 0);
        //to add time in TimeSpan instance
        Console.WriteLine("After adding: {0}",ts.Add(new TimeSpan(2,30,0)));
        Console.WriteLine("After subtracting: {0}",ts.Subtract(new TimeSpan(1,23,45)));
        Console.WriteLine("Hour: {0}",ts.Hours);
        Console.WriteLine("Minutes: {0}", ts.Minutes);
        Console.WriteLine("Seconds: {0}", ts.Seconds);
        Console.ReadLine();
    }
}
  
  

Output


After adding: 10:45:00
After subtracting: 06:51:15
Hour: 8
Minutes: 15
Seconds: 0
  
Programers who are from JAVA remember that DateTime and TimeSpan are structers C# while in JAVA everything is class.

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword