asp.net C # class type Code

Source: Internet
Author: User
Tags static class

A type is a custom type that consists of field data (member variables) and members (properties, methods, constructors, events, and so on) that manipulate field data. Where the field data represents the state of the class instance (object).

In C #, classes use the Class keyword definition, for example:
  
   
   
public class car
{
Field of car (status)
private int _speed;
private string _name;
Properties of the Car action field
public int Speed
{
set {This._speed=value;}
Get{return This._speed;}
}
public string Name
{
set {This._name=value;}
Get{return This._name;}
}
To explicitly define a default constructor
Public car () {}
Custom constructors
Public car (string name,int speed)
{
This._name=name;
This._speed=speed;
}
function of car (method)
public void Showstate ()
{Console.WriteLine ("Car {0} is going {1} mph", this._name,this. _speed);
}
Another: The fields of a class are rarely defined as public, and in order to protect the integrity of state data, it is best to define the field data as private (or protected) and then provide controlled access through the property.
Use New keyword to assign an object
The object must be allocated to memory using the New keyword, and if it is not new, then an attempt to use the class variable will receive a compilation error.
  
   
   
public static void Main (string[] args)
{
Error, forget to use new
Car C;
C.name= "Bruce";
}
The right example:
  
   
   
public static void Main (string[] args)
{
Creates a car object.
Car c;//declares a reference to a car object that has not yet been created.
C=new car ("Bruce Wong", 150)//A valid reference is assigned to an object through new, which points to a memory-valid object.
C.showstate ();
Console.readkey (TRUE);
}



Class constructors
Function: Assigns a value to the field (state) of an object, which allows the state to be created when the object is created.
Constructors are class-specific methods that are invoked indirectly when an object is created with the new keyword.
Note: The constructor does not return a value (even void), and its name is always the same as the name of the class.
Default constructor
C # provides a default constructor that you can redefine when needed, and the default constructor does not accept any arguments. It assigns new objects to memory and ensures that all fields are set to the correct default values. When you are dissatisfied with these defaults, you can redefine the default constructor. Such as:
  
   
   
Public car ()
{
This._name= "My Car";
this._speed=100;
}
Each time you use the new car (), the car object with the status _name= "My Car" _speed=100 will be created.
Custom constructors
Function: The state of an object can be initialized directly when the object is created.
  
   
   
Public car (string name,int speed)
{
This._name=name;
This._speed=speed;
}
Note: Once a custom function is defined, the default constructor that is brought is automatically removed from the class (the object cannot be created with the default constructor). If you want to create a class object using the default constructor, you must explicitly define the default constructor.
This the role of the key word
First, provide access to the current instance.
You can resolve the scope ambiguity that occurs when the name of an incoming parameter is the same as the name of the Type field. For example:
  
   
   
Class car
{
private string name;
public void SetName (string name)
{this.name=name;}
}
The field name,this that assigns the value of the parameter name to this object (instance) represents this instance.
Second, the parameter passes. Use this to make a concatenation constructor call
A class is designed using a technique called a constructor chain. This design pattern can be useful when a class defines more than one constructor.
Because constructors typically validate incoming arguments to enforce various business rules, redundant validation logic is often found in the constructors collection of classes.
  
   
   
Class car
{
public int speed{get;set;}
public string Name{get;set;}
Public car () {}
public car (int speed)
{
if (speed>150)
{speed=150;}
This.speed=speed;
}
Public car (string name)
{
This.name=name;
}
public car (int speed,string name)
{
if (speed>150)
{speed=150;}
This.speed=speed;
This.name=name;
}
}
In-line constructor scheme: Let a constructor that accepts the maximum number of parameters do the "primary constructor" and implements the necessary validation logic. The rest of the constructors use the This keyword to transfer the arguments to the primary constructor and provide other required parameters. In this way, we only care about the logic of the primary constructor, while the other constructor bodies are essentially empty.
  
   
   
Class car
{
public int speed{get;set;}
public string Name{get;set;}
Public car () {}
public car (int speed): This (Speed, "") {}
Public car (string name): This (0,name) {}

Primary constructor
public car (int speed,string name)
{
if (speed>150) {speed=150;}
This.speed=speed;
This.name=name;
}
}
Using the This keyword in tandem constructors simplifies programming tasks, and class definitions are easier to maintain and more concise. But it's not mandatory.
Sequence of execution of inline constructors:
1. Call the constructor to forward the parameter value supplied by the caller to the main constructor, and provide other necessary initialization parameter values.
2. Perform the main constructor.
3. Executes the logic that invokes the constructor body.
Third, custom indexer
  
   
   
Class Carcollection:ienumerable
{
Private ArrayList arcar=new ArrayList ();
Public Car This[int Index]
{
get{return (car) arcar[index];
Set{arcar.insert (Index,value);}
}
//...
}
Static keyword
C # classes (or structs) can use the static keyword to define many static members. These static members can only be invoked from the class level and not from the object level (you do not need to create instance objects when calling static members).
For example:
  
   
   
Error, WriteLine is a static member and is a class-level method.
Console c=new console ();
C.writeline ("Bruce Wong");

That's right! WriteLine is a class-level approach
Console.WriteLine ("Bruce Wong");
Attention:
A static member can only manipulate static data or invoke static members of a class. Non-static members can manipulate both instance data and static data (members), because static members are available to all instances of the class.
Second, the CLR allocates static data to memory only once, and changing static data affects all instances of this class.
Defining a static constructor
Constructors are used to set the data values of a class object when the class object is created. If you use instance-level constructors to assign values to static data, you'll be surprised to find that static data is reset every time a new class object is created. So we'd better use static constructors to initialize static data.
A static constructor is a special constructor that is very useful for initializing the values of static data that is unknown at compile time:
The de jure class (struct) can only define a static constructor.
Second, static constructors do not allow access to modifiers and cannot accept any parameters.
No matter how many class instances are created, static functions are known to execute once.
The CLR invokes the static constructor before the CLR creates the class instance or invokes the class static member for the first time.
Static constructors are executed before other constructors at the instance level.
Static class: A class is defined as static (decorated with the static keyword), you cannot use the New keyword to create a class instance, and a static class can only contain static class members or fields that are marked with static.
PS Tutorial: Project application objects, such as classes that define the main () method, are often defined as static classes to ensure that only static members are included and cannot be created directly. Such as:
 
   
   
Static class program
{
static void main (string[] args)
{
//...
}
}
Related Article

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.