- 浏览: 401272 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
Since the .net 4.0 there introduce the concept of object initializer and collection initializer. They are especially handy to cut the amount of code to write expiclity and delegate the work to Compiler.
Object Initializer
class Cat { public int Age { get; set; } public string Name { get; set; } } public class Program { public static void Main(string[] args) { Cat cat = new Cat { Age = 10, Name = "Fluffy" }; } }
In LINQ, where you can use the Object initializer with anonymous types, which alows you to do the projection and others quite easily.
var pet = new { Age = 10, Name = "Fluffy" }; // it is especially useful in LINQ eury expression
Collection Initializer
Collection initializer will allow you to initialize an array .e.g Cat[] or a List<Cat> quick easily. generally you can do
List<Cat> cats = new List<Cat> { new Cat{ Name = "Sylvester", Age=8 }, new Cat{ Name = "Whiskers", Age=2 }, new Cat{ Name = "Sasha", Age=14 } };
But if you like, you can use the more cannonical way of constructing collections with constructor parenthesis appended .
List<Cat> cats2 = new List<Cat>() { new Cat(){ Name = "Sylvester", Age=8 }, new Cat(){ Name = "Whiskers", Age=2 }, new Cat(){ Name = "Sasha", Age=14 } };
and if you are using an array, you can even omit the type in after the new keyword.
Cat[] cats3 = new []{ new Cat(){ Name = "Sylvester", Age=8 }, new Cat(){ Name = "Whiskers", Age=2 }, new Cat(){ Name = "Sasha", Age=14 } };
Below is the full code that I used to test the full Object initializer and Collection Initializer things.
namespace ObjectCollectionInitializer { // Demo the use of Object initalizer // where you have the form of // Cat cat = new Cat { Age = 10, Age = "Fluffy" } ; class Cat { public int Age { get; set; } public string Name { get; set; } } public class Program { public static void Main(string[] args) { Cat cat = new Cat { Age = 10, Name = "Fluffy" }; var pet = new { Age = 10, Name = "Fluffy" }; // it is especially useful in LINQ eury expression List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // and you can still keep the () operator List<int> digits2 = new List<int> { 0 + 1, 12 % 3, MakeInt() }; // Demo the use of Collection initializer // the collection initializer is a syntax sugar that allows you to // add a set of objects to a collection. // the basic form of a Collection Initializer is as follow. // List<Cat> cats = new List<Cat> { // new Cat { Age = 10, Name = "Fluffy" }, // ... // } // the same is true for object List<Cat> cats = new List<Cat> { new Cat{ Name = "Sylvester", Age=8 }, new Cat{ Name = "Whiskers", Age=2 }, new Cat{ Name = "Sasha", Age=14 } }; // or if you prefer, you can use the cannonical way (with () ) List<Cat> cats2 = new List<Cat>() { new Cat(){ Name = "Sylvester", Age=8 }, new Cat(){ Name = "Whiskers", Age=2 }, new Cat(){ Name = "Sasha", Age=14 } }; // if the collection is an array and // if it is the same type, you can omit the types as well Cat[] cats3 = new []{ new Cat(){ Name = "Sylvester", Age=8 }, new Cat(){ Name = "Whiskers", Age=2 }, new Cat(){ Name = "Sasha", Age=14 } }; } public static int MakeInt() { return 0; } } }
References:
you can refer "Object and Collection Initializer (C# Programming Guide)"
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1979first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1929Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6555Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12054标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1253DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2576I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10955In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1296If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 943When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 819I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1926You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5337As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1191Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1717P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6100NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1227It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1099WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 870In WCF, primitives type are s ...
相关推荐
在标题"Lazy.Object.Initialize"中,我们关注的是`Lazy<T>`类在初始化对象时的应用。 `Lazy<T>`类在C#中的使用主要是为了优化性能。它有两个关键特性:线程安全和可控制的初始化。下面将详细介绍这两个方面: 1. *...
### 浅析C# 中object sender与EventArgs e #### 一、C# 预定义事件处理机制概览 在深入了解C#中的`object sender`与`EventArgs e`之前,我们首先需要理解.NET框架中与事件相关的类和委托的基础概念。 在C#中,...
1. Person#initialize使用名称Person#initialize 在lib/person.rb中定义一个Person类, lib/person.rb提供一个#initialize方法,该方法接受一个#initialize的参数。 该参数应存储在@name实例变量中。2. Dog#...
1. Person#initialize使用名称Person#initialize 在lib/person.rb中定义一个Person类, lib/person.rb提供一个#initialize方法,该方法接受一个#initialize的参数。 该参数应存储在@name实例变量中。2. Dog#...
在C#编程环境中,WebBrowser控件是一个非常实用的组件,它允许开发者在应用程序中集成网页浏览的功能。本文将深入探讨`WebBrowser`控件的使用及其在C#中的应用,包括如何实现上一页、下一页、刷新和跳转到特定URL等...
在lib/person.rb中定义一个Person类, lib/person.rb提供一个#initialize方法,该方法接受一个#initialize的参数。 该参数应存储在@name实例变量中。 2. Dog#initialize使用名称和品种默认为“ Mutt” Dog#...
基恩士LK-GD500激光感测初始化设置和参数的录入
Person#initialize使用名称Person#initialize在lib/person.rb中定义一个Person类, lib/person.rb提供一个#initialize方法,该方法接受一个#initialize的参数。该参数应存储在@name实例变量中。 2. Dog#initialize...
接着,调用Cef.Initialize方法启动CEF,并创建CefBrowser实例,将其添加到WinForms控件中。为了确保跨平台兼容性,CefGlue还支持.NET Core,使开发者能够在不同的操作系统上构建应用程序。 在提供的压缩包文件...
这将创建一个名为`laravel-initialize`的新目录,并在其中安装Laravel框架及其所有依赖。 **三、理解项目结构** 安装完成后,让我们快速浏览一下项目的基本目录结构: - `app/`:包含你的应用程序代码,如控制器...
在C#编程环境中,开发一个窗体应用来循环显示多张图片是一项常见的任务,尤其在创建用户界面或者制作多媒体展示时。本知识点将详细介绍如何利用C# WinForms技术实现这一功能,包括窗体设计、图像处理和定时器控件的...
- `InitializeComponent()`方法:自动为窗体及其上的控件设置属性和事件处理程序。 - 构造函数:如`Form1()`,通常用于初始化窗体。 7. **运行和调试**: - 点击工具栏上的绿色三角形按钮(运行按钮)可启动应用...
Clone the mupen64plus-ae repository and initialize the working copy git clone https://github.com/mupen64plus-ae/mupen64plus-ae.git Open the project using Android Studio Build and run the app from ...
Ajax-jquery.initialize.zip,用于动态创建元素初始化的jquery插件(几年前很好,在2019年 考虑react或其他东西而不是jquery),ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和...
和声2 无人维护 模式对象 旨在对 Javascript 对象强制执行架构。 允许您通过一组属性指定值的类型、转换和验证。...Initialize instance of user var user = new User ( { firstName : 'Scott' , lastName : 'Hovestadt
派生类应覆盖initialize和finalize方法。变化V2.1.x 将可状态能力添加到任何类中。 将RefObject移至 完全解耦抽象对象。 所有零件均可单独使用。 状态=需求('抽象对象/能力') eventable = require('events-ex / ...
在C#编程中,动态添加或删除窗体中的控件是一项常见的需求,特别是在设计具有高度交互性和自定义功能的应用程序时。通过操作程序窗体的`Controls`属性,我们可以实现这个功能,使得用户界面(UI)可以根据需要进行...
NX二次开发UF_ASSEM_initialize_sequencing_keep_layout 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE ...
NX二次开发UF_DRF_initialize_custom_symbol_text_data 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE ...
NX二次开发UF_DRF_initialize_leader_data 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的...