Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications -...
-
Upload
adarsh-patel -
Category
Engineering
-
view
57 -
download
1
Transcript of Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications -...
![Page 1: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/1.jpg)
Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications
- Generating Console Output, Processing , Console Input
Subject :: Dot Net Technology
:: Presented by ::Adarsh Patel
![Page 2: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/2.jpg)
Overview
Modifiers PropertyIndexersAttributes & Reflection APIConsole Applications
Reference :: http://www.tutorialspoint.com :: https://msdn.microsoft.com/en-IN/library/
2D O T N E T
![Page 3: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/3.jpg)
• The Modifiers are keywords added with any programming element to give some especial emphasis on how the programming element will behave or will be accessed in the program.
• For example, the access modifiers: • Public• Private • Protected• Friend • Protected Friend• indicate the access level of a programming element
like a variable, constant, enumeration or a class.
3D O T N E T
Modifiers
![Page 4: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/4.jpg)
4D O T N E T
Modifier Description
Access Modifiers• Public• Private• Internal• protected
Specifies the declared accessibility of types and type members.
abstract Indicates that a class is intended only to be a base class of other classes.
async Indicates that the modified method, lambda expression, or anonymous method is asynchronous.
const Specifies that the value of the field or the local variable cannot be modified.
event Declares an event.
volatile Indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.
Modifiers Continue..
![Page 5: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/5.jpg)
5D O T N E T
Modifiers Continue..Modifier Description
extern Indicates that the method is implemented externally.
new Explicitly hides a member inherited from a base class.
override Provides a new implementation of a virtual member inherited from a base class.
partial Defines partial classes, structs and methods throughout the same assembly.
readonly Declares a field that can only be assigned values as part of the declaration or in a constructor in the same class.
sealed Specifies that a class cannot be inherited.
static Declares a member that belongs to the type itself instead of to a specific object.
unsafe Declares an unsafe context.
virtual Declares a method or an accessor whose implementation can be changed by an overriding member in a derived class.
![Page 6: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/6.jpg)
6D O T N E T
Property
• Properties are named members of classes, structures, and interfaces. Member variables or methods in a class or structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.
• Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
• Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.
![Page 7: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/7.jpg)
7D O T N E T
class TimePeriod { private double seconds;
public double Hours { get { return seconds /
3600; } set { seconds = value *
3600; } }}
class Program { static void Main() { TimePeriod t = new TimePeriod();
// Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24;
// Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours);
} }
// Output: Time in hours: 24
Property Continue..
![Page 8: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/8.jpg)
8D O T N E T
• A Get procedure returns the value of a property. It is called when you access the property in an expression.
• A Set procedure sets a property to a value, including an object reference. It is called when you assign a value to the property.
You usually define property procedures in pairs, using the Get and Set statements, but you can define either procedure alone if the property is read-only (Get Statement) or write-only (Set Statement).
Property Continue..
![Page 9: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/9.jpg)
9D O T N E T
Indexer
• An indexer allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a virtual array. You can then access the instance of this class using the array access operator ([ ]).
• properties return or set a specific data member, whereas indexers returns or sets a particular value from the object instance. In other words, it breaks the instance data into smaller parts and indexes each part, gets or sets each part.
• public int this[int index] // Indexer declaration { // get and set accessors}
![Page 10: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/10.jpg)
10D O T N E T
Indexer Continue..
class SampleCollection<T> {
// Declare an array to store the data elements.private T[] arr = new T[100];
/* Define the indexer, which will allow client code to use [] notation on the class instance itself. (See line 2 of code in Main below.) */
public T this[int i] { get {
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array. return arr[i]; } set { arr[i] = value; } }
}
![Page 11: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/11.jpg)
11D O T N E T
Indexer Continue..
// This class shows how client code uses the indexer.
class Program { static void Main(string[] args){
// Declare an instance of the SampleCollection type. SampleCollection<string> st = new SampleCollection<string>();
// Use [] notation on the type.st [0] = "Hello, World"; System.Console.WriteLine(st [0]);
}}
![Page 12: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/12.jpg)
12D O T N E T
Comparison of indexers and propertiesPROPERTY INDEXER
Allows methods to be called as if they were public data members.
Allows elements of an internal collection of an object to be accessed by using array notation on the object itself.
Accessed through a simple name. Accessed through an index.
Can be a static or an instance member. Must be an instance member.
A get accessor of a property has no parameters.
A get accessor of an indexer has the same formal parameter list as the indexer.
A set accessor of a property contains the implicit valueparameter.
A set accessor of an indexer has the same formal parameter list as the indexer, and also to the value parameter.
Supports shortened syntax with Auto-Implemented Properties (C# Programming Guide).
Does not support shortened syntax.
![Page 13: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/13.jpg)
13D O T N E T
Attributes
• Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The .Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.
• An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program.
• Syntax for specifying an attribute is as follows[attribute(positional_parameters,name_parameter =
value, ...)] element
![Page 14: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/14.jpg)
14D O T N E T
Attributes Continue..
• example shows how to apply attributes to methods, method parameters, and method return values in C#.
// default: applies to method [SomeAttr] int Method1() { return 0; }
// applies to method [method: SomeAttr] int Method2() { return 0; }
// applies to return value [return: SomeAttr] int Method3() { return 0; }
![Page 15: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/15.jpg)
15D O T N E T
Attributes Continue..
• The .Net Framework provides three pre-defined attributes
AttributeUsage
Conditional
Obsolete
![Page 16: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/16.jpg)
16D O T N E T
Reflection API
• Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace.
• The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.
• as below given code
Type t = typeof(Car);MethodInfo[] methods = t.GetMethods();foreach (MethodInfo nextMethod in methods){
// etc.}
![Page 17: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/17.jpg)
17D O T N E T
• The System.Type class is the main class for the .NET Reflection functionality and is the primary way to access metadata. The System.Type class is an abstract class and that represents a type in the Common Type System (CLS).
• The MemberInfo object of the System.Reflection class needs to be initialized for discovering the attributes associated with a class. To do this, you define an object of the target class.as below given code
Type t = typeof(Car);MethodInfo[] methods = t.GetMethods();foreach (MethodInfo nextMethod in methods){
// etc.}
Reflection API Continue..
![Page 18: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/18.jpg)
18D O T N E T
Console Application
When to use console application
Console Application
![Page 19: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/19.jpg)
19D O T N E T
When to use Console Application
• A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems .
• In .NET you can work with the console application by using the Console class and its methods.Method Description
Read() Reads the next character from the console windows
ReadLine() Reads the Next line of characters from the console Windows
Write() Writes the specified string value to the console windows
WriteLine() Writes the specified string value , followed by the current line terminator , to the console windows
![Page 20: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/20.jpg)
20D O T N E T
Consol application
• In order to create a C# console based applicationOpen your Visual Studio.
Open Your visual studio. On the File menu, click New Project. Then the New Project
dialog box appears. This dialog box lists the different default application types.
Select Console Application as your project type and change the name of your application at the bottom textbox. If you are not comfortable with default location, you can always enter a new path if you want.
Then Click OK.
![Page 21: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/21.jpg)
21D O T N E T
Consol application Continue..
![Page 22: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/22.jpg)
22D O T N E T
• After click OK button, you will get a screen like the following picture.
Consol application Continue..
![Page 23: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/23.jpg)
23D O T N E T
• You can accept the values from users, process that input, and display the respective result on console Window.
Consol application Continue..
![Page 24: Modifiers - Property and Indexers , Attributes & Reflection API, When to use Console Applications - Generating Console Output, Processing , Console Input](https://reader034.fdocuments.in/reader034/viewer/2022050811/58f072b51a28ab7b5b8b45d7/html5/thumbnails/24.jpg)
Thank you…