C# 学习笔记(2)

ref,  out的区别

Ref out都是是传递引用,out是返回值,两者有一定的相同之处,不过也有不同点。

使用ref前必须对变量赋值,out不用。

out的函数会清空变量,即使变量已经赋值也不行,退出函数时所有out引用的变量都要赋值,ref引用的可以修改,也可以不修改。

下面是使用outref进行数组修改的例子:

static void FillArray(out int[] arr)

{

        // Initialize the array:

        arr = new int[5] { 1, 2, 3, 4, 5 };

}

 

static void FillArrayRef(ref int[] arr)

{

        // Create the array on demand:

        if (arr == null)

        {

                arr = new int[10];

        }

        // Fill the array:

        arr[0] = 1111;

        arr[4] = 5555;

}

 

yield return 


Ref1   Ref2.
yield
C#2.0引入的新关键字,它主要是用来遍历函数返回的对象,其主要功能是在il代码中生成了状态信息,使用户不用自己维护遍历器的状态信息。下面是一个例子:每次调用GetInt()都会得到一个增加的数:

public static IEnumerable<int> GetInt()

{

   for (int i = 0; i < 5; i++)

       yield return i;

}

下面是调用:

class Program

{

    static void Main(string[] args)

    {

        foreach (int i in GetInt())

            Console.WriteLine("Got " + i.ToString());

    }

    public static IEnumerable<int> GetInt()

    {

        for (int i = 0; i < 5; i++)

            yield return i;

    }

}

yield通常用在实现IEnumerable接口的GetEnumerator()函数中。

class TestClass : IEnumerable<int>

{

    #region IEnumerable<int> Members

    public IEnumerator<int> GetEnumerator()

    {

        for (int i = 0; i < 5; i++)

            yield return i;

    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()

    {

        return GetEnumerator();

    }

    #endregion

}

 

C#中通过DllImport使用 Win32 API


        .NET框架程序可以通过静态 DLL 入口点的方式来访问本机代码库。DllImport 属性用于指定包含外部方法的实现的dll 位置。
DllImport
属性定义如下:

namespace System.Runtime.InteropServices

{

    [AttributeUsage(AttributeTargets.Method)]

    public class DllImportAttribute : System.Attribute

    {

        public DllImportAttribute(string dllName) {...}

        public CallingConvention CallingConvention;

        public CharSet CharSet;

        public string EntryPoint;

        public bool ExactSpelling;

        public bool PreserveSig;

        public bool SetLastError;

        public string Value { get {...} }

   }

}

说明:
1
DllImport只能放置在方法声明上。

2
DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。

3
DllImport具有五个命名参数:

       a
CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi

       b
CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto

       c
EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。

       d
ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false

       e
PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true

       f
SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false

4
、它是一次性属性类。

5
、此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。


下面是 C# 调用 Win32 MessageBox 函数的示例:
using System;

using System.Runtime.InteropServices;

class MainApp

{ //通过DllImport引用user32.dll类。MessageBox来自于user32.dll

    [DllImport("user32.dll", EntryPoint = "MessageBox")]

    public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);

    public static void Main()

    {

        MessageBox(0, "您好,这是PInvoke", ".net", 0);

    }

}

 

    面向对象的编程语言几乎都用到了抽象类这一概念,抽象类为实现抽象事物提供了更大的灵活性。

 

使用C#访问NTFS系统压缩文件


NTFS的系统压缩文件可以使用户无缝集成压缩功能,实现压缩的流式存储。 但是,.NET还不支持这一系统级的功能,可以通过Interops调用Native函数。下面是一段示例代码:
using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using Microsoft.Win32.SafeHandles;

using System.Runtime.InteropServices;

 

namespace CompressNTFSTest

{

    class Program

    {

        [DllImport("kernel32.dll")]

        public static extern int DeviceIoControl(SafeFileHandle hDevice, int

            dwIoControlCode, ref short lpInBuffer, int nInBufferSize, IntPtr

            lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr

            lpOverlapped);

 

        static void Main(string[] args)

        {

            string fileName = @"C:/Test.log";

            int lpBytesReturned = 0;

            int FSCTL_SET_COMPRESSION = 0x9C040;

            short COMPRESSION_FORMAT_DEFAULT = 0;

 

            FileStream f = File.Open(fileName, System.IO.FileMode.Open,

                System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);

 

            int result = DeviceIoControl(f.SafeFileHandle, FSCTL_SET_COMPRESSION,

                ref COMPRESSION_FORMAT_DEFAULT, 2 /*sizeof(short)*/, IntPtr.Zero, 0,

                ref lpBytesReturned, IntPtr.Zero);

        }

    }

}

 

范型(Generic

C#.NET2.0以后引入了范型的概念,可以创建带有类型的参数,而且这些类型在被实例化的时候才真正指定。这种范型的引入是有一定的性能代价的,在装箱/拆箱大型数据结构时会有比较明显的性能影响。

下面是一个例子

// Declare the generic class

public class GenericList<T>

{

    void Add(T input) { }

}

class TestGenericList

{

    private class ExampleClass { }

    static void Main()

    {

        // Declare a list of type int

        GenericList<int> list1 = new GenericList<int>();

 

        // Declare a list of type string

        GenericList<string> list2 = new GenericList<string>();

 

        // Declare a list of type ExampleClass

        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();

    }

}

 

 

StructLayoutAttribute

通常,CLR会自己控制管理类或者结构的物理存储结构,如果这些数据类型需要以指定的方式进行存储,可以使用 StructLayoutAttribute. 按照指定的方式进行存储在进行非托管程序的编程时非常重要,比如,这些类有可能会被作为参数传递给非托管代码。LayoutKind 用来指定存储方式,其中 Sequential 强迫成员按照顺序进行存储,Explicit 可以明确指定每一个成员的存储方式,但使用 Explicit, 每一个成员必须使用FieldOffsetAttribute 标识该成员在此类型中的偏移位置(OffSet)。

例子:

[StructLayout(LayoutKind.Explicit, Size = 16, CharSet = CharSet.Ansi)]

public class MySystemTime

{

    [FieldOffset(0)]

    public ushort wYear;

    [FieldOffset(2)]

    public ushort wMonth;

    [FieldOffset(4)]

    public ushort wDayOfWeek;

    [FieldOffset(6)]

    public ushort wDay;

    [FieldOffset(8)]

    public ushort wHour;

    [FieldOffset(10)]

    public ushort wMinute;

    [FieldOffset(12)]

    public ushort wSecond;

    [FieldOffset(14)]

    public ushort wMilliseconds;

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值