C # Basics (vi) (enumerations, arrays, namespaces)

Source: Internet
Author: User

One, enumeration.

Enumerations are user-defined integer types. When declaring an enumeration type, you need to specify a set of acceptable instance values that the enumeration can contain.

Let's look at an example.

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, //年龄
      gread=2, //年级
      fatherAge=65 //父亲的年龄
     }
  }
}

In the example, 65 is output. The public enum user is the enumeration type that declares a user. The Age,gread,fatherage is its three options. Each is endowed with three specific values. int useraage= (int) user.fatherage; This defines a useraage and assigns it a value of user.fatherage, which is 65.

Let's look at a little sample.

static void Main()
    {
      user userTemp=user.gread;
      Console.WriteLine(userTemp.ToString());
     }

In this example, the output is the Gread string. Why, then? Because an enumeration is instantiated in the background as a structure derived from System.Enum, this means that it can be invoked to perform some action. The example above is a description.

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.