阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


 

废话少说,看程序就明白了。

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    
/// <summary>
    
/// C#转换操作符号学习
    
/// </summary>

    public sealed class MyCls
    
{
        
private string _data;

        
public string Data
        
{
            
get return _data; }
            
set { _data = value; }
        }


        
public MyCls(int intValue)
        
{
            _data 
= intValue.ToString();
        }


        
public MyCls(float singleValue)
        
{
            _data 
= singleValue.ToString();
        }


        
public int ToInt32()
        
{
            
int returnValue = -1;
            
if (Int32.TryParse(_data, out returnValue))
            
{
                
return returnValue;
            }

            
else
            
{
                
return -1;
            }

            
        }


        
public float ToSingle()
        
{
            
float returnValue = float.NaN;
            
if (float.TryParse(_data, out returnValue))
            
{
                
return returnValue;
            }

            
else
            
{
                
return float.NaN;
            }


        }


        
隐式转换到基元类型

        
显式从基元转换到Mycls类型
    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            
//不使用类型转换操作符
            MyCls a = new MyCls(1);
            
int i = a.ToInt32();
            MyCls b 
= new MyCls(float.MinValue);
            
float f = b.ToSingle();

            
//使用类型转换操作符
            MyCls c = (MyCls)i;
            MyCls d 
= (MyCls)f;
            
int k = c;
            
float t = d;
        }

    }

}


 

可研究.NET的Decimal类型,很有学习价值。

posted on 2008-05-29 21:51  阿牛-专注金融行业开发  阅读(334)  评论(0编辑  收藏  举报