zoukankan      html  css  js  c++  java
  • CLR via C#学习笔记:C#转换操作符号学习


     

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

    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类型,很有学习价值。

  • 相关阅读:
    一个贼基础的 编码解码方式
    SQL 中循环、for循环、游标
    sql中判断是否存在 数据库、表、存储过程、函数
    sql 同步表或同步表的时候更改部分字段
    sql存储过程的建立
    POJ
    UCloud 的安全秘钥 (计蒜客初赛第五场)(待解决)
    UCloud 机房的网络搭建(计蒜客初赛第五场)
    2017 计蒜之道 初赛 第四场
    腾讯课堂的物理实验(2017计蒜客初赛第三场)
  • 原文地址:https://www.cnblogs.com/rockniu/p/1210292.html
Copyright © 2011-2022 走看看