   A class is a construct that enables you to    create your own custom types by grouping    together variables of other ...
   The data and functions within a class are    known as the class’s members.   Data Members      Data members are thos...
   A struct type is a value type that can contain    constructors, constants, fields, methods, properties,    indexers, o...
   Structs are value types and classes are    reference types.   Unlike classes, structs can be instantiated    without ...
   A method is a code block that contains a series of    statements.   In C#, every executed instruction is performed in...
   Passing Parameters to Methods    ◦ Parameters can be passed into methods by reference or by value.    ◦ In C#, all par...
   Passing by Reference vs. Passing by Value      When a value type is passed to a method, a copy is       passed instea...
   Named Arguments      Named arguments allow you to pass in parameters in       any order.   Optional Arguments      ...
   C# supports method overloading – several    versions of the method that have different    signatures.    ◦ To overload...
   A property is a member that provides a flexible    mechanism to read, write, or compute the value of    a private fiel...
   Whenever a class or struct is created, its constructor is called.   A class or struct may have multiple constructors ...
   Static Constructors        A static constructor is used to initialize any static data, or to perform         a partic...
   A constructor can invoke another constructor    in the same object by using the this keyword.
   The readonly keyword is a modifier that you can    use on fields.   When a field declaration includes a readonly    m...
   Anonymous types provide a convenient way to    encapsulate a set of read-only properties into a single    object witho...
   It is possible to split the definition of a class or a struct, an    interface or a method over two or more source fil...
   A static class is basically the same as a non-static    class, but there is one difference: a static class    cannot b...
   As indicated earlier, all .NET classes are ultimately      derived from System.Object.     System.Object Methods:Equa...
   Extension methods enable you to "add" methods to    existing types without creating a new derived type,    recompiling...
Objects and Types C#
Upcoming SlideShare
Loading in …5
×

Objects and Types C#

481
-1

Published on

Published in: Technology, Business
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
481
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
21
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Objects and Types C#

  1. 1.  A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. Syntax for creating a class  [attributes] [modifiers] class identifier [:base-list] { class-body } Ex:  public class Customer { //Fields, properties, methods and events go here... } Contd…
  2. 2.  The data and functions within a class are known as the class’s members. Data Members  Data members are those members that contain the data for class – fields, constants and events.  Data members can be static.  Fields are any variables associated with the class.  Events are class members that allow an object to notify a caller whenever something of interest occurs. Function Members  Function members are those members that provide some functionality for manipulating the data in the class.  These include methods, properties, constructors, finalizers, operators, and indexers.
  3. 3.  A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:  [attributes] [modifiers] struct identifier [:interfaces] {//body } Structs share most of the same syntax as classes, although structs are more limited than classes: Within a struct declaration, fields cannot be initialized unless they are declared as const or static. A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
  4. 4.  Structs are value types and classes are reference types. Unlike classes, structs can be instantiated without using a new operator. Structs can declare constructors that have parameters. A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object. A struct can implement interfaces. A struct can be used as a nullable type and can be assigned a null value.
  5. 5.  A method is a code block that contains a series of statements. In C#, every executed instruction is performed in the context of a method. Declaring Methods ◦ Syntax [modifiers] return-type MethodName ([parameters]) { //method body } Contd…
  6. 6.  Passing Parameters to Methods ◦ Parameters can be passed into methods by reference or by value. ◦ In C#, all parameters are passed by value unless you specifically say otherwise. ref Parameters ◦ The ref keyword causes arguments to be passed by reference. The effect is that any changes to the parameter in the method will be reflected in that variable when control passes back to the calling method. out Parameters ◦ The out keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. Contd…
  7. 7.  Passing by Reference vs. Passing by Value  When a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method. You can pass a value-type by reference by using the ref keyword.  Reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy. Changes made through this reference will therefore be reflected in the calling method. Contd…
  8. 8.  Named Arguments  Named arguments allow you to pass in parameters in any order. Optional Arguments  Parameters can also be optional  You must supply a default value for parameters that are optional  The optional parameter(s) must be the last ones defined as well.
  9. 9.  C# supports method overloading – several versions of the method that have different signatures. ◦ To overload methods, you simply declare the methods with the same name but different number or types of parameters.
  10. 10.  A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. ◦ Ex: private int _age; public int Age { get{return age;} set{age=value;} } ◦ We can have access modifiers for set and get accessors
  11. 11.  Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read. If you do not provide a constructor for your object, C# will create one by default that instantiates the object and sets member variables to the default values. Static classes and structs can also have constructors. We can have private constructors Contd…
  12. 12.  Static Constructors  A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.  A static constructor does not take access modifiers or have parameters.  The user has no control on when the static constructor is executed in the program. Calling Constructors from Other Constructors  A constructor can use the base keyword to call the constructor of a base class.  Ex: public Manager() : base() { //Add further instructions here. } Contd…
  13. 13.  A constructor can invoke another constructor in the same object by using the this keyword.
  14. 14.  The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. Ex: class Age { readonly int _year; Age(int year) { _year = year; } void ChangeYear() { //_year = 1967; // Compile error if uncommented. } }
  15. 15.  Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. You create anonymous types by using the new operator together with an object initializer. ◦ Ex:  var v = new { Amount = 108, Message = "Hello" };
  16. 16.  It is possible to split the definition of a class or a struct, an interface or a method over two or more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled. To split a class definition, use the partial keyword modifier, as shown here: public partial class Employee { public void DoWork() { } } public partial class Employee { public void GoToLunch() { } }
  17. 17.  A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. The following list provides the main features of a static class:  Contains only static members.  Cannot be instantiated.  Is sealed.  Cannot contain Instance Constructors.
  18. 18.  As indicated earlier, all .NET classes are ultimately derived from System.Object.  System.Object Methods:Equals(Object) Determines whether the specified object is equal to the current Object.Finalize() Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.GetHashCode() Serves as a hash function for a particular typeMemberwiseClone() Creates a shallow copy of the current Object.ToString() Returns a string that represents the current object.
  19. 19.  Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×