Private protect partial internal public difference

Source: Internet
Author: User

From: msdn

 

The situation is similar to personal secrets (shared only with friends), family secrets (shared with friends and children), and nonsecrets (shared with anybody), respectively.

A partial: it is possible to split the definition of a class or a struct, or an interface over two or more source files. each source file contains a section of the class definition, and all parts are combined when the application is compiled.

Please see the following example code:

Public partial class coords
{
Private int X;
Private int y;

Public coords (int x, int y)
{
This. x = X;
This. Y = y;
}
}

Public partial class coords
{
Public void printcoords ()
{
System. Console. writeline ("coords: {0}, {1}", x, y );
}

}

Class testcoords
{
Static void main ()
{
Coords mycoords = new coords (10, 15 );
Mycoords. printcoords ();
}
}

 A private: member of a class is accessible only by members and friends of the class.

See the following example code:

Using system;

Class employee
{
Private string name = "firstname, lastname ";
Private double salesary = 100.0;

Public String getname ()
{
Return name;
}

Public double salary
{
Get {return salary ;}
}
}

Class mainclass
{
Static void main ()
{
Employee E = new employee ();

// The data members are inaccessible (private), so
// Then can't be accessed like this:
// String n = E. Name;
// Double S = E. salary;

// 'Name' is indirectly accessed via method:
String n = E. getname ();

// 'Salary 'is indirectly accessed via property
Double S = E. salary;
}
}

 
A protected: member of a class is accessible by members and friends of the class and by members and friends of derived classes, provided they access the base member via a pointer or a reference to their own derived class.

Please see the following example code:

Using system;
Class
{
Protected int x = 123;
}

Class B:
{
Static void main ()
{
A A = new ();
B = new B ();

// Error cs1540, because X can only be accessed
// Classes derived from.
// A. X = 10;

// OK, because this class derives from.
B. x = 10;
}
}
 A public: member of a class is accessible by everyone.

See the following example code:
Using system;
Class Point
{
Public int X;
Public int y;
}

Class mainclass
{
Static void main ()
{
Point P = new point ();
// Direct access to public members:
P. x = 10;
P. Y = 15;
Console. writeline ("x = {0}, y = {1}", p. x, P. y );
}
}

 

 

A property: member is a generic term that refers to a field, property, or method of a class. A field is a variable, a property is a bit of data, and a method is a function/subroutine. for example:

Public class bankaccount
{
Private int _ balance;
Public int balance
{
Get {return _ balance ;}
Set {_ balance = value ;}
}
Public void deposit (INT depositamount)
{
// I can reference _ balance here because I'm inside the class that defined it
_ Balance = _ balance + depositamount;
}
}

The above C # Code represents a class. _ balance (field), balance (property), and deposit (method) are all "members" of the class.
Think of a class as a blueprint. A blueprint isn't a house, it's the definition of what the House will look like when you build it. you "build" an object from a class by "new" ing it.

Public class bank
{
Public void main ()
{
// Ba represents an object created from the class bankaccount
Bankaccount BA = new bankaccount ();

// This won't compile because _ balance is private so it is not accessible outside the class
Ba. _ balance = 10;

// This works fine because balance is public, so it is accessible from this code that
// Exists outside of the class definition for bankaccount
Ba. Balance = 10;
}
}

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.