Serving Information Simply

Friday 18 January 2013

Data Types in C#.Net


C# allows you to declare two kinds of variables: value types and reference types. The value types hold actual values, while reference types hold references to values stored somewhere in memory. 

Also value types are allocated on the stack and are available in most programming languages. Reference types are allocated on the heap and typically represent class instances. 
All the data types in .net framework are available within the namespace System. 

There are two types of data type in C#
1.   Primitive types or predefined

Ex:  byte, short, int, float, double, long, char, bool, DateTime, string, object etc...

 2.   Non-primitive types or User Defined

Ex: class, struct, enum, interface, delegate, array.
These are Special keywords used to define type of data and range of data.

Let’s discuss in brief……

Value Types

-          It Hold the real value. Internally are structure
-          Can of different types
o   Integrals
§  Number without decimal point
§  byte – 1 byte (unsigned)
§  short – 2 bytes
§  int – 4 bytes
§  long – 8 byte
§  sbyte – 1 (singed)
§  ushort (unsigned)
§  uint
§  ulong
o   Floatings
§  float – 4 bytes (accuracy 7 decimal points)
§  double –8 bytes (accuracy 15 decimal points)
§  decimal  - 16 bytes (accuracy 28 decimal points)
o   Characters
§  char – 2 bytes
o   Boolean
§  bool – 1 byte
Note:

Use sizeof() operator to view size of a data type

Output Style

  1. Java Style
    1. Use + to concatenate the results
    2. Example
Console.WriteLine("Size of decimal is " + sizeof(decimal));

  1. C – Style Output
    1. Use place holder like {0}, {1} etc. for different set of variables
    2. Example 1
Console.WriteLine("Size of decimal is {0}", sizeof(decimal));

Example 2

int a=5, b=7;
Console.WriteLine("sum of {0} and {1} is {2}", a, b, a + b);

Reference Types

-          Reference type Used to hold the address
-          These are …
o   string – To hold address of strings only
o   object – To hold address of any data type

Date and Time

Date time is one of the most frequently used data type in C#, here I am going to let you know some of properties about it also.

DateTime CurrentTime = DateTime.Now;//display’s current Date Time. 

int Days = DateTime.DaysInMonth(2011, 7);// it displays “31”.  

Examples

DateTime Now = DateTime.Now;
Console.WriteLine("Time:" + Now.TimeOfDay);//it display only Current Time of that day.

Console.WriteLine("Current month: "+Now.Month);//it display what is Current Month.
Console.WriteLine("To Day is: "+Now.DayOfWeek);// it gives current Day name.

Try these examples and watch the results....

DateTime myDateTime = DateTime.Parse("7/28/2011 10:17:30");
TimeSpan TimeSpan = new TimeSpan(3, 4, 3, 12);
DateTime newDateTime = myDateTime + TimeSpan;
DateTime subtracttime = myDateTime - TimeSpan;
Console.WriteLine("myDateTime + TimeSpan = " + newDateTime);
Console.WriteLine("myDateTime - TimeSpan = " + subtracttime);

DateTime now = DateTime.Now;
DateTime addtwodays = DateTime.Now.AddDays(2);//it adds two days to current date and time.
DateTime addminutes = DateTime.Now.AddMinutes(50);
Console.WriteLine("Current DateTime:" + now);
Console.WriteLine("Addition of two days, DateTime:" + addtwodays);
Console.WriteLine("adition of minutes, DateTime:" + addminutes);


using System;
class timespan
{
public static void Main()
{
DateTime Time = DateTime.Now;
TimeSpan TimeSpan = new TimeSpan(24, 00, 00);
DateTime Time24 = Time.Subtract(TimeSpan);
Console.WriteLine("myTimeSpan = " + TimeSpan);
Console.WriteLine("time before 24 hours = " + Time24);
Console.ReadLine();
}
}

Thanks .Keep visiting guys......

2 comments:

  1. best to learner if, he want to success in his line ........

    ReplyDelete