C # Fundamentals Review (ii)-page value passing, overloading and rewriting, classes and structs, boxing and unpacking

Source: Internet
Author: User

I. Preface

-The lonely Road with dreams to accompany, the waves-

two. Page Value delivery

(1) C # each page between the exchange of data and transfer, the page can be based on the data obtained, the respective operation (jump, calculation, etc.). In order to implement data transfer in many ways, C # provides several ways:

1.query.string Way

2.server.transfer Way

3.Cookie mode

4.Session mode

5.Application mode

(2) Realization mode

New Testtransfer project, add transferone.aspx page and Transfertwo.aspx page. In transferone.aspx, add two text boxes and a button to implement the jump. Add the implemented logic code in Btnlogin_click:

Add in Transfertwo.aspx:

Add the logic implementation code to the Page_Load

These are several ways of passing values between C # pages, where the Query.string method displays the parameter value information passed in the URL address bar.

Note: The difference between a session and a cookie

Because the HTTP protocol is TCP/IP-based, when the user sends a request from the client to the server, the server responds to the request and returns the data to the client, the process is stateless, in order to save some state of the client, and therefore uses the session and cookie mechanism.

(1) The session is stored on the server side, the cookie is stored on the client, and the storage size is limited.

(2) The essence of session implementation is to rely on the cookie to do, generally the content of sensitive information with the session stored on the server, such as passwords. First, after the server generation session, return SessionID to the client, the client cookie to save the value of SessionID,

The next time you request, take SessionID to get the corresponding session value.

(3) The period of validity of a cookie may be set.

How to declare a cookie:

var cookie = request.cookies["user"]?? New HttpCookie ("user");

Cookies. Values.set ("username", this. TextBox1.Text);

Cookies. Values.set ("Password", this. TextBox2.Text);

Response.setcookie (cookie);

RESPONSE.COOKIES.ADD (cookie);

DECLARE session mode:

session["username"] = this. TextBox1.Text;

session["password"] = this. TextBox2.Text;

third, overloading and rewriting, hiding

(1) Definition:

Overloading: Occurs within the same scope (for example, inside a class) and defines a series of methods with the same name, but the method has a different argument list. This allows you to decide which one to call by passing different parameters. The return type can be the same or different.

Override: Occurs when inheriting, redefining a method in a parent class in a subclass, and the method in the subclass is the same as the method of the parent class. For example: The base class method is declared as virtual (virtual method), and the override in the derived class is used to declare this method.

Hidden: The base class method does not make a declaration (default is a non-virtual method), and in a derived class uses new to declare the hiding of this method. A subclass overrides a method of a base class whose return value type must be the same.

(2) Implement Demo

static void Main (string[] args)

{

var coderone=new DoWork ();

Coderone.docoding ("Liupeng");

Coderone.docoding ("Liupeng", 24);

var codertwo=new subdowork ();

Codertwo.say ();

Methods for hiding base classes

Codertwo.enjoylife ();

Decide whether to call a method in a base class or subclass by instantiating an object

Coderone.enjoylife ();

Console.readkey ();

}

public class DoWork

{

public virtual void Say ()

{

Console.WriteLine ("I am in Base calss,hello everyone!");

}

public void Enjoylife ()

{

Console.WriteLine ("I am a coder, but I like my life, Coding are my life,also is my girl! Haha! ");

}

public void Docoding (string name)

{

Console.WriteLine (String. Format ("My name is {0},i love coding!", name);

}

public void Docoding (string name, Int. age)

{

Console.WriteLine (String. Format ("My name is {0},age are {1},i always love coding!", name, age));

}

}

public class Subdowork:dowork

{

public override void Say ()

{

Console.WriteLine ("I AM in Subclass,hello everybody!");

}

New public void Enjoylife ()

{

Console.WriteLine ("I Am a new bird, I must work hard to achieve my dream!");

}

}

Iv. class and Structure

1. About the differences between classes and structs:

(1) The content passed in the class is stored in the managed heap, and the content passed in the struct is stored in the stack.

(2) When the object passed in the class is modified, the source object is modified, and when the object in the struct is modified, the source object does not change and remains in its original state.

(3) A constructor in a class, either a default parameterless constructor or a constructor with arguments, but the constructor in the struct must be a constructor with parameters and cannot be the default constructor.

2. Implement Demo

static void Main (string[] args)

{

Console.WriteLine ("-----------The value of the field before initialization--------------");

var studentone = new Stuname ("Zhangyonghe", 26);

var studenttwo = new Structstuname ("Liupeng", 25);

Studentone.sayhello ();

Studenttwo.sayhello ();

Console.WriteLine ("--------------to modify the value of the object when it is passed------------");

var studentonenew = Studentone;

Studentone.age = 30;

Studentone.sayhello ();

var studenttwonew = Studenttwo;

Studenttwonew.age = 35;

Studenttwo.sayhello ();

Console.readkey ();

}

public class Stuname

{

public string Name {get; set;}

public int Age {get; set;}

Public Stuname (string name, Int. age)

{

This. name = name;

This. Age = age;

}

public void SayHello ()

{

Console.WriteLine (String. Format (the name of the class struct is: {0}, Age: {1} "," name ");

}

}

public struct Structstuname

{

public string Name;

public int age;

The constructor of a struct must be dripping. Parameters

Public Structstuname (string name, Int. age)

{

name = name;

Age = age;

}

public void SayHello ()

{

Console.WriteLine (String. Format (the name of the struct is: {0}, age is: {1} "," Name "," Ages ");

}

}

The result of the operation is:

five. Box packing and unpacking

(1) Definition: Converting a value type to a reference type is boxing; converting a reference type to a value type is called unpacking.

(2) familiar with a section of code:

public void Method1 () {

Line 1

int i=4;

Line 2

int y=2;

Line 3

Class1 cls1 = new Class1 ();

}

At this point, the program is parsed like this:

(1) The corresponding memory space is allocated on the stack based on the size of the value type, and the value of the values type variable exists on the stack space.

(2) When executing to instantiate an object CLS1, the compiler creates a pointer on the station, and the real object is stored in another memory called "Heap".

The heap does not track running memory, it is more like a heap of objects that can be accessed at any time. The heap is used to dynamically allocate memory. It is emphasized here that the reference pointer is allocated on the stack.

Declaring Class1 CLS1 does not allocate memory to an instance of Class1, but instead allocates a stack variable CLS1 (and sets to null) and then points it to the heap.

When we initialize an object and assign CLS1 to CLS2, we create a stack variable on top of the stack and point the pointer to the reference to the Cls1 object.

And then we'll see what happened to the unpacking process.

int i=4;

Object o=i;

int j= (int) o;

The process of unpacking:

(1). Check instance: first check whether the value of the variable is null, if yes, throw the NullReferenceException exception, and then check that the reference to the variable refers to the object is not a boxed object of the given value type, and if not, throws a InvalidCastException exception.

(2). Return address: Returns the address of the original value Type field in the boxed instance, and two additional members (type object pointer and Synchronous block index) are not returned.

Here, the unpacking process is over, but with the unboxing, the "often" (described in "CLR via C #", which uses "often", and does not say certain), immediately follows the copy of the field once.

It's actually about copying the instance fields from the boxed object to the memory stack.

A self-maintenance of the public number, the current article is not much, I hope the next day, will have been updated, write their own life, sharing technology, hope to love the life of the technology house, together with the exchange!

C # Fundamentals Review (ii)-page value passing, overloading and rewriting, classes and structs, boxing and unpacking

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.