惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Vercel News
Vercel News
Martin Fowler
Martin Fowler
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs

博客园 - 阿布

手Q兴趣号的价值在哪里 生命中绽放的花儿 Google发布App Engine 欲打造网络应用统一平台 NetBeans 7.0 揭密:用JavaScript重写,变成一个实足的在线IDE???!!! Rails存储库从SVN转向Git IKVM.NET,有点意思的Java虚拟机 Using Routing With WebForms 计划中的VS10特性将会作为VS2008的扩展而发布 使.NET运行在Linux, Mac OSX 和 FreeBSD(ouspec翻译) 使.NET运行在Linux, Mac OSX 和 FreeBSD(每个人都知道mono,但这里说的不是mono) 微软加入开源组织 微软发布 “Microsoft | NextWeb” 新展示站点 Google众高管跳槽至Facebook 基于Ubuntu的商业版 Linspire 6.0 发布! 教科书蒙蔽IT业很多年 世界第一台计算机名叫ABC Internet Explorer Administration Kit 7 RTM (IEAK)简体中文版 Vixta v0.94 - 基于Fedora Linux的Vista克隆版 微软到底为何取消IE7的正版验证 长江大学父子联手开发自由省略输入法
if ... else if ... else if ... else ... : an alternative
阿布 · 2008-03-11 · via 博客园 - 阿布

Like many tips & tricks concerning programming languages, what I will present here will be so utterly obvious to some C# developers, but could be an eye-opener to others.

How often do you write something like this?

        if (token == "A")
tokenNumber = 1;
else if (token == "B")
tokenNumber = 4;
else if (token == "C")
tokenNumber = 5;
else if (token == "X")
tokenNumber = 10;
else
tokenNumber = 20;


How about writing it like this?

      tokenNumber = (token == "A") ? 1:
(token == "B") ? 4:
(token == "C") ? 5:
(token == "X") ? 10:
20;

It's the same thing, but it looks cleaner, and the generated IL code is almost the same (it's even a bit shorter).

http://www.blog.activa.be/2008/03/11/ifElseIfElseIfElseAnAlternative.aspx