Let us first understand what is called the object class.
Object class:
The object class is the base class for all types, all of which are derived from him, and all classes in C # are directly or indirectly inherited from the Sytem.object class (perhaps a bit confusing, it's okay, for example, if the object class is a trunk, all the classes we've learned before are branches or leaves.) Understand it). Therefore, a variable of type object can be given any type of value.
To set an object variable:
A variable declaration of type object with the Object keyword, which is the keyword in the. The NET Framework structure is defined in the predefined namespace system that we provide, which is the alias of the class System.Object. The definition format is this: the object variable name;
Instance:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Test { class program { static void Main (string[] args) { object int,str,doub,ch; Defines 4 object variables Int = 1; Success of the shaping assignment Str = "HC666"; String type assignment succeeds Doub = 12.32; Double type assignment succeeds Ch = ' male '; Char type assignment succeeded Console.WriteLine ("Int = {0}\tstr = {1}\tdoub = {2}\tch = {3}", Int,str,doub,ch);} }
Output Result: (fully consistent ^_^)
Example exploration:
The definition of object above emphasizes that object is the base class for all types, so we define a struct class and class can also be converted to object?? In the above code I have added some elements:
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace Test { class program {public struct Student {public string name; public char sex; public uint-age; } static void Main (string[] args) { Student stu = new student{name = "HC666", sex = ' male ', age = +}; Initialize Stu Object m = stu; Converts Stu to an object of type M Student x = (Student) m; Converts an object of type objects m to student x to be cast Console.WriteLine ("Name:{0}\tsex:{1}\tage:{2}", x.name,x.sex,x.age); } }
<span style= "FONT-SIZE:18PX;" > The result is this:</span>
The above example shows that the struct type and class type can be transformed with the object type, and the conversion process is as follows:
struct---->object---->struct; (only struct---->object cannot output value), the value in Stu is not changed during conversion
The above is the C # Learning Diary---Reference type of object class content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!