新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
常用自定義C#類型轉(zhuǎn)換函數(shù)
這里將介紹常用自定義C#類型轉(zhuǎn)換函數(shù),大家經(jīng)常碰到類弄轉(zhuǎn)換,但都不知道哪些系統(tǒng)函數(shù)才可以轉(zhuǎn)換。希望本文能對大家有所幫助。

- ///
- /// 將字符型類型轉(zhuǎn)換為整型值
- ///
- /// 字符型
- /// 無法轉(zhuǎn)換時的默認值
- ///
整型值 - public static int IntParse(string objValue, int defaultValue)
- {
- int returnValue = defaultValue;
- if (!string.IsNullOrEmpty(objValue))
- {
- try
- {
- returnValue = int.Parse(objValue);
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- return returnValue;
- }
- ///
- /// 將對象類型轉(zhuǎn)換為整型值
- ///
- /// 對象類型
- /// 無法轉(zhuǎn)換時的默認值
- ///
整型值 - public static int IntParse(object objValue, int defaultValue)
- {
- int returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = int.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// 將對象類型轉(zhuǎn)換為整型值
- ///
- /// 對象類型
- ///
整型值 - public static int IntParse(object objValue)
- {
- return IntParse(objValue, 0);
- }
- ///
- /// 將對象類型轉(zhuǎn)換為日期值
- ///
- /// 對象類型
- /// 無法轉(zhuǎn)換時的默認值
- ///
日期值 - public static DateTime DateTimeParse(object objValue, DateTime defaultValue)
- {
- DateTime returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = DateTime.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// 將對象類型轉(zhuǎn)換為日期值
- ///
- /// 對象類型
- ///
日期值 - public static DateTime DateTimeParse(object objValue)
- {
- return DateTimeParse(objValue, DateTime.MinValue);
- }
- ///
- /// 將對象類型轉(zhuǎn)換為字符型
- ///
- /// 對象類型
- /// 無法轉(zhuǎn)換時的默認值
- ///
字符型 - public static string StringParse(object objValue, string defaultValue)
- {
- string returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = objValue.ToString();
- }
- catch
- {
- returnValue = defaultValue; ;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// 將對象類型轉(zhuǎn)換為字符型
- ///
- /// 對象類型
- ///
字符型 - public static string StringParse(object objValue)
- {
- return StringParse(objValue, string.Empty);
- }
- ///
- /// 將對象類型轉(zhuǎn)換為GUID
- ///
- /// 對象類型
- /// 無法轉(zhuǎn)換時的默認值
- ///
GUID - public static Guid GuidParse(object objValue, Guid defaultValue)
- {
- Guid returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = new Guid(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue; ;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// 將對象類型轉(zhuǎn)換為GUID
- ///
- /// 對象類型
- ///
GUID - public static Guid GuidParse(object objValue)
- {
- return GuidParse(objValue, Guid.Empty);
- }
- ///
- /// C#類型轉(zhuǎn)換函數(shù)
- ///
- ///
目標類型值 - /// 對象類型
- /// 無法轉(zhuǎn)換時的默認值
- ///
目標類型值 - public static T Parse
(object objValue, T defaultValue) - {
- T returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = (T)objValue;
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- ///
- /// C#類型轉(zhuǎn)換函數(shù)
- ///
- ///
目標類型值 - /// 對象類型
- ///
目標類型值 - public static T Parse
(object objValue) - {
- return Parse
(objValue, default(T)); - }
【編輯推薦】
- C#運算符重載實現(xiàn)復數(shù)運算
- C#運算符重載實例解析
- C#運算符重載的一些總結(jié)
- C#運算符重載“>”的操作淺析
- C#運算符優(yōu)先級介紹
網(wǎng)頁名稱:常用自定義C#類型轉(zhuǎn)換函數(shù)
網(wǎng)站鏈接:http://www.dlmjj.cn/article/djepdcc.html


咨詢
建站咨詢
