C # study notes -- 1

Source: Internet
Author: User

Finalize method: Terminator.
Implementing the Final adapter will greatly reduce the program performance.
Release mode: [release of unmanaged resources]
Manually release the {dispose () method or close () method. True: release of unmanaged resources or release of managed resources. [our class contains other classes. Can I release these classes here? In general, managed resources are recycled by the recycle bin. ]} --- Block finalize calls.
The finalize () method. False unmanaged resource release)

Public class myresource: IDisposable
{
Private bool disposed = false;
Public void dispose ()
{

}
Public void close ()
{

}
~ Myresource ()
{

}
Private void dispose (bool disposing)
{
Sentence //
}
}

Class modifier:
Access Control modifier; [includes: default; public; private; internal; protected internal.]
Class mod
{
Void defaultMethod ();
{
Console. writeline ("");
}

}

// Call a method is first instantiated and then called:
Mod mod = new Mod (); // instantiate.
Mod. defaultMethod (); // call the process.

To make the two classes visible to each other, you can place the two classes under the same namespace.


Class modifier:
Sealed: class that cannot be inherited;
Partial: classes that can be declared in different files.


Type conversion:
Implicit conversion:
Explicit conversion:
Code :,
Class conversions
{
Static void main ()
{
Int a = 4; // system. int32
Long B; // system. int64
B = a; // implicit conversion
Console. writeline ();
}
}

You can initialize the two variables set above and then output them.
Explicit conversion is like this: a = (int) B;

Checked and unchecked operators and statements:
Add the checked operation server before the explicit conversion.

There is a sentence in the program:
Messagebox. show ("fashengyichu") can bring up a pair of photo frames.

There is also a feeling:
B = B + 100;
B + = 100;
At this time, the initial class B is of the byte type, so these two statements are different. The first sentence above is very error-prone during operation. The modification method is as follows: add the following to the expression on the right:
B = (byte) (B + 100 );
At this time, it will be consistent with the following sentence.
In the following sentence, the constants of the byte type are converted to the byte type by implicit conversion.

Conversion of reference types:
CLR allows forced conversion.

Fruit f = new Apple (); // directly indicates that the subclass can be forcibly converted into its parent class.
It should be emphasized that the parent class cannot be converted into any subclass.

To know the exact type of an object, call the gettype () method.

Type t = f. gettype ();//*********

Apple a = (Apple) f; // type conversion method.
You can also convert the objects in parameters in the output statement.

Use the is OPERATOR:
Console. writeline (f is fruit );
Console. writeline (f is apple );
Console. writeline (a is fruit );
Console. writeline (a is apple );
The running result is:
True;
False;
True;
True;
Here we know that when using the is operator, only the bool value is printed, that is, true and false.

The significance of as: for example, apple a = f as apple; // if f is compatible with apple, the returned value of a is true, and false. In this way, the following judgment statement uses the judgment Expression:! = NULL.

Foreach loop ??????


The controls attribute indicates that the returned value is a set.
Foreach (control c in this. controls)
{
Listbox1.items. add (c. name );
Control c1 = c as button;
If (c1! = Null)
{
C1.text = "kkkkkkkkkk ";
}
} // A simple example of forced type conversion.


Attribute:
Class user
{
Private string m_name;
Private string m_sex;
//
Public void setname (string values)
{Name = values ;}
Public string getname ()
{
Return name;
}
Public void setsex (string values)
{
If (values = "" | values = "")
{
Sex = values;
}
Else {
Console. writeline ("gender can only be" male "or" female "");
}
}
Public string getsex ()
{
Return sex;
}
Public name
{
Get {
Return name;
}
Set {
Name = values;
}
Public sex
{
Get {return sex}
Set {
[If judgment statement]
}
}
}
//
}
Class property
{
Static void main ()
{
User zs = new user ();
Zs. name = ("James ");
Zs. sex = ("male ");
Console. writeline ("name:" + zs. getname + "Gender:" + zs. getsex)
Zs. name = "James ";
Zs. sex = "male ";
Console. writeline ("name:" + zs. name + "Gender:" + zs. sex)
}

The property call is exactly the same as the call of the class field.
Static attribute: only static access is allowed.

The above dialog box is Using system. windows. forms;

The age attribute should generate a dynamic field based on the birthday.

Private static int m_logincount;
Public user ()
{M_logincount ++ ;}
Public static int logincount
{Get
{
Return m_logincount;
}
} // Read-only attribute.

Indexer:
Similar to attributes. The indexer is called the property with parameters.

Class arrclass
{
Private readonly string name;
Public arrclass (string name)
{
This. name = name;
}
Public string name
{
Get (return name ;)
}
}
Class test
{
Static void main ()
{
Arrclass [] a = new arrclass [10];
A [0] = new arrclass ("");
A [1] = new arrclass ("");
A [2] = new arrclass ("");
Console. writeline ("a [0] =" + a [0]. name );
Console. writeline ("a [1] =" + a [1]. name );
Console. writeline ("a [2] =" + a [2]. name );
}
}

To define an index, you must add this and brackets.

The indexer only needs to declare one instance, and the class needs to declare multiple instances.

Hash Table: [container]
Key is usually used for quick search.
Adding is used to add value to hashtable.

Code:
Class indexclass // a Class with an indexer.
{
Private hashtable name = new hashtable ();
Public string this [int index]
{
Get {return name [index]. tostring ();}
Set {name. add (index, value );}
}
}
Class test
{
Static void main ()
{
// Use the indexer.
Indexclass B = new indexclass ();
B [1, 100] = "zghan ";
B [1, 200] = "kkkkk ";
B [2, 300] = "ksldjflksdj ";
Console. writeline ("jjjj" + B [100]);
* ****** // Three words are omitted here!
}
}

Comparison between indexer and array:
Storage of class Arrays:
The indexer has get accessors and set accessors. There is no place to store variables.
The indexer can be overloaded,

Int [] a = new int [3]
A [0] = 1;
A [1] = 2;
A [2] = 3; // array storage.

Class arrclass
{}
......
Arrclass [] a = new arrclass [3];
A [0] = new arrclass ();
A [1] = new arrclass ();
A [2] = new arrclass ();
// Store class arrays.

The key role of this: The indexer can only use this.

Public int this [string aname]
{
Get {

}
Set {

}
}


The indexer cannot be declared as static.

Code example:
Using system;
Using systemcollections;
// Name. Course id. Score.
Class coursescore
{
Private string name;
Private int courseid;
Private int score;
Public coursescore (string name, int courseid, int score)
{
This. name = name;
This. couseid = couseid;
This. score = score;
}
Public string name
{
Get {}
Set {}
}
}
Class coursescoreindexer
{
Private arraylist arrcoursescore;
Public couresescoreindexer ()
{
Arrcoursescore = new arraylist ();
}
Public int this [string name, int courseid]
{
Get
{
Foreach (coursescore cs in arrcoursescore)
{
If (cs. name = name $ cs. courseid = courseid)
{
Return cs. score;
}
}
Return-1;
}
Set
{
Arrcoursescore. add (new coursescore (name, courseid, value ))
}
}

Public arraylist this [string name]
{
Get {}
}
}
Class Test
{
Static void main ()
{
Coursescoreindexer csi = new coursescoreindexer ();
Csi ["", 1] = 90;
Console. writeline (csi ["", 1]);
Console. wroteline ("chengjiwei :")
}
}

Now we will introduce the C # program language specification:

Bitarray class;
Can store any length.

The Left shift is multiplied by 2; the right shift is divided by 2.

The returned value of the indexer is the bool value, so the first sentence in implementation is:
Public bool this [int index]
{......}

> The symbol represents the number of digits that move to the right;
<Symbol indicates the number of digits that move to the left;

& Operator: When all values are 1, the final result is 1.

~ The operator in C # Is a bitwise complement operation. [0: 1; 0: 1 .]


How to Use bitarrary class:

This article is from the "Li Yuan Cao" blog

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.