C#3.5语言 学习笔记

2.5.1隐含类型化的局部变量

var i = 5;

var string ="Hello World";

var Array=new int []{1,2,3};

注意:

声明注意的问题:

声明的同时要赋值

初始化语言必须是一个表达式,不能包含本身,但可包含对象或集合初始器的一个new表达式(即匿名类型)

 使用范围:

var的声明仅限于局部变量,也可以包含在foreach、for、using语句中

示例:

View Code
 1 var s1 = "My Name is xiaoxiao";//相当于string s1="My Name is xiaoxiao";
2 var s2 = 20;//相当于int s2=20;
3 var s3 = 1.0;//相当于double s3=1.0;
4 var s4 = new string[] { "xiaoxiao", "xiaofang", "xiaoming" };
5 //相当于string s4=new string[] { "xiaoxiao","xiaofang","xiaoming"};
6 Console.WriteLine(s1);
7 Console.WriteLine(s2);
8 Console.WriteLine(s3);
9 foreach (var ss in s4)
10 {
11 Console.WriteLine(ss);
12 }
13 Console.ReadLine();

扩展方法:

扩展方法是一种特殊的静态方法,定义在静态类中,但可在其他类的对象上调用实例方法那样调用。通过该方法在不修改类型的情况下对一个类型进行功能上的补充。

扩展方法和一般静态方法的定义方法类似唯一区别是在第一个参数前面要加上关键字this作为修饰符,同时第一个参数的类型也绝顶了扩展方法可以扩展的类型

扩展方法的基本语法:

       public  static 返回类型  扩展方法名(this 要扩展的类型 sourceObj){.........}

示例:

View Code
//定义被扩展的类:MyExt
class MyExt
{
int Age = 20;
public int myAge
{
set { Age = value; }
get { return Age; }
}
public void Write()
{
Console.WriteLine("Hi!I'm Ann,and my age is{0}", Age);
}
}
//定义用于扩展的类:myClass
static class myClass
{
public static void ExMyExt(this MyExt n)
{
Console.WriteLine("扩展MyExt类型");
n.myAge = 22;
n.Write();
}
}

class Program
{
static void Main(string[] args)
{
MyExt mx = new MyExt();
Console.WriteLine("这是静态方法!");
mx.Write();
Console.WriteLine();
Console.WriteLine("这是一个扩展方法");
mx.ExMyExt();
Console.ReadKey();
}
}

对象与集合初始化器

View Code
namespace Demo3
{
public class Book
{
public string BookName { get; set; }
public int Price { get; set; }
public string content { get; set; }
internal int Num;
}

class myObject
{
static void Main(string[] args)
{
//对象初始化器
Book book = new Book {BookName="C#",Price =56,content ="好看",Num=50 };
Console.WriteLine(book.Num);
Console.WriteLine(book.BookName );
Console.WriteLine();

//集合初始化器
List<Book> book1 = new List<Book>
{
new Book {BookName="C#",Price =56,content ="好看" },
new Book {BookName="ASP.NET",Price =41,content ="呵呵" },
new Book {BookName="J2EE",Price =54,content ="不错" },
//null,
};
foreach (var str in book1)
{
Console.WriteLine(str.BookName);
Console.WriteLine(str.Price);
Console.WriteLine(str.content);
Console.WriteLine();
}
Console.ReadKey();
}
}
}

运行结果:

匿名类型

View Code
namespace Demo4
{
class Program
{
static void Main(string[] args)
{
var s1 = new {Name ="ASP.NET 3.0高级编程",Price=59 };
var s2 = new { Name = "JavaScript特效", Price = 56 };
Console.WriteLine(s1.GetType());
Console.WriteLine(s2.GetType());
Console.ReadKey();
}
}
}

运行结果:

Lambda表达式(可显示或隐式定义)

 是一种简洁的创建匿名方法并最终简化.NET委托类型的使用方式

参数列表=>语句(语句集)

示例:

View Code
class Program
{
static int[] i = new int[] { 1, 2, 3, 4 };//参数定义
static void Main(string[] args)
{
Array.FindAll<int>(i, p => //使用Lambda表达式
{
Console.WriteLine(p);
return p % 2 == 0;
});
Console.ReadKey();
}
}

运行结果:




 

转载于:https://www.cnblogs.com/yilingjue/archive/2012/02/23/2365694.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值