(4) C # "pseudo class" and array

Source: Internet
Author: User

We all know that Java is a single-inheritance language, want to inherit multiple classes, it depends on the interface to implement. The same is true of C #, where numerous interfaces provide great convenience for programming. It is also a good choice to define your own interface. It is impossible for a son in life to inherit the property of many fathers, the heavens cannot drop the Pies, the interface can provide to each class only a function of the empty shell, all the methods must be implemented by the class itself. Everyone is fair and does not specialize. Simple interface Implementation:

The complete code is as follows:

Namespace ConsoleApplication4

{

public interface Ifirst

{

void Readblog ();

}

Interface Isecond

{

void Readblog2 ();

}

Class Program:ifirst, Isecond

{

public void Readblog () {

Console.WriteLine ("First interface is implemented");

}

public void Readblog2 ()

{

Console.WriteLine ("A second interface is implemented");

}

static void Main (string[] args)

{

Program A = new program ();

A.readblog ();

A.READBLOG2 ();

}

}

}

Some students have to ask, if two interfaces have the same name function, but the function is doing different things to do? C # provides two methods for implicit definition and display definition to solve this problem:

Namespace ConsoleApplication4

{

public interface Ifirst

{

void Readblog ();

}

public interface Isecond

{

void Readblog ();

}

Class Program:ifirst, Isecond

{

Implicitly defined

public void Readblog () {

Console.WriteLine ("First interface is implemented");

}

Display definition

void Isecond.readblog ()

{

Console.WriteLine ("A second interface is implemented");

}

static void Main (string[] args)

{

Program A = new program ();

A.readblog ();

Isecond B = A;

B.readblog (); Call the Readblog () of the display definition

}

}

}

From the above example, is the interface more characteristic of a class? The language designer's intentions never adhere to a style, since there are multiple inheritance, then make an interface concept, more flexible than inheriting the parent class.

There is also a very vivid image of the keyword, called where

Where to use

The WHERE clause is used to specify type constraints that can be used as variables for type parameters defined in a generic declaration.
1. Interface constraints.
For example, you can declare a generic class mygenericclass, so that the type parameter T can implement the Icomparable<t> interface:

public class mygenericclass<t> where t:icomparable {}


2. Base class constraint: Indicates that a type must use the specified class as the base class (or the class itself) to be used as a type parameter for the generic type.
Once such a constraint is used, it must appear before all other constraints on that type parameter.

Class Myclassy<t, u>
where T:class
where u:struct
{
Have you heard of irregular arrays? C # can be achieved, for this is very difficult to see things, C # has its own way.

Namespace ConsoleApplication4

{

Class Program

{

public void Displayvalue (params int[] values)

{

foreach (int i in values)

{

Console.WriteLine ("Displayvalues {0}", i);

}

}

static void Main (string[] args)

{

Program A = new program ();

A.displayvalue (3, 4, 5, 8, 9);

Int[][] Jagarray;

Jagarray = new int[3][] {new int[4], new int[6],new int[8]};

Jagarray[0][3] = 34;

JAGARRAY[2][2] = 54;

}

}

}

There's a keyword params in it, and smart readers can think of what it is? If you are anxious to know the answer, you can comment below the blog.

In addition, there is a way to directly define an array, and also to determine the upper and lower bounds of the array.

public class SAB

{

public static void Creatarraywithbounds () {

int []lengthsarray = new int[2]{3,5};

int []boundsarray = new int [2]{2,3};

Array mutidimension = Array.CreateInstance (typeof (String), Lengthsarray,boundsarray);

Console.WriteLine ("Bounds:\tlower\tupper");

for (int i=0;i<mutidimension.rank;i++)

{

Console.Write ("Rank: {0}\t", I);

Console.Write (Mutidimension.getlowerbound (i) + "T");

Console.Write (Mutidimension.getupperbound (i) + "\ n");

}

}static void Main ()

{

Sab. Creatarraywithbounds ();

}

}

The concept of dimension here is important, in this case, the dimension is the row and column. Rank refers to the dimension of the array, which indicates an array of bounds, which greatly reduces the risk of crossing over.

(4) C # "pseudo class" and array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.