zzzzz

Extension Method: Return another string if string is null or empty
Just a tiny little extension method. You may know the ?? operator for null checks:
var something = objectThatCouldBeNull ?? anotherObject;
Basically if objectThatCouldBeNull is null, then anotherObject is used instead. Unfortunately, this does not work with empty strings, so here is a quick extension method for that:
public static string IfEmpty(this string input, string otherString)
{
    if (string.IsNullOrEmpty(input)) return otherString;
    return input;
}
call it like
var myString = someString.IfEmpty("someOtherString");
The nice thing on extension methods is that you can call them on null instances of an object, so a call to
((string)null).IfEmpty("SomethingElse");
is safe.

A simple Even/Odd Cycler for .net
If you want a table with different CSS Styles for even/odd rows, that is reasonably easy, usually you have code like this:
bool evenRow = true;
foreach (var item in someList)
{
  evenRow = !evenRow;
  SomeTable.Rows.Add(SomeFunctionThatReturnsATableRow(item,evenRow?"evenRow":"oddRow"));
}
So we have a bool that we switch on every iteration and then we derive the CSS Class Name from that. If you do that in one place, it‘s okay, but if you have to do that in many places, it can become quite a bit tedious. Sure, if you happen to use a for-loop you can just use "index % 2 == 0" to find out if you are on an even row, but I instead created a class:
public class EvenOddCycler
{
    private readonly string _oddClassName;
    private readonly string _evenClassName;
    private int _numCycles;

public EvenOddCycler() : this("evenRow","oddRow"){}

public EvenOddCycler(string evenClassName, string oddClassName)
    {
        _evenClassName = evenClassName;
        _oddClassName = oddClassName;
        _numCycles = 0;
    }

public string Cycle()
    {
        _numCycles++;
        return IsCurrentlyEven ? _evenClassName : _oddClassName;
    }

public void Reset()
    {
        _numCycles = 0;
    }

public bool IsCurrentlyEven
    {
        get { return (_numCycles % 2 == 0); }
    }
}
Really simple stuff here: The constructor takes two strings, one for even and for odd. It has a Cycle function that increases a counter and returns one of the two strings. Also included a Reset() function to re-use the cycler in case you have more than one table on a page.The usage looks like this:
var cycler = new EvenOddCycler();
foreach (var item in someList)
{
    SomeTable.Rows.Add(SomeFunctionThatReturnsATableRow(item,cycler.Cycle()));
}
What did we gain?•No need to constantly repeat the class names over and over again. You shouldn‘t hardcode them, but even if you put it in a Resource class of some kind, you still have to have the boolean switch somewhere to get the correct class name. No need here anymore.
•No need to copy/paste this if you have multiple tables on a Page. Just call cycler.Reset()
Granted. it‘s a small thing, but every little thing I don‘t have to worry about is good. This is not Thread-safe because I see no scenario where you would share one cycler. Making it Thread-safe is a simple matter of adding a lock object and locking all three method bodies though.

时间: 2025-01-06 06:25:41

zzzzz的相关文章

zzzzz 排序总结

一.冒泡排序 基本思想是:两两比较相邻记录的关键字,如果反序则交换 冒泡排序时间复杂度最好的情况为O(n),最坏的情况是O(n^2) 改进思路1:设置标志位,明显如果有一趟没有发生交换(flag = false),说明排序已经完成 改进思路2:记录一轮下来标记的最后位置,下次从头部遍历到这个位置就Ok 二.直接插入排序 将一个记录插入到已经排好序的有序表中, 从而得到一个新的,记录数增1的有序表 时间复杂度也为O(n^2), 比冒泡法和选择排序的性能要更好一些   三.简单选择排序 通过n-i次

HBase介绍(2)---数据存储结构

在本文中的HBase术语:基于列:column-oriented行:row列组:column families列:column单元:cell 理解HBase(一个开源的Google的BigTable实际应用)最大的困难是HBase的数据结构概念究竟是什么?首先HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库.另一个不同的是HBase基于列的而不是基于行的模式. Google's BigTable论文 清楚地解释了什么是BigTable:Bigtable是一个疏松的分布式的

Python开发【第xxx篇】函数练习题-----员工信息表

文件存储格式如下: id,name,age,phone,job 1,Alex,22,13651054608,IT 2,Egon,23,13304320533,Tearcher 3,nezha,25,1333235322,IT 现在需要对这个员工信息文件进行增删改查. 基础必做: a.可以进行查询,支持三种语法: select 列名1,列名2,- where 列名条件 支持:大于小于等于,还要支持模糊查找. 示例: select name,age where age>22   #> < s

oracle 函数 NVL2...NVL

NVL2(expr1,expr2,expr3) : 记忆,nvl2(),expr1为null ,2表示 选择第2个数(即expr3)返回 功能:如果参数表达式expr1值为NULL,则NVL2()函数返回参数表达式expr3的值:如果参数表达式expr1值不为NULL,则NVL2()函数返回参数表达式expr2的值. NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数都

SQL基础用法(实例二)

1 /* 2 3 4 2006年10月01日 5 6 SQL Server 数据库的高级操作 7 (1) 批处理 8 (2) 变量 9 (3) 逻辑控制 10 (4) 视图 11 (5) 函数 12 (6) 高级查询 13 14 */ 15 16 (1)批处理 17 将多条SQL语句作为一个整体去编译,生成一个执行计划,然后,执行! 18 理解批处理的关键在于"编译",对于由多条语句组成的一个批处理, 19 如果在编译时,其中,有一条出现语法错误,将会导致编译失败! 20 21 cre

Linux命令(11):rmdir命令

6.示例:删除空目录 1 2 3 4 5 6 [[email protected] zdw]# rmdir zzzzz          使用rmdir,后面接目录名称 [[email protected] zdw]# ll total 12 drwxr-xr-x 3 root root 4096 Apr  2 21:37 t drwxr-xr-x 2 root root 4096 Apr  2 21:26 test1 drwxr-xr-x 3 root root 4096 Apr  2 21:

C++长文(复习)

/* 文档较长  注意这分区 文档包括   哈希表平面点距 字典项目 排序等 */    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆  ★ │ 心想 │ 事成 │       ☆    ╭═╮      ★      ☆      ★      ☆    ╰═╯             ★☆★☆★☆★☆★☆★☆★☆★☆★☆★.                {@}           {@} * {@}        {@} * {@} * {@}            {@}* {@} * {@}

正则念念碎

正则表达式就原理来讲,只有一点点东东,就是一个状态机,只能用在上下文无关文法的环境. 但是它使用还是非常灵活的,那些厉害的,能够玩出花来,工作效率提高很多. 1.常见正则表达式符号 符号 描述 示例  literal 匹配文本字符串的字面值literal   foo  re1|re2 匹配表达式re1或者表达式re2  foo|bar  . 匹配除了(\n)之外的任何字符  b.b  ^ 匹配字符串起始的部分  ^Dear  $ 匹配字符串的结束部分  /bin/*sh$  * 匹配0次或者多次

谷歌技术&quot;三宝&quot;之BigTable

2006年的OSDI有两篇google的论文,分别 是BigTable和Chubby.Chubby是一个分布式锁服务,基于Paxos算法:BigTable是一个用于管理结构化数据的分布式存储系统, 构建在GFS.Chubby.SSTable等google技术之上.相当多的google应用使用了BigTable,比如Google Earth和Google Analytics,因此它和GFS.MapReduce并称为谷歌技术"三宝". 与GFS和MapReduce的论文相比,我觉得BigT