[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//声明数组. 第一种方法. 声明并分配元素大小.
int[] Myint = new int[30];
Myint[0] = 30;
Myint[1] = 50;
// 以此类推, 起始下标为0
//-------------------------------
// 声明数组,第二种方法, 声明并直接赋值,没有指定元素大小.
int[] Myint1 = { 20,10,50,65,18,90};
//------------------------------------------
//声明数组,第三种方法, 声明并分配大小,且赋值.
int[] i = new int[5] { 10, 20, 30, 40, 50 };
// -----------------------------------------------
// foreach循环遍历数组..
int[] Sum = new int[50];
Random rd = new Random();
// 先用for循环给数组取随机数.
for (int s = 0; s <= Sum.Length - 1; s++) // Sum.Length是数组的一个属性,Length代表数组的长度
{
Sum = rd.Next(100);
}
// 遍历数组输出
foreach (int t in Sum)
{
Console.WriteLine(t);
}
}
}
}
.equals("要查找的元素"))
{....}
}
其实在C# 2.0对List,Array元素的查找,MS已经提供了一些泛型方法,让Coding人员更好的查找,遍历,等等...
以下是我简单对List的一些操作所写的Demo.供大家参考,以及和大家进行交流。
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
static void Main(string[] args)
{
//Prdicate用法
//第一种用法:[不创建显式的委托,也不创建指定泛型方法的类型参数]
ListOneMethod();
//Prdicate用法
//第二种用法:[创建显式的委托,也创建指定泛型方法的类型参数]
ListTwoMethod();
//第三种用法:[同于第二种方法,但用了两个类进行区分]
ListThreeMethod();
}
#region 第一种用法
private static void ListOneMethod()
{
String[] strs = { "WPF", "WCF", "WF", "Author", "WinFx", "Linq" };
String Name = Array.Find(strs, FindWhere);
Console.WriteLine("Result: ---------- " + Name + " ----------");
}
public static Boolean FindWhere(String str)
{
return str.Equals("Author") ? true : false;
}
#endregion
#region 第二种用法
private static void ListTwoMethod()
{
List<String> strlist = new List<String>();
strlist.Add("WPF");
strlist.Add("WCF");
strlist.Add("WF");
strlist.Add("Author");
strlist.Add("WinFx");
strlist.Add("Linq");
Predicate<String> FindValues = delegate(String list)
{
return list.Equals("WinFx") ? true : false;
};
Console.WriteLine("Result: ---FindIndex--- " + strlist.FindIndex(FindValues) + " ----------");
Console.WriteLine("Result: ---Exists---- " + strlist.Exists(FindValues) + " ----------");
List<String> lists = strlist.FindAll(FindValues);
foreach (string str in lists)
{
Console.WriteLine("Result: ---FindAll----- " + str + " ----------");
}
Console.WriteLine("Result: ---FindLast---- " + strlist.FindLast(FindValues) + " ----------");
Console.WriteLine("Result: ---FindLastIndex-- " + strlist.FindLastIndex(FindValues) + " ----------");
Console.WriteLine("Result: ---RemoveAll-- " + strlist.RemoveAll(FindValues) + " ----------");
Console.WriteLine("Result: ---TrueForAll- " + strlist.TrueForAll(FindValues) + " ----------");
}
#endregion
#region 第三种用法
private static void ListThreeMethod()
{
ListClass lists = new ListClass();
// 使用List.Add()方法來新增集合內容
lists.Values.Add(new ValueClass("WPF"));
lists.Values.Add(new ValueClass("WCF"));
lists.Values.Add(new ValueClass("WF"));
lists.Values.Add(new ValueClass("Author"));
lists.Values.Add(new ValueClass("WinFx"));
lists.Values.Add(new ValueClass("Linq"));
Predicate<ValueClass> FindValue = delegate(ValueClass obj) { return obj.Value == "Author"; };
Console.WriteLine("Result: ---------- " + lists.Values.FindIndex(FindValue) + " ----------");
Console.WriteLine("將所有資料列出");
int idx = 0;
Action<ValueClass> ListAll = delegate(ValueClass obj)
{
Console.WriteLine(string.Format("第 {0} 個的Value值為 {1}", idx, obj.Value));
idx++;
};
lists.Values.ForEach(ListAll);
}
public class ValueClass
{
private string _value = string.Empty;
public string Value
{
get { return _value; }
}
public ValueClass(string value)
{
_value = value;
}
}
public class ListClass
{
private List<ValueClass> _values = new List<ValueClass>();
public List<ValueClass> Values
{
get { return _values; }
}
public ListClass() { }
}
#endregion
*Predicate 是对方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该方法返回 true。当前 List 的元素被逐个传递给 Predicate 委托,在找到匹配项时停止处理。此方法执行线性搜索;
- 还有就是有关于List其他的一些查找方法:
- 1.当需要依条件来寻找集合内的某个类别时, 可用List<T>Find(), List<T>FindLast()来搜寻, 回传搜寻到的类别
- 2.当需要依条件来寻找集合内的某些类别时, 可用List<T>FindAll()来搜寻, 将回传一个新的List<T>对象集合
- 3.当需要依条件来寻找集合内的某个类别的索引值时, 可用List<T>FindIndex(), List<T>FindLastIndex()
- 4.List<T>Find(), List<T>FindLast()的不同是, List<T>Find()由Index=0开始寻找, 而List<T>FindLast()由Index = List<T>.Count - 1开始寻找
- 同理, List<T>FindIndex(), List<T>FindLastIndex()也是一样, 不同的是, 这两个回传的是索引值
- 5.当使用List<T>Find()相关函示时, 必须delegate.这个Predicate<T>
- 其内容就是搜寻的判断式, 如:
- Predicate<class1> FindJaofeng = delegate(class1 obj) {
- return obj.Value == "Jaofeng";
- };
- return type为boolean值
- 而上面也有介绍一个List<T>.ForEach(), 这个Method只是将原本我们用foreach()的方式, 简化而已
- 譬如原本的习惯写法:
- foreach (class1 cls in myText.Values) {
- // Do something
- }
- // 现在变成
- Action<class1> ActionName = delegate(class1 obj) {
- // Do something
- };
- myText.Values.ForEach(ActionName);
-
- 查找DropDownList中的Item的
- ListItemCollection items = DisplayModeDropdown.Items;
- 查找 Index:
- int selectedIndex = items.IndexOf(items.FindByText("需要查找匹配的item"));
- 查找 Value:
- string selectedValue = items.FindByText("需要查找匹配的item");
复制代码
作者: 李维强-15级 时间: 2016-11-6 16:39
C# Lambda表达式
http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html
作者: 李维强-15级 时间: 2016-12-23 15:09
C#3.0新特性 匿名类
http://www.cnblogs.com/joey0210/archive/2012/10/25/2739017.html
作者: 李维强-15级 时间: 2017-6-16 15:48
线程相关:
在睡眠的线程如何及时唤醒并退出!http://www.cnblogs.com/theLife/p/6569279.html
作者: 李维强-15级 时间: 2017-6-17 21:25
本帖最后由 李维强-15级 于 2017-6-17 21:38 编辑
申明全局变量
C#里面定义全局变量是在以下两种方式任选其一。
1:类里面申明static来完成
2:单条件模式(单例)
http://blog.csdn.net/sven_xu/article/details/46324023
作者: 李维强-15级 时间: 2017-6-25 15:19
String字符串处理
http://blog.csdn.net/wangshubo1989/article/details/46905881
作者: 李维强-15级 时间: 2017-12-6 20:07
本帖最后由 李维强-15级 于 2017-12-10 22:28 编辑
类 部分的概念
类和对象的区别:
类只是一个模板,模子,对象是通过类实例化过后的东西,也就是分配了内存了的,通过构造函数等初始化过后了的东西。
const 和readonly有区别:
readonly可以被赋值,但是只能在构造函数里面赋值,是实例过后才有的东西,其他地方不能更改了,这就是为什么每个实例的readonly字段可以不同,但是也不能被修改的原因。
而const在类里面是共享的,静态的,不需要实例化就有的东西,而且不能被再次赋值,只能用,通常装一些固定的值,如PI,E等参数。
派生
只要是派生类,只要实例化了过后,都会在基类的基础上再分配更多的内存,派生类保留了基类的东西,内存更大。只是相应的virtual override等修饰会有不同的调用规则
virtual修饰符
虚属性或虚方法,但是方法一旦被实现重写,且实例化了过后,这么方法就一直存在了,即使从派生类转换到基类,那么在基类里面,也是被派生类的方法重写,实际上用的就是派生类的方法。《C#本质论第4版,6.2.1》
new和override的区别和联系
讨论的情况是他们都在派生类里面的,如果基类是虚函数,那么在派生类函数前面加new 或者override都是隐藏了基类的方法,但是都可以用base.xxx访问得到基类的方法。但是如果派生类赋值给基类,那么在加new的情况下,基类的方法还是基类的,那么在加override的情况下,基类的方法会被重写为派生类的方法,当然同样可以在派生类里面使用base.XXX访问到基类的原来方法。总结,也就是赋值过后有区别,不然都一样。
任何加上override的方法都会被自动成为虚方法,也就是默认加上个virtual。
构造函数
默认构造函数是不带参数的,任何派生类的构造函数都是先要调用其父类的构造函数,再执行自己的构造函数,如果派生类有多级,那么编译器就是回溯到最基级调用构造函数,然后再一级一级的调用派生类的构造函数,最后完成初始化。
如果基类构造函数是带参数的情况,那么派生类是需要给基类构造函数传递参数的,用base(xxx xxx)这种形式
静态构造函数
静态构造函数,是在调用的时候才会被执行,如果在有实例化过程的情况下,同样是先执行基类的静态构造函数,再执行派生类的静态构造函数。
https://www.cnblogs.com/jiagoushi/p/3775046.html (这个帖子的第二个例子)
如果在没有实例化的情况下,直接使用一个类的情况下,静态构造函数只会被调用1次,而且是执行的基类的静态构造函数,https://www.cnblogs.com/jiagoushi/p/3775046.html (这个帖子的第一个例子)
多态性
一般是利用abstract关键词来在基类里面写个纯虚函数,然后再去给其他类继承,继承过后其他类就有且必须有相应的 abstract 实现方法了,就是继承类里面去override基类里面被abstract修饰了的东西。那么该基类会被多个其他类继承,那么基类里面被abstract修饰的东西会被不同的继承类里面override不同的实现。也就是说在不同的派生类里面,对于相同的方法名会有不同的实现。那么问题的关键就来了,当不同的继承类用“=”号,重新赋值给基类的时候,此时的那个基类的纯虚方法就有了不同的实现,这个时候,拿着这个基类,去调用那个纯虚方法,就有对应的继承类的实现方法了。这就是多态,
作者: 李维强-15级 时间: 2019-6-18 22:10
string 用法合集
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
// 为了满足20160302-003 的格式,下面先找出20160302有多少个审核单,然后再在那个基础上加1,最后得到审核单编号
sql = "";
sql = "select * from T_M_transFee where transFeeNum like '%" + DateTime.Now.ToString("yyyyMMdd") + "%'";
int n = BaseDao.execute(sql);
n++;
string transFeeNum = "YF"+DateTime.Now.ToString("yyyyMMdd") + "-" + n.ToString("000");
作者: 李维强-15级 时间: 2020-2-6 22:30
CSDN免费课程
https://edu.csdn.net/course/detail/5344
作者: 李维强-15级 时间: 2024-7-26 08:37
[C#] syntaxhighlighter_viewsource syntaxhighlighter_copycode
public class User
{
public string? Name { get; set; }
public string? Role { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
// Define a list of users
var users = new List<User>
{
new User { Name = "Alice", Role = "Admin" },
new User { Name = "Bob", Role = "Member" },
new User { Name = "Jay", Role = "Member" },
new User { Name = "Krishna", Role = "Admin" },
new User { Name = "An", Role = "Member" },
new User { Name = "Ka", Role = "Guest" },
};
// CountBy Role using GroupBy and Select
var roleCounts = users
.GroupBy(user => user.Role) // Group users by their roles
.Select(group => new { Role = group.Key, Count = group.Count() }); // Select the role and count for each group
// Print the results
foreach (var roleCount in roleCounts)
{
Console.WriteLine($"Role: {roleCount.Role}, Count: {roleCount.Count}");
}
}
}
欢迎光临 重工电子论坛 (http://cqutlab.cn/) |
Powered by Discuz! X3.1 |