泛型(CSDN轉載)

函數的參數不同叫多態,函數的參數類型可以不確定嗎?

函數的返回值只能是一個嗎?函數的返回值可以不確定嗎?

泛型是一種特殊的類型,它把指定類型的工作推遲到客戶端代碼聲明并實例化類或方法的時候進行。

下面是兩個經典示例:

?

1.輸入一個字符串,轉化為想要的類型。

利用泛型的特性,返回值可以是指定的類型。

2.比較兩個對象,返回值較大的一個。

using System;

using System.Collections.Generic;

using System.Text;

?

namespace FamilyManage

{

??? class CGeneric

??? {

??????? //數據轉換

??????? static public T Convert<T>(string s) where T : IConvertible

??????? {

??????????? return (T)System.Convert.ChangeType(s, typeof(T));

??????? }

??????? //取兩個數較大的一個

??????? static public T Max<T>(T first, T second) where T : IComparable<T>

??????? {

??????????? if (first.CompareTo(second) > 0)

??????????????? return first;

??????????? return second;

??????? }

??????? //使用

??????? static public void test()

??????? {?????????

??????????? int iMax = Max(123, 456);

??????????? double dMax = Max<double>(1.23, 4.56);//可以指定返回類型

??????

??????????? int iConvert = Convert<int>("123456");

??????????? float fConvert = Convert<float>("123.456");

?????????

??????????? System.Windows.Forms.MessageBox.Show(iMax + "|" + dMax + "|" + iConvert + "|" + fConvert);

??????? }

??? }

}

?

  System.Convert.ChangeType(s, typeof(T));這個函數,在以往的項目中用的比較少,用的最多的還是Convert.ToInt()和Convert.ToDouble()。一般多是把一個字符串轉化需要的整型數據或者浮點型數據。例如:

  

            string intString="123";int result=System.Convert.ToInt32(intString);Console.WriteLine(result); //輸出123  

?

  也有用Int類或者Double類的Parse或者TryParse函數進行轉化的,在此不在舉例。把注意力拉回到System.Convert.ChangeType(s, typeof(T));這個函數。

  ChangeType:返回一個指定類型的對象,該對象的值等效于指定的對象。此方法有四種函數重載。如下面的表格所示:

?

        double d = -2.345;int i = (int)Convert.ChangeType(d, typeof(int));Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i);string s = "12/12/98";DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt);     

  

                                          想要了解更多請參看MSDN的Convert類。

?

轉載于:https://www.cnblogs.com/zhangyuanbo12358/p/3612069.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377218.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377218.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377218.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《MySQL tips:隱式類型轉換與隱式字符編碼轉換對查詢效率的影響》

維護一個交易系統&#xff0c;交易記錄表tradelog包含交易流水號(tradeid)、交易員id(operator)、交易時間(t_modified)等字段。 create table tradelog (id int(11) not null,tradeid varchar(32) default null,operator int(11) default null,t_modified datetime default n…

HDU4291 A Short problem

求通項和斐波那契數列的方法一樣&#xff0c;矩陣快速冪。 這道題麻煩在套了三層。 但其實取模這種操作肯定會出現循環的&#xff0c;可以先本地暴出循環節&#xff0c;1000000007對應的循環節是222222224&#xff0c;222222224對應的循環節是183120。 最外層的結果是對1000000…

pvr波形是什么意思_PVR的完整形式是什么?

pvr波形是什么意思PVR&#xff1a;Priya村路演 (PVR: Priya Village Roadshow) PVR is an abbreviation of Priya Village Roadshow. It is one of the biggest and leading multiplex cinema chains in India. PVR是Priya Village Roadshow的縮寫 。 它是印度最大和領先的多元…

《MySQL——查詢長時間不返回的三種原因與查詢慢的原因》

目錄查詢長時間不返回等MDL鎖等flush等行鎖查詢慢構造一張表&#xff0c;表有兩個字段id和c&#xff0c;再里面插入了10萬行記錄 create table t (id int(11) not null,c int(11) default null,primary key (id) ) engine InnoDB;delimiter ;; create procedure idata() begi…

Linux 命令積累 fuser lsof mtr

fuser 用途:使用文件或文件結構識別進程,即:查詢都有哪些進程占用了制定的文件、目錄、設備或套接字;lsof MTR fuser命令 用途:使用文件或文件結構識別進程,即:查詢都有哪些進程占用了制定的文件、目錄、設備或套接字;語法:fuser [-c|-d|-f] [-k] [-u] [-x] [-V] 文件/目錄…

線程終止問題

http://topic.csdn.net/u/20080429/09/9cfe5204-20b5-40fb-ac12-afdc1e4939e9.html?590511460 線程終止問題 http://blog.csdn.net/wuyazhe/article/details/1771470 帶有消息機制的線程 - CustomMessageQueue(c#) using System; using System.Collections.Generic; using Sy…

HTH的完整形式是什么?

HTH&#xff1a;希望這個(那個)有幫助 (HTH: Hope This (That) Helps) HTH is an abbreviation of "Hope This (That) Helps". HTH是“希望有幫助”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking si…

排序算法復習—希爾排序

希爾排序&#xff0c;也稱遞減增量排序算法&#xff0c;是插入排序的一種更高效的改進版本。 希爾排序將整個待排元素序列分割成若干個子序列&#xff08;由相隔某個“增量”的元素組成的&#xff09;分別進行直接插入排序&#xff0c;過程中較小的元素&#xff0c;跳躍式的往前…

《MySQL——幻讀與next-key lock與間隙鎖帶來的死鎖》

create table t (id int(11) not null,c int(11) default null,d int(11) default null,primary key (id),key c (c) ) engine InnoDB;insert into t values(0,0,0),(5,5,5),(10,10,10),(15,15,15),(20,20,20),(25,25,25);該表除了主鍵id&#xff0c;還有索引c。 問下面的語句…

css 陰影 效果_CSS陰影效果

css 陰影 效果CSS中的陰影效果 (Shadow Effects in CSS) It is always good to make our web pages stylish and beautiful, web pages that would catch users eyes instantly but one gets confused as to how to style his or her web page. The confusion is quite legit t…

java常見的ClassNotFoundException-----菜鳥學習java

java常見的ClassNotFoundException 1 - java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory 添加包common-logging.jar2 - java.lang.ClassNotFoundException: javax.transaction.Synchronization 添加包jta.jar(hiberante)3 - java.lang.ClassNo…

關于easyui的一些小知識點(1)

讓layout布局自動適應瀏覽器寬度只需要加上fit"true"屬性。轉載于:https://www.cnblogs.com/haifg/p/3613789.html

《MySQL——加鎖規則(待補全,有些沒看懂)》

catalog加鎖規則等值查詢間隙鎖非唯一索引等值鎖主鍵索引范圍鎖非唯一索引范圍鎖唯一索引范圍鎖 bug非唯一索引上存在"等值"的例子limit語句加鎖關于死鎖總結 1、查詢過程中訪問到的對象才會加鎖&#xff0c;而加鎖的基本單位是next-key lock&#xff08;前開后閉&am…

c# 命名空間命名規范_C#中的命名空間

c# 命名空間命名規范C&#xff03;命名空間 (C# Namespace ) In C# namespaces are used to group similar type of classes. Two classes with same name in different namespaces never conflict to each other. 在C&#xff03;中&#xff0c;名稱空間用于對相似類型的類進…

PHP環境搭建:Windows 7下安裝配置PHP+Apache+Mysql環境教程

這兩天剛裝好Windows 7&#xff0c;碰巧前段時間有朋友問我Windows下如何安裝搭建PHP環境&#xff0c;所以打算勤勞下&#xff0c;手動一步步搭建PHP環境&#xff0c;暫且不使用PHP環境搭建軟件了&#xff0c;在此詳細圖解在Windows 7下安裝配置PHPApacheMysql環境的教程&#…

《MySQL—— 業務高峰期的性能問題的緊急處理的手段 》

catalog短連接風暴先處理占著連接但是不工作地線程減少連接過程的消耗慢查詢性能問題索引沒有設計好語句沒寫好選錯索引QPS突增問題短連接風暴 正常的短連接&#xff1a; 執行很少sql語句就斷開&#xff0c;下次需要的時候再重連。MySQL建立連接的過程成本很高&#xff0c;包含…

sql 算出下級銷售總和_找出總和字符串

sql 算出下級銷售總和Description: 描述&#xff1a; This is a standard interview problem to check that the given string is a sum string or not using backtracking. 這是一個標準的面試問題&#xff0c;用于檢查給定的字符串是否為總和字符串或不使用回溯。 Problem…

Request 分別獲取具有相同 name 屬性表單元素值

html 中是允許多個具有相同name屬性的元素的&#xff0c;例如 <div> <input name"txtName" id"txtFirstName" type"text" /> <input name"txtName" id"txtMiddleName" type"text" /> <input…

《MySQL——redo log 與 binlog 寫入機制》

目錄binlog寫入機制redo log寫入機制組提交機制實現大量的TPS理解WAL機制如何提升IO性能瓶頸WAL機制告訴我們&#xff1a;只要redo log與binlog保證持久化到磁盤里&#xff0c;就能確保MySQL異常重啟后&#xff0c;數據可以恢復。 下面主要記錄一下MySQL寫入binlog和redo log的…

BBIAB的完整形式是什么?

BBIAB&#xff1a;再回來一點 (BBIAB: Be Back In A Bit) BBIAB is an abbreviation of "Be Back In A Bit". BBIAB是“ Be Back in A Bit”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites lik…