C # Basic notes (20th day)

Source: Internet
Author: User
Tags modifiers

1. Review
Properties: Protecting the field's
Constructors: Initializing objects
Initialize objects: Assign values to each property of an object
When will the constructor be called: when we are new
Two keywords to be aware of in an object-oriented
This 1. Represents an object of the current Class 2. Call your own constructor
New 1. Create an object 2. Hide members that inherit from the parent class
Base calls the parent class's constructor and calls the parent class's name method in the subclass

What is a class?
A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have
What is an object?
object is a concrete entity that you can see and touch-everything objects (things that exist)
The class is the stencil that creates the object of the stencil, abstract.
A class is a data type, a user-defined data type
Class composition: Fields, properties, methods, constructors, etc.
object is specific, is a concrete instance of the class, the object has attributes (characteristics) and methods (behavior)
A class contains data (represented by a field) and behavior (represented by a method (function, function) as a piece of code with a name)

2. Access modifiers
Publicly public.
Private, can only be accessed within the current class, members of the class, if you do not add access modifiers, the default is private
Procteced is protected, can be accessed within the current class, or can be accessed in subclasses of that class.
Internal can be accessed in the current project.
procteced internal can be accessed in both the current project and the subclass of the modified class
There are only two access modifiers that can modify a class, internal and public

3. Commonly used Keywords
This
1. Objects representing the current class
2. Call your own constructor
Base
Calling a member of the parent class
New
1. Create an Object
2. Hiding members of a parent class
Virtual
Marking a method is a virtual method
Abstract
of abstract
Override
Rewrite
Interface
Interface
Partial
Partial classes
Sealed
Sealed class: Not allowed to inherit, but can inherit others
Return
1. Return the value you want to return in the method
2. End this method immediately
Break
Jump out of the current loop
Continue
End this cycle and return to the loop condition to judge
Static
of static
struct
Structure
Enum
Enumeration
Const
Constant

4. Virtual method, abstract method
Some points to note about virtual methods:
1. If there are methods in the parent class that need to be overridden by subclasses, you can mark the method as virtual
2. The virtual method must have an implementation in the parent class, even if it is an empty implementation.
3. Virtual method subclasses can be overridden (override), or they can not be overridden
Some points to note about the punching method:
1. You need to mark with the Abstracy keyword.
2. Abstract methods cannot be implemented in any way.
3. Abstract members must be included in the abstract class.
4. Because the abstract member does not have any implementations, the subclass must override the abstract member.
5. Abstract classes cannot be instantiated,
The role of abstract classes: the function of abstract classes is to allow subclasses to inherit.
6. Abstract classes can include abstract members, which can include members with specific code.
7. There are also abstract methods that cannot be modified with static

5. Interface
1. The interface can only contain methods (properties, time, indexers are also methods)
2. None of the members in the interface can have any implementations. Say no to do
3. Interfaces cannot be instantiated
4. Members in an interface cannot have any access modifiers. (Default to public)
5. Subclasses that implement an interface must implement all the members of the interface.
6. Subclasses implement the method of the interface, do not need any keyword, directly implemented.
7. The meaning of the interface is to be polymorphic.

What is an interface? When do I use the interface? What is the purpose of using an interface? Polymorphic
A bird can fly, a plane can fly.
Can not be extracted from the fly as a parent class, can only fly into an interface
public interface Iflyable
{
void Fly ();
}

public class Bird
{

}
public class Qq:bird
{

}
public class Maque:bird, iflyable
{
public void Fly ()
{
Console.WriteLine ("Bird will Fly");
}
}

public class Plane:iflyable
{
public void Fly ()
{
Console.WriteLine ("Aircraft will Fly");
}
}


Iflyable fly = new Maque ();
Fly.fly ();
Console.readkey ();

Inherit, first write the parent class, then write the interface

Property
constructor function
Overloaded, polymorphic

6. String
1. Immutability of strings
2. String can be considered as a read-only group of type Char

7. Efficient StringBuilder
No need to open up a new string space, you can add, splice, insert, replace string, and finally tostring to string type
StringBuilder sb = new StringBuilder ();
Sb. Append ("Zhang San");
Sb. Append ("John Doe");
Sb. Append ("Harry");
Sb. Append ("Zhao Liu");
Sb. Insert (1, 123);
Sb. Replace ("John Doe", "color");
Sb. ToString ();
Console.WriteLine (SB);
Console.readkey ();
Console.readkey ();

Form
StringBuilder sb = new StringBuilder ();
Sb. Append ("Sb. Append ("Sb. Append ("Sb. Append ("<body>");
Sb. Append ("<table border= ' 1px ' cellpadding= ' 0px ' cellspacing= ' 0px ' >");
Sb. Append ("<tr>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("</tr>");
Sb. Append ("<tr>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("</tr>");
Sb. Append ("<tr>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("</tr>");
Sb. Append ("<tr>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("<td> Monday </td>");
Sb. Append ("</tr>");
Sb. Append ("</table>");
Sb. Append ("</body>");
Sb. Append ("Webbrowser1.documenttext = sb. ToString ();

7. Collection
Arrarylist
Hashtable
List<t>
Dictionary<tkey,tvalue>

ArrayList list = new ArrayList ();
List. ADD ();
List. AddRange ();
Set of key-value pairs
Hashtable ht = new Hashtable ();
Ht. ADD ()
list<int> list = new list<int> ();
List. ADD (); Add a single element
List. AddRange (); Add collection
List. Insert ();
List. Insertrange (); Insert Collection
List. Remove ();
List. RemoveAt (); Remove according to subscript
List. RemoveRange (); Remove a range of elements
List. Contains (); Determine if it contains

List. RemoveAll ();

List<t> is similar to the upgraded version of Arraylist,arraylist.
Various methods: Sort (), Max (), Min (), Sum () ...
Dictionary<k,v> is similar to the upgraded version of Hashtable,hashtable.
It is recommended to use generic collections.
T,k,v is like a lock, lock the set value can exist a certain type, here the t,k,v can also be other letters
A generic collection can be a foreach traversal because it implements the ienumerable<t> with the GetEnumerator () method

Dictionary<int, string> dic = new Dictionary<int, string> ();
Dic. ADD (1, "Zhang San");
Dic. ADD (2, "John Doe");
Dic. ADD (3, "Harry");
Dic. ADD (4, "Zhao Liu");
Add Direct Overlay adds error
DIC[4] = "or Zhao Liu";
foreach (keyvaluepair<int,string> kv in dic)
//{
Console.WriteLine ("{0}-----{1}", KV. Key,kv. Value);
//}
Console.readkey ();
8. Packing or unpacking
Boxing: Converting a value type to a reference type
Unpacking: Converting a reference type to a value type
Value type: BOOL int double char struct enum decimal stack
Reference type: string Array collection interface object custom class heap
The two types of unpacking or boxing must have an integrated

Set to Array
int[]ji= list. ToArray ();

Collection initializer
list<int> list = new List<int> () {1, 2, 3, 5, 64, 589, 642, 0, 36, 1000};

9. Differences in structure and class
Type
Structure: value type stack
Class: Reference type Heap

Declared language: class struct

In a class, in a constructor, you can either assign a value to a field or assign a value to a property. Constructors can be overloaded.
However, in the constructor of a struct, you must only assign a value to a field
In the constructor of a struct, we need to assign values to all fields, but not to assign values to the selected fields.

Call:

Can the structure be new?
The constructor of the new call structure in the stack opening space structure

Constructors for structs and classes:
The same point: whether it is a struct or a class, it will have a default parameterless constructor.
Difference: When you write a new constructor in the class, the default parameterless constructor is eliminated.
However, in the structure, if you write a new constructor, the default parameterless constructor is still there.

If we are simply storing data, we recommend using structs.
Because the value of the structure is allocated on the stack, the comparison saves space. Storing data simply does not involve object-oriented.

If we want to use object-oriented thinking to develop a program, we recommend using our class because the structure does not have object-oriented features

C # Basic notes (20th day)

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.