Lesson 2.17

Object Data Type C# - Programming Example


Objective

What is object data type in c#?
How to declare and initialize object data type?
Programming examples of object data type

What is object Data Types in C#?

"An object datatype is a such type of datatype which can holds any type of variables like value type variables (numeric, Boolean, char , data) or reference type variable (string, array, class or interface). In a simple word, you can assign any type of value to a object. But before assigning value conversion is required.

When a value converted into an object type, it is called boxing and when an object is converted into value, it is called unboxing. "

TypeRangeSize.Net Framework type
object----System.object

How to declare and initialize object data type in c# ?

You can declare object data type in following manner.

		  
  object obj=123; //boxing
		  

Programming Example

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

namespace object_data_type
{
    class Program
    {
        static void Main(string[] args)
        {
            object obj1 = 123; //boxing
            object obj2 = 345; //boxing
            int result;
            result = (int)obj1 + (int)obj2; //Unboxing
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
	  

Output

468
_

Summary

In this chapter you have learned what object data type is in c # and how to use it in c# programming. You have also learned how to boxing and unboxing object data type. In the next lesson you will learn about different types of operators in C#.

BackNext