随笔分类 -  C#

1 2 下一页

【转载】#470 Define Your Own Custom Attribute
摘要:You can use predefined attributes to attach metadata to type members.You can also define a custom attribute by creating a new class inheriting from System.Attribute. The class name must end in "Attribute". You typically define a conostructor that takes arguments that consist of the metadat 阅读全文

posted @ 2014-03-20 21:31 yuthreestone 阅读(272) 评论(0) 推荐(0)

【转载】#458 Errors While Converting Between enum and Underlying Type
摘要:You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type.However, when you cast a value that doesn't have a corresponding enumerator in the type, you don't get any sort of error.In the example below,the Mood type has enumerators tha 阅读全文

posted @ 2014-03-20 20:36 yuthreestone 阅读(220) 评论(0) 推荐(0)

【转载】#457 Converting Between enums and their Underlying Type
摘要:When you declare an enum, by default each enumerated value is represented internally with an int. (System.Int32 - 4 bytes). You can convert between values of the underlying type and enum values using an explicit cast. Because an enum is represented by an int by default, you can convert between integ 阅读全文

posted @ 2014-03-20 20:20 yuthreestone 阅读(201) 评论(0) 推荐(0)

【转载】#446 - Deciding Between an Abstract Class and an Interface
摘要:An abstract class is a base class that may have some members not implemented in the base class, but only in the derived classes. An interface is just a contract that a class can choose to fulfill - a list fo member that it must implement if it implements the interface.You'll often use an abstrac 阅读全文

posted @ 2014-03-12 17:20 yuthreestone 阅读(219) 评论(0) 推荐(0)

【转载】#445 - Differences Between an Interface and an Abstract Class
摘要:An interface provides a list of members, without an implementation, that a class can choose to implement. This is similar to an abstract class, which may include abstract methods that have no implementation in the abstract class, but might also include an implementation for some of its members.One d 阅读全文

posted @ 2014-03-12 17:10 yuthreestone 阅读(169) 评论(0) 推荐(0)

【转载】#443 - An Interface Cannot Contain Fields
摘要:An interface can contain methods, properties, events or indexers. It cannot contain fields.1 interface IMoo2 {3 // Methods4 void Moo();5 6 // Field not allow - compile-time error7 string Name;8 }Instead of a field, you can use a property.1 interface IMoo2 {3 // Methods4 voi... 阅读全文

posted @ 2014-03-12 16:59 yuthreestone 阅读(306) 评论(0) 推荐(0)

【转载】#440 - A Class Can Implement More than One Interface
摘要:It's possible for a class to implement more than one interface.For example, a Cow class might implement both the IMoo and the IMakeMilk interfaces. Multiple interfaces are listed after the class declaration, separated by commas. 1 public class Cow: IMoo, IMakeMilk 2 { 3 public void Moo() 4 {... 阅读全文

posted @ 2014-03-12 16:41 yuthreestone 阅读(284) 评论(0) 推荐(0)

【转载】#438 - Benefits of Using Interfaces
摘要:You might wonder why you'd define an interface, have a class implement that interface and then access the class through the interface instead of just using the class directly.One benefit of interface is that it allows you to treat different types of objects in the same way, provided that they im 阅读全文

posted @ 2014-03-12 16:25 yuthreestone 阅读(238) 评论(0) 推荐(0)

【转载】#437 - Access Interface Members through an Interface Variable
摘要:Onece a class implementation a particular interface, you can interact with the members of the interface through any instance of that class.1 Cow bossie = new Cow("Bossie", 5);2 3 // Cow implements IMoo, which includes Moo method4 bossie.Moo();You can also declare a variable whose type is a 阅读全文

posted @ 2014-03-12 14:45 yuthreestone 阅读(164) 评论(0) 推荐(0)

【转载】#402 - Value Equality vs. Reference Equality
摘要:When we normally think of "equality",we're thinking of value equality - the idea that the values stored in two different objects are the same. This is also known as equivalence. For example, if we have two different variables that both store an integer value of 12, we say that the vari 阅读全文

posted @ 2014-03-12 10:25 yuthreestone 阅读(269) 评论(0) 推荐(0)

【转载】#370 - Subscribe to an Event by Adding an Event Handle
摘要:You subscribe to a particular event in C# by defining an event handler-code that will be called whenever the event occurs (is raised). You then attach your event handler to an event on a specific object, using the += operator.Below is an example where we define an event handler for the Dog.Barked ev 阅读全文

posted @ 2014-03-12 09:20 yuthreestone 阅读(190) 评论(0) 推荐(0)

【转载】#349 - The Difference Between Virtual and Non-Virtual Methods
摘要:In C#, virtual methods support polymorphism, by using a combination of the virtual and override keywords. With the virtual keyword on the base class method and the override keyword on the method in the derived class, both methods are said to be virtual.Methods that don't have either the virtual 阅读全文

posted @ 2014-03-11 20:59 yuthreestone 阅读(169) 评论(0) 推荐(0)

【转载】#346 - Polymorphism
摘要:Recall that polymorphism is one of the three core principles of object-oriented programming.Polymorphism is the idea that the same code can act differently, depending on the underlying type of the object being acted upon. The type of the object is determined at run-time, rather than at compile-time. 阅读全文

posted @ 2014-03-11 20:48 yuthreestone 阅读(177) 评论(0) 推荐(0)

【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object
摘要:When you use the new modifier to hide a base class method, it will still be called by objects whose type is the base class. Objects whose type is the derived class will call the new method in the derived class.1 Dog kirby = new Dog("kirby", 15);2 3 // Calls Dog.Bark4 kirby.Bark();5 6 Terri 阅读全文

posted @ 2014-03-11 20:30 yuthreestone 阅读(194) 评论(0) 推荐(0)

【转载】#336 - Declaring and Using a readonly Field
摘要:You can make a field in a class read-only by using the readonly modifier when the field is declared.In the example below, code that creates or uses instances of the Dog class will be able to read the SerialNumber field, but not write to it. 1 public class Dog 2 { 3 public string Name; 4 publ... 阅读全文

posted @ 2014-03-11 19:37 yuthreestone 阅读(179) 评论(0) 推荐(0)

【转载】#335 - Accessing a Derived Class Using a Base Class Variable
摘要:You can use a variable whose type is a base class to reference instances of a derived class.However, using the variable whose type is the base class, you can use it to access only members of the base class and not members of the derived class.In the example below, we have two instances of the Terrie 阅读全文

posted @ 2014-03-11 19:28 yuthreestone 阅读(235) 评论(0) 推荐(0)

【转载】#330 - Derived Classes Do Not Inherit Constructors
摘要:A derived class inherits all of the members of a base class except for its constructors.You must define a constructor in a derived classunlessthe base class has defined a default (parameterless) constructor. If you don’t define a constructor in the derived class, the default constructor in the base 阅读全文

posted @ 2014-03-11 12:09 yuthreestone 阅读(199) 评论(0) 推荐(0)

【转载】#324 - A Generic Class Can Have More than One Type Parameter
摘要:A generic class includes one or more type parameters that will be substituted with actual types when the class is used. If the class has more than one type parameter, all parameters must be substituted when the class is used.Here's an example of a generic class that stores two objects, each havi 阅读全文

posted @ 2014-03-11 11:48 yuthreestone 阅读(226) 评论(0) 推荐(0)

【转载】#323 - A Generic Class is a Template for a Class
摘要:A generic classs is a class that takes one or more type parameters, which it then uses in the definition of the class. It can be thought of as a template for a class.1 public class ThingContainer2 {3 private TParam theThing;4 5 public void SetThing(TParam newValue)6 {7 theThing = newValue;... 阅读全文

posted @ 2014-03-11 10:53 yuthreestone 阅读(299) 评论(0) 推荐(0)

【转载】#303 - Accessibility of Class Members
摘要:Members of a class can have different kinds of accessibility.An accessibility keyword indicates what source code can access the member. The different types of accessibility are:public -All code can access the memberprivate -Only other code in the class can access the memberprotected -Code in this cl 阅读全文

posted @ 2014-03-11 09:36 yuthreestone 阅读(176) 评论(0) 推荐(0)

1 2 下一页

导航