c# 聲明類的時候初始化類_使用C#初始化的列表聲明

c# 聲明類的時候初始化類

The task is to create/declare a list with an initializer list in C#.

任務是在C#中使用初始化列表創建/聲明一個列表

C#清單 (C# List)

A list is used to represent the list of the objects, it is represented as List<T>, where T is the type of the list objects/elements.

列表用于表示對象的列表,它表示為List <T> ,其中T是列表對象/元素的類型。

A list is a class which comes under System.Collections.Generic package, so we have to include it first.

列表是System.Collections.Generic包下的一個類,因此我們必須首先包含它。

具有初始化的列表聲明 (List declaration with initialization)

To declare and initialize a list with the elements/objects, we use the following syntax,

要使用元素/對象聲明和初始化列表 ,我們使用以下語法,

    List<T> list_name= new List<T> {list_of_objects/elements};

Here, T is the type and list_name is the name of the list.

在這里, T是類型, list_name是列表的名稱。

Examples:

例子:

    //an integer list
List<int> int_list = new List<int> { 10, 20, 30, 40, 50 };
//a string list 
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};

Here, int_list is a list of integer elements and str_list is a list of string elements.

在這里, int_list是整數元素的列表, str_list是字符串元素的列表。

C#程序使用初始化來創建/聲明列表 (C# program to create/declare a list with initialization )

using System;
using System.Text;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//an integer list
List<int> int_list = new List<int> { 10, 20, 30, 40, 50 };
//a string list 
List<string> str_list = new List<string>{
"Manju", "Amit", "Abhi", "Radib", "Prem"
};
//printing list elements
Console.WriteLine("int_list elements...");
foreach (int item in int_list)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.WriteLine("str_list elements...");
foreach (string item in str_list)
{
Console.Write(item + " ");
}
Console.WriteLine();
//hit ENTER to exit
Console.ReadLine();
}
}
}

Output

輸出量

int_list elements...
10 20 30 40 50
str_list elements...
Manju Amit Abhi Radib Prem

翻譯自: https://www.includehelp.com/dot-net/list-declaration-with-initialization-in-c-sharp.aspx

c# 聲明類的時候初始化類

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

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

相關文章

編寫程序計算所輸日期是當年的第幾天

/* 1.輸入年月日&#xff0c;編寫程序計算所輸日期是當年的第幾天 *//* 2.已知列車隔日發車&#xff0c;且1/1/2006不發車(無ticket),如果所輸入數據在此日期之后&#xff0c;則輸出有沒有車票&#xff0c;否則僅輸出上一步結果。*/ /* month/date/year is which day of the ye…

匯編語言-005(XCHG、標志位操作、算術操作、比例因子的變址尋址、多個常用運算符運用、大端轉小端、數組操作)

1: 用不超過3條XCHG指令對4個8位寄存器的值重新排序&#xff0c;A,B,C,D調整為D,C,B,A .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data.code main PROCmov al,Amov bl,Bmov cl,Cmov dl,Dxchg al,dlxchg bl,clINVOKE ExitProcess,0 main ENDP E…

bcd碼二進制轉十進制_二進制編碼的十進制(BCD碼)及其加法

bcd碼二進制轉十進制Prerequisite: Number systems 先決條件&#xff1a; 數字系統 BCD Code (8421 Code): In BCD 8421 code, each decimal digit is represented using a 4-bit binary number. The 4-bit binary numbers have their weights attached as 8, 4, 2, 1 from MS…

SVN服務器部署

一、SVN版本控制器 Subversion就是一款實現版本控制的工具軟件&#xff0c;通常也成為版本控制器&#xff0c;簡稱SVN。 Subversion是Apache軟件基金會組織下的一個項目 SVN基本操作&#xff1a; checkout&#xff08;檢出&#xff09;&#xff1a;將一個服務端創建好的項目…

rtmp流\http流測試地址

測試方式&#xff1a;ffplay rtmp://58.200.131.2:1935/livetv/cctv1 rtmp&#xff1a; CCTV-1綜合:rtmp://58.200.131.2:1935/livetv/cctv1 CCTV-2財經:rtmp://58.200.131.2:1935/livetv/cctv2 CCTV-3綜藝:rtmp://58.200.131.2:1935/livetv/cctv3 CCTV-4中文國際:rtmp://58.2…

LINQ to XML:如何讀寫XCData

using System;using System.Xml.Linq;namespace ConsoleApplication1 {class Program{static void Main(string[] args){//寫入CDATA元素塊var doc new XElement("Test",new XElement("User",new XAttribute("name", "chenxizhang"),…

C#中的結構和類之間的區別

C&#xff03;類和結構 (C# class and structure) In C# and other programming languages, structure and classes are used to define a custom data type, that we can organize according to our need with different types of variables, methods etc. 在C&#xff03;和其…

[轉載]SQL?Plus?一些使用技巧

原文地址&#xff1a;SQL Plus 一些使用技巧作者&#xff1a;☆水『若寒Sql*plus的使用 Sql*plus介紹 Sql*plus是oracle提供的一個工具程序&#xff0c;既可以在oracle服務器使用&#xff0c;也可以在oracle客戶端使用。在windows下分兩種&#xff0c;sqlplus.exe是命令行程序&…

云服務器(Centos)部署SVN

1&#xff0c;安裝svn yum install subversion 2&#xff0c;查看版本號 svnserve --version 3&#xff0c;創建SVN版本庫&#xff08;在var/svn 文件夾下&#xff09; 新建文件夾 mkdir -p /var/svn/svnrepos 創建版本庫 svnadmin create /var/svn/svnrepos 4&#xff0c;修改…

ffmpeg命令提取像素格式

1&#xff1a; 提取yuv格式&#xff1a;不修改寬高 取3秒 ffmpeg -i test_1920x1080.mp4 -t 3 yuv420p_orig.yuv ffmpeg -i test_1920x1080.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv 可以使用ffplay播放&#xff1a;ffplay -video_size 1920x1080 yuv420p_orig.yuv 提取y…

Javascript(js)使用function定義構造函數

Javascript并不像Java、C#等語言那樣支持真正的類。但是在js中可以定義偽類。做到這一點的工具就是構造函數和原型對象。首先介紹js中的構造函數。 Javascript中創建對象的語法是在new運算符的后面跟著一個函數的調用。如 1 varobj newObject();2 vardate newDate();運算符new首…

錯誤:將字符串分配給C中的char變量| 常見的C程序錯誤

If you assign a string to the character variable, it may cause a warning or error (in some of the compilers) or segmentation fault error occurs. 如果將字符串分配給字符變量&#xff0c;則可能會導致警告或錯誤(在某些編譯器中)或發生分段錯誤。 Consider the code…

【轉】用BibTeX 寫 Reference

BibTeX 是一種格式和一個程序&#xff0c; 用于協調LaTeX的參考文獻處理&#xff0c;BibTeX 使用數據庫的的方式來管理參考文獻.&#xff0c;BibTeX 文件的后綴名為 .bib。 例子&#xff1a; article{name1, author {作者, 多個作者用 and 連接}, title {標題}, journal {期…

計算機二級C語言易混淆的區別

1&#xff0c;if(a1)與if(a1)的區別 首先&#xff0c;if(a1) 等價于 a1;if(a); 而a 1&#xff0c;是判斷a是不是為1&#xff1b; if(sq)里面的分為兩種情況&#xff0c;一種是sq為0&#xff0c;不執行if里面的代碼內容&#xff1b;另一種是sq不為0&#xff0c;執行里面的代碼內…

ffmpeg命令mp3中提取pcm格式

原mp3文件: ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm &#xff08;這可能是pcm原格式查不到什么信息但是可以播放的&#xff1a;ffplay -ar 48000 -ac 2 -f s16le 48000_2_s16le.pcm&#xff09; ffmpeg -i buweishui.mp3 -ar 48000 -ac 2 -samp…

C++ STL map的使用

1、map簡介 map是一類關聯式容器。它的特點是增加和刪除節點對迭代器的影響很小&#xff0c;除了那個操作節點&#xff0c;對其他的節點都沒有什么影響。對于迭代器來說&#xff0c;可以修改實值&#xff0c;而不能修改key。 2、map的功能 自動建立Key &#xff0d; value的…

bfs廣度優先搜索算法_圖的廣度優先搜索(BFS)

bfs廣度優先搜索算法What you will learn? 您將學到什么&#xff1f; How to implement Breath first search of a graph? 如何實現圖的呼吸優先搜索&#xff1f; Breadth First Search is a level-wise vertex traversal process. Like a tree all the graphs have verte…

考研C++必刷題(一)

【程序1】 題目&#xff1a;有1、2、3、4個數字&#xff0c;能組成多少個互不相同且無重復數字的三位數&#xff1f;都是多少&#xff1f; 解題思路&#xff1a; 利用三層循環&#xff0c;分別控制百位十位個位&#xff0c;若百位十位個位有重復的&#xff0c;則不輸出即可。 代…

關于計算機存儲單位?

關于計算機存儲單位&#xff1f; 計算機只能識別二進制。(1010100110. . . ) 1字節 8bit&#xff08;8比特&#xff09;–>1byte 8bit 1bit 就是一個 1 或 0 1KB 1024byte byte是[-128 ~ 127]&#xff0c;共可以標識256個不同的數字。 byte類型的最大值是怎么計算出來的…

ffmpeg 命令轉封裝

1&#xff1a; 改變編碼格式 原mp4文件:視頻是h264 音頻是aac 視頻轉成h265&#xff0c;音頻轉成mp3&#xff08;容器為mkv&#xff0c;有些容器不一定支持放h265的&#xff09; ffmpeg -i test_60s.mp4 -vcodec libx265 -acodec libmp3lame out_h265_mp3.mkv 播放&#xff1a…