C # BASICS (6) (enumeration, array, namespace)

Source: Internet
Author: User

I,Enumeration.

Enumeration is user-definedInteger type. When declaring an enumeration type, you must specify a group of acceptable instance values that can be included in the enumeration.

Let's look at an example.

  

 Code 
Using System;
Using System. windows;
Namespace Gosoa.com.cn
{
Class Myfirstclass
{
Static Void Main ()
{
Int Useraage = ( Int ) User. fatherage;
Console. writeline (useraage );
}

Public Enum User
{
Age = 18 , // Age
Gread = 2 , // Grade
Fatherage = 65 // Father's age
}
}
}

 

In the example 65. Public Enum user Is to declare User . Age, gread, fatherage Is its three options. Three specific values are assigned respectively. Int useraage = (INT) user. fatherage; This defines Useraage And assign the value User. fatherage , That is 65.

Let's take a look at a small example.

 
Code
Static VoidMain ()
{
User usertemp=User. gread;
Console. writeline (usertemp. tostring ());
}

 

in this example, the output is gread String. Why? Because enumeration is instantiated in the background as derived from system. enum structure, which indicates that you can call methods and perform some operations on them. The example above is a description.

II,Array.

First, declare an integer array.Int [] usercount;

Int []This indicates an integer array,UsercountIs the array name.

Let's look at another declaration method:Int [] usercount = new int [20];

This example declares20.

Note,All arrays are of reference type..

Array, we will briefly introduce this point here, and we will learn the array together with the set later.

III,Namespace.

Let's look at an example.

  

 Code 
Using System;
Namespace Gosoa.com.cn
{
Public Class Myfirstclass
{
Public String Geturl ()
{
Return " Gosoa.com.cn " ;
}

Static Void Main ()
{
Www.gosoa.com.cn. myfirstclass urlclassnew = New Www.gosoa.com.cn. myfirstclass ();
String URL = Urlclassnew. geturl ();
Myfirstclass myfirstclassnew = New Myfirstclass ();
String Url2 = Myfirstclassnew. geturl ();
Console. writeline (URL );
Console. writeline (url2 );
}
}
}

Namespace Www.gosoa.com.cn
{
Public Class Myfirstclass
{
Public String Geturl ()
{
Return " Www.gosoa.com.cn " ;
}
}
}

 

In this example, there are two Namespace . Note: Two Namespace The class names in are the same and all have Geturl () Method. But in the first Class Main () When we call these two methods, no error is reported, because the two classes are located in two different namespaces, thus avoiding the conflict of class names.

The output result is Www.gosoa.com.cn AndGosoa.com.cn

In this example, Gosoa. myfirstclass urlclassnew = new gosoa. myfirstclass (); Is used to instantiate Gosoa. myfirstclass This class. We need to use the struct class. Namespace + "." + Class Name In this way.

If the namespace name is very long, it will be very long, so we can useUsingStatement is simplified.

we can see that, the first sentence before each class is always using system; This is because all C # many classes are included in System namespace.

You can also create an alias for the namespace. For example, in the previous example, we can use

Using gosoa = www.gosoa.com.cn; To introduce Namespace.

 

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.