[Csharp]
[Csharp]
Namespace _ 3. _ 12 forced type conversion
{
/// <Summary>
/// It is always safe to forcibly convert the instance of the derived class to the base class type. You can even convert a derived class instance to an abstract base class type. After being forcibly converted to the base type, the base reference is the alias of the basic part of the derived class object.
/// Forcibly convert a value type into a basic interface with different semantics. When a value type is forcibly converted to an interface type, a different entity is created. The interface is a reference type. Whenever a value type is set
/// Forcible conversion to the reference type, including converting a value type to an interface, will be boxed. Binning allocates memory and copies the value type to the managed stack. The original value has nothing to do with the copy.
/// Any change will not affect the other one.
/// </Summary>
Public interface IAdd {
Void Increment ();
Int Count
{
Get;
}
}
Public struct XStruct: IAdd {
Private int propCount;
Public void Increment (){
PropCount ++;
}
Public int Count {
Get {
Return propCount;
}
}
}
Public class Starter {
Static void Main (){
XStruct xstruct = new XStruct ();
Xstruct. Increment ();
IAdd obj = xstruct;
Xstruct. Increment ();
Console. WriteLine (xstruct. Count); // 2
Console. WriteLine (obj. Count); // 1
// It indicates that obj and xstruct point to different entities. obj = xstruct creates a copy.
// This is a significant sign of the class type
IAdd boj2 = obj;
Console. WriteLine (obj. Count); // 1
// Indicates that obi and obj2 point to the same object. obj = obj2 assigns a value to obj.
// This is a flag of the reference type and can be significant.
Console. ReadKey ();
}
}
}
Namespace _ 3. _ 12 forced type conversion
{
/// <Summary>
/// It is always safe to forcibly convert the instance of the derived class to the base class type. You can even convert a derived class instance to an abstract base class type. After being forcibly converted to the base type, the base reference is the alias of the basic part of the derived class object.
/// Forcibly convert a value type into a basic interface with different semantics. When a value type is forcibly converted to an interface type, a different entity is created. The interface is a reference type. Whenever a value type is set
/// Forcible conversion to the reference type, including converting a value type to an interface, will be boxed. Binning allocates memory and copies the value type to the managed stack. The original value has nothing to do with the copy.
/// Any change will not affect the other one.
/// </Summary>
Public interface IAdd {
Void Increment ();
Int Count
{
Get;
}
}
Public struct XStruct: IAdd {
Private int propCount;
Public void Increment (){
PropCount ++;
}
Public int Count {
Get {
Return propCount;
}
}
}
Public class Starter {
Static void Main (){
XStruct xstruct = new XStruct ();
Xstruct. Increment ();
IAdd obj = xstruct;
Xstruct. Increment ();
Console. WriteLine (xstruct. Count); // 2
Console. WriteLine (obj. Count); // 1
// It indicates that obj and xstruct point to different entities. obj = xstruct creates a copy.
// This is a significant sign of the class type
IAdd boj2 = obj;
Console. WriteLine (obj. Count); // 1
// Indicates that obi and obj2 point to the same object. obj = obj2 assigns a value to obj.
// This is a flag of the reference type and can be significant.
Console. ReadKey ();
}
}
}
In C #, data types include value type and reference type.
Value Type: struct and enumeration
Reference Type: Class, interface, array, delegate
Conclusion:
When we declare that the object (s1, s2) is of the struct type, the object is of the value type, and the object is created in the stack
When the declared object (ic1 and ic2) is of the interface type, the object is of the reference type, and the object is created in the heap