C + + programmers need to pay attention to some problems in learning C # (I.)

Source: Internet
Author: User
Tags foreach arrays functions inheritance multiple inheritance in c
C++| Program | programmers | questions

This article is about C + + programmers in learning C #, some of the issues to be noted.


C + + programmers need to pay attention to some problems in learning C # (I.)

1) using the interface (interface)
In C #, you use keyword interface to define interfaces, and you cannot use multiple inheritance in C #.

Interface icar//Interface ICar
{
int speed//Property Speed
{
Get
Set
}

void Run ();//interface method
void Stop ();
}

Class Mycar:icar//Inheritance interface
{
int _speed;

public int Speed
{
Get
{
return _speed;
}

Set
{
_speed = value;
}
}

public void Run ()
{
System.Console.WriteLine ("Mycar run, at speed {0}", _speed);
}

public void Stop ()
{
System.Console.WriteLine ("Mycar stop.");
}
}

2) using an array
The arrays in C # are allocated in the heap, so they are reference types. C # supports arrays of type 3: one-dimensional array (single dimensional), multidimensional array (multi dimensional) array (array of array).

int[] array = new INT[10]; single-dimensional array of int
for (int i = 0; i < array. Length; i++)
Array[i] = i;

int[,] array2 = new int[5,10]; 2-dimensional array of int
array2[1,2] = 5;

Int[,,] array3 = new int[5,10,5]; 3-dimensional array of int
array3[0,2,4] = 9;

int[][] Arrayofarray = new int[2]; Array of array of int
Arrayofarray[0] = new INT[4];
Arrayofarray[0] = new int[] {1,2,15};

3) using index (indexer)
An index makes it possible to access class data as if it were an array.

Class Indextest
{
Private int[] _a;

Public Indextest ()
{
_a = new INT[10];
}

public int This[int Index]
{
Get
{
return _a[index];
}

Set
{
_a[index] = value;
}
}
}

4) General parameter transfer

Class Passarg
{
public void Byvalue (int x)//by-value
{
x = 777;
}

public void byref (ref int x)//by-ref
{
x = 888;
}

public void Byout (out int x)//out
{
x = 999;
}
}

5) Passing Array parameters
Use the params keyword to pass an array parameter.

Class Arraypass
{
public void Func (params int[] array)
{
Console.WriteLine ("Number of elements {0}", array.) Length);
}
}

6 is and as
The obj is class is used to check whether object obj is an object or convertable that belongs to class classes.
The obj as class is used to check whether object obj is an object of class class or convertable, and if so, convert obj to class type.

Class IsAs
{
public void work (object obj)
{
if (obj is Mycar)
{
System.Console.WriteLine ("obj is Mycar object");

ICar ic = obj as ICar;
Ic. Speed = 999;
Ic. Run ();
Ic. Stop ();
}
Else
{
System.Console.WriteLine ("obj is not Mycar object");
}
}
}

7) foreach
int []arr = new INT[10];
foreach (int i in arr)
Console.WriteLine ("{0}", i);

8) Virtual and override
Subclasses overload the parent class virtual function, which requires the use of the override keyword.

Class BaseClass
{
public virtual void Speak ()
{
System.Console.WriteLine ("Hello, BaseClass.");
}
}

Class Son:baseclass
{
public override void Speak ()
{
System.Console.WriteLine ("Hello, Son.");
}
}

Class Grandson:baseclass
{
public override void Speak ()
{
System.Console.WriteLine ("Hello, grandson.");
}
}

9 using the keyword new to hide the function implementation of the parent class

Class Shape
{
public virtual void Draw ()
{
Console.WriteLine ("Shape.draw");
}
}

Class Rectangle:shape
{
Public new void Draw ()
{
Console.WriteLine ("Rectangle.draw");
}
}
Class Square:rectangle
{
Public new void Draw ()
{
Console.WriteLine ("Square.draw");
}
}

10 calling functions in the parent class
Use the keyword base to invoke functions in the parent class.

Class Father
{
public void Hello ()
{
System.Console.WriteLine ("hello!");
}
}

Class Child:father
{
Public new void Hello ()
{
Base. Hello ();
}
}




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.