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.mzph.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…