.net杂记

系统 1439 0
C#中要使用foreach需要实现什么接口或类
悬赏分:10 - 解决时间:2007-12-10 16:07
<cd>C#中要使用foreach需要实现什么接口或类</cd>
问题补充: 2) 能用foreach遍历访问的对象需要实现 ______________接口或声明______________方法的类型。
这个题目该怎样答?
最佳答案
<ca>能用foreach遍历访问的对象必须是集合或数组对象,而这些都是靠实现超级接口IEnumerator或被声明 GetEnumerator 方法的类型。</ca>
 
Array . . :: . Clone Method <!---->

A shallow copy of an Array copies only the elements of the Array , whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.

In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

The clone is of the same Type as the original Array .

This method is an O( n ) operation, where n is Length .

 

 

Array . . :: . CopyTo Method <!---->

<!--content type: Devdiv1. Transform: orcas2mtps.xslt.-->

<!---->

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array .

<!---->   Name Description
<!--src=[../icons/pubmethod.gif]--> Public method <!--src=[../icons/cfw.gif]--> Supported by the .NET Compact Framework <!--src=[../icons/xna.gif]--> Supported by the XNA Framework CopyTo(Array, Int32) Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 32-bit integer.
<!--src=[../icons/pubmethod.gif]--> Public method CopyTo(Array, Int64) Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index. The index is specified as a 64-bit integer.

 

using System ;

public class JaggedArray
{
    public static void Main ( )
    {
       // 声明一个锯齿型数组,该数组有两个元素
       int [ ] [ ] myArray = new int [ 2 ] [ ] ;

       // 其中第一个元素是一个含有五个元素的数组
       // 初始化myArray[0]
       myArray [ 0 ] = new int [ 5 ] { 1 , 3 , 5 , 7 , 9 } ;

       
       // 其中第二个元素是一个含有4个元素的数组
       // 初始化myArray[1]
       myArray [ 1 ] = new int [ 4 ] { 0 , 2 , 4 , 6 } ;

       // 逐一打印出数组的数组中所有的元素
       for ( int i = 0 ; i < myArray . Length ; i + + )
       {
           Console . Write ( "第({0})个数组: " , i ) ;

           // 打印一维数组中的元素
           for ( int j = 0 ; j < myArray [ i ] . Length ; j + + )
           {
               Console . Write ( "{0} " , myArray [ i ] [ j ] ) ;
           }

           Console . WriteLine ( ) ;
        
    }     Console . Read ( ) ;
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace toshiba_test
{
 enum Days{Mon=1,Tue=6,Wen=9,Thu,Fri,Sat,Sun}
 class Program
 {
  static void Main(string[] args)
  {
   Days d = Days.Fri;
   Console.WriteLine(d);
   Console.WriteLine(d.ToString());
   Console.WriteLine(d.ToString("D"));
   Console.WriteLine(d.ToString("G"));
   Console.WriteLine(d.ToString("X"));
  }
 }
}

 

 

GC.Collect 方法<!---->

<!--content type: DocStudio. Transform: devdiv2mtps.xslt.-->

强制进行垃圾回收。
 

 class Program2
 {
  public static void Main(string[] args)
  {
   B b=new B();
   A a = b;
   a.Output();
  }
 }
 public class A
 {
  public A()
  {
   this.Output();
  }

  public virtual void Output()
  {
  }
 }
 public class B:A
 {
  private int y = 0, x = -1;
  public B()
  {
   y = -1;
   Console.WriteLine("y={0}",x);
  }
  public override void Output()
  {
   Console.WriteLine("x={0},y={1}",x,y);
  }

 }

.net杂记


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论