.NET的值类型和引用类型

先看下微软官方介绍:

如果数据类型在它自己的内存分配中存储数据,则该数据类型就是“值类型”“引用类型”包含指向存储数据的其他内存位置的指针。

值类型包括:

  • 所有数字数据类型

  • BooleanCharDate

  • 所有结构,即使其成员是引用类型

  • 枚举,因为其基础类型总是 SByteShortIntegerLongByteUShortUIntegerULong

引用类型包括:

  • String

  • 所有数组,即使其元素是值类型

  • 类类型,如 Form

  • 委托

以下编程元素未限定为类型,因为您无法将它们中的任何一个指定为声明元素的数据类型:

  • 命名空间

  • 模块

  • 事件

  • 属性和过程

  • 变量、常数和字段

如果如下这样是值类型;

int i = 10;

int j = i;

i++;

Console.Write(i);

Console.Write(j);

i的值是11,j的值是10;

如下这样DataTable传递是引用类型:

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtOriginalData"></param>
        /// <returns></returns>
        protected override DataTable TransferData(DataTable dtOriginalData)
        {
            DataTable dtFinalData = null;
            try
            {

                DataTable dtFolderData = base.ExcuteOracleReader(GetSqlQueryGetFolder);
                dtQARelate = base.ExcuteOracleReader(GetQARelateSql);               
                if (dtOriginalData != null && dtOriginalData.Rows.Count > 0)
                {
                    dtFinalData = dtOriginalData.Copy();
                    AddGlossaryToDataTable(dtFinalData);
                    AddReferencesToDataTable(dtFinalData);                   
                    AddImageUrlToDataTable(dtFinalData);
                    TransforFolderData(dtFolderData, base.SiteUrl);
                    AddProductCatagory(dtFinalData, dtFolderData);
                    this.ChangeFieldName(dtFinalData);
                }
            }
            catch (Exception ex)
            {
            }
            return dtFinalData;
        }
        private void AddImageUrlToDataTable(DataTable dtFinalData)
        {
            DataTable dtImageUrl = base.ExcuteOracleReader(GetAnswerSummarySql);

            foreach (DataRow dr in dtFinalData.Rows)
            {
                if (dtImageUrl != null && dtImageUrl.Rows.Count > 0)
                {
                    string ID = dr["ID"].ToString();
                    DataRow[] newdrs = dtImageUrl.Select(string.Format("ID='{0}'", ID));
                    dr[this.ORACLE_FIELD_ANSWER_DETAIL] += GetImageUrl(newdrs);
                }
            }
            if (dtImageUrl != null && dtImageUrl.Rows.Count > 0)
            {
                dtImageUrl.Dispose();
                dtImageUrl.Clear();
            }
        }
        private string GetImageUrl(DataRow[] drs)
        {
            string strImageUrl = "";
            if (drs != null && drs.Count() > 0)
            {
                string NewUrl = "";
                string ImageUrl = "<src img=\"{0}\"/>";
                foreach (DataRow dr in drs)
                {
                    if (dr[this.ORACLE_FIELD_ANSWER_ImageURL] != null && !string.IsNullOrEmpty(dr[this.ORACLE_FIELD_ANSWER_ImageURL].ToString()))
                    {

                        NewUrl = dr[this.ORACLE_FIELD_ANSWER_ImageURL].ToString();

                        strImageUrl += "<br />" + string.Format(ImageUrl, NewUrl);
                    }
                }
            }
            return strImageUrl;
        }



        protected override List<SingleResultBE> ModifyQARelateAndRelateBy(List<SingleResultBE> newlistResult)
        {
            List<SingleResultBE> onelistResult = new List<SingleResultBE>();
 foreach (SingleResultBE entity in newlistResult) { if (!string.IsNullOrEmpty(entity.Id) && !string.IsNullOrEmpty(entity.ListItemId.ToString())) { DataRow[] newdrs = dtQARelate.Select(string.Format("ID='{0}'", entity.Id)); if (newdrs != null && newdrs.Count() > 0) entity.QARelate = GetQARelate(newdrs, newlistResult); DataRow[] otherdrs = dtQARelate.Select(string.Format("RELATEID='{0}'", entity.Id)); if (otherdrs != null && otherdrs.Count() > 0) entity.RelateBy = GetQARelateBy(otherdrs, newlistResult); onelistResult.Add(entity); } } if (dtQARelate != null && dtQARelate.Rows.Count > 0) { dtQARelate.Dispose(); dtQARelate.Clear(); } return onelistResult; } #endregion #region Private Methods private string GetQARelateBy(DataRow[] drs, List<SingleResultBE> newlistResult) { string strQARelateBy = ""; foreach (DataRow dr in drs) { string strQARelateID = ""; string strTitle = ""; if (dr[this.ORACLE_FIELD_ID] != null) strQARelateID = dr[this.ORACLE_FIELD_ID].ToString(); if (dr[this.ORACLE_FIELD_TITLE] != null) { strTitle = dr[this.ORACLE_FIELD_TITLE].ToString(); } List<SingleResultBE> needlistfolder = new List<SingleResultBE>(); if (!string.IsNullOrEmpty(strTitle) && !string.IsNullOrEmpty(strQARelateID)) needlistfolder = GetlistResult(newlistResult, strTitle, strQARelateID); if (needlistfolder != null && needlistfolder.Count > 0) { strQARelateBy += GetlistItemGuid(needlistfolder[0].ListItemGuid); } } return strQARelateBy; } private string GetQARelate(DataRow[] drs, List<SingleResultBE> newlistResult) { string strQARelate = ""; foreach (DataRow dr in drs) { string strQARelateID = ""; string strTitle = ""; if (dr[this.ORACLE_FIELD_QARELATEDID] != null) strQARelateID = dr[this.ORACLE_FIELD_QARELATEDID].ToString(); if (dr[this.ORACLE_FIELD_QARELATED_TITLE] != null) strTitle = dr[this.ORACLE_FIELD_QARELATED_TITLE].ToString(); List<SingleResultBE> needlistfolder = new List<SingleResultBE>(); if (!string.IsNullOrEmpty(strTitle) && !string.IsNullOrEmpty(strQARelateID)) needlistfolder = GetlistResult(newlistResult, strTitle, strQARelateID); if (needlistfolder != null && needlistfolder.Count > 0) { strQARelate += GetlistItemGuid(needlistfolder[0].ListItemGuid); } } return strQARelate; } private void AddProductCatagory(DataTable dtFinalData, DataTable dtFolderData) { dtFinalData.Columns[this.ORACLE_FIELD_PRODUCT_CATEGORY].MaxLength = 99999; foreach (DataRow dr in dtFinalData.Rows) { if (dtFolderData != null && dtFolderData.Rows.Count > 0) { string ID = dr["ID"].ToString(); DataRow[] newdrs = dtFolderData.Select(string.Format("ID='{0}'", ID)); dr[this.ORACLE_FIELD_PRODUCT_CATEGORY] = GetProductCatagory(newdrs, dtFolderData); } } } private string GetProductCatagory(DataRow[] drs, DataTable dtFolderData) { List<SingleResultBE> listResult = new List<SingleResultBE>(); listResult.AddRange(HavelistResult); string strProductCatagory = ""; if (drs != null && drs.Count() > 0) { foreach (DataRow dr in drs) { string foldername = ""; string folderID = "";
 string ID = "";
 if (dr[this.ORACLE_FIELD_FOLDERID] != null) folderID = dr[this.ORACLE_FIELD_FOLDERID].ToString(); DataRow[] drfolders = dtFolderData.Select(string.Format("FOLDERID='{0}'", folderID)); List<SingleResultBE> needlistfolder = new List<SingleResultBE>(); foreach (DataRow drf in drfolders) { List<SingleResultBE> Islistfolder = new List<SingleResultBE>(); foldername = drf[this.ORACLE_FIELD_FATHERTITLE].ToString(); ID = drf[this.ORACLE_FIELD_FOLDERID].ToString(); if (!string.IsNullOrEmpty(ID)) { Islistfolder = GetlistResult(listResult, foldername, ID); if (Islistfolder != null && Islistfolder.Count() > 0) needlistfolder.AddRange(Islistfolder); } if (needlistfolder.Count > 0) needlistfolder = needlistfolder.Distinct().ToList(); } if (needlistfolder != null && needlistfolder.Count > 0) { foreach (SingleResultBE entity in needlistfolder) { strProductCatagory += GetlistItemGuid(entity.ListItemGuid.ToString()); } } } } return strProductCatagory; } private string GetlistItemGuid(string itemguid) { if (!string.IsNullOrEmpty(itemguid)) { int startindex = itemguid.IndexOf(startwith) + 1; int endindex = itemguid.IndexOf(endwith); return itemguid.Substring(startindex, endindex - startindex).ToLower() + ";#"; } return null; } private List<SingleResultBE> GetlistResult(List<SingleResultBE> listResult, string title, string ID) { List<SingleResultBE> needlistfolder = new List<SingleResultBE>(); if (!string.IsNullOrEmpty(title)) { needlistfolder = (from entity in listResult where entity.Title == title && entity.Id == ID select entity).ToList(); } return needlistfolder; }

还有如下也是引用类型:

///新建一个类
 public class FolderBE
    {
       public string FolderOne
       { get; set; }
       public string FolderTwo
       { get; set; }
       public string FolderThree
       { get; set; }
       public string FolderFour
       { get; set; }
       public string FolderFive
       { get; set; }
    }

//然后:
         List<FolderBE> listFolder = new List<FolderBE>();

                    FolderBE folder = new FolderBE();
                    folder.FolderOne = "one";
                    folder.FolderTwo = "two";
                    folder.FolderThree = "three";

                    listFolder.Add(folder);

                    folder.FolderFour = "four";

  ///然后listFolder里的一条数据的FolderFour也会变成"four";

///   所以我们在使用它时要这样:

                     List<FolderBE> listFolder = new List<FolderBE>();

                    FolderBE folder = new FolderBE();
                    folder.FolderOne = "one";
                    folder.FolderTwo = "two";
                    folder.FolderThree = "three";

                    listFolder.Add(folder);

                  folder = new FolderBE();
                    folder.FolderOne = "one";
                    folder.FolderTwo = "two";
                    folder.FolderThree = "three";

                    folder.FolderFour = "four";

///listFolder里面的数据 FolderFour不会变成"four" 

先写到这里吧.

转载于:https://www.cnblogs.com/springyangwc/archive/2011/03/11/1980584.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页