iFour Consultancy Basics of .NET
https://www.ifourtechnolab.com/aspdotnet-web-development
Constructor Special method of a class which is invoked automatically whenever instance or object of class is created Responsible for object initialization and memory allocation of its class
Any class without constructor, the compiler will automatically create one default constructor for that class. There is
always at least one constructor in every class Constructor types
Default Constructor Parameterized Constructor Copy Constructor Static Constructor Private Constructor Example: class SampleA { public SampleA() { Console.WriteLine("Sample A Test Method"); } }
https://www.ifourtechnolab.com/aspdotnet-web-development
Data Types Programming language that is set of data with values having predefined characteristics Examples : integer, floating point unit number, character, string, and pointer
Value type
Value type variables can be assigned a value directly Derived from the class System.ValueType E.g. int, char, and float, that stores numbers, alphabets, and floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value
Reference type
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location Data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic and string
https://www.ifourtechnolab.com/aspdotnet-web-development
Control statements Statement that determines whether other statements will be executed An if statement decides whether to execute another statement, or decides which of two
statements to execute.
A loop decides how many times to execute another statement. There are three kinds of loops: while loops test whether a condition is true before executing the controlled statement do-while loops test whether a condition is true after executing the controlled statement for loops are (typically) used to execute the controlled statement a given number of times
A switch statement decides which of several statements to execute
https://www.ifourtechnolab.com/aspdotnet-web-development
Properties and Methods Properties
Fields of objects/classes with dedicated getter/setter routines (which can be considered as methods. There are languages that don't have properties and this behavior is achieved using a private field+get/set methods.).
Methods
Similar to functions, they belong to classes or objects and usually express the verbs of the objects/class. For example, an object of type Window usually would have methods open and close which do corresponding operations to the object they belong.
https://www.ifourtechnolab.com/aspdotnet-web-development
Structure Value type data type It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure. To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. It cannot be used as a base for other structures or classes
A structure can implement one or more interfaces Structure members cannot be specified as abstract, virtual, or protected Classes are reference types and structs are value types
For example, here is the way you can declare the Book structure: struct Books { public string title; public string author; public string subject; public int book_id; };
https://www.ifourtechnolab.com/aspdotnet-web-development
Abstract Classes An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block Derived classes of the abstract class must implement all abstract methods When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method Classes can be declared as abstract by putting the keyword abstract before the class definition For example: public abstract class A { // Class members here. }
https://www.ifourtechnolab.com/aspdotnet-web-development
Interface It contains definitions for a group of related functionalities that a class or a struct can implement. The public definitions comprise the interface for the class, which should never change, and a contract between the
creator of the class and the users of the class It contains declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared Defining an Interface: interface IMyInterface { void MethodToImplement(); } Using an Interface: class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } }
https://www.ifourtechnolab.com/aspdotnet-web-development
Delegates and Events Delegate and Event concepts are completely tied together. Delegates are just function pointers, That is, they hold references to functions It is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer Every delegate has a signature. For example: delegate int SomeDelegate(string s, bool b); The Event model in .NET Languages finds its roots in the event programming model that is popular in asynchronous programming
• • • • • •
The following important conventions are used with events: Event Handlers in the .NET Framework return void and take two parameters The first parameter is the source of the event; that is the publishing object The second parameter is an object derived from EventArgs Events are properties of the class publishing the event The keyword event controls how the event property is accessed by the subscribing classes
https://www.ifourtechnolab.com/aspdotnet-web-development
Error Handling An exception is a problem that arises during the execution of a program. An exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to
divide by zero Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw.
try: Block of code for which particular exceptions is activated, it is followed by one or more catch blocks catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem.
The catch keyword indicates the catching of an exception finally: Used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not throw: A program throws an exception when a problem shows up, this is done using a throw keyword Example: try { // statements causing exception } catch( ExceptionName ex){ // error handling code} catch( ExceptionName ex){ // error handling code} finally{ // statements to be executed }
https://www.ifourtechnolab.com/aspdotnet-web-development
Static and virtual Keyword It’s a keyword which denotes things that are singular It often improves performance, but makes programs less flexible. These are called with the type
name. No instance is required - this makes them slightly faster. Static methods can be public or private For E.g. static class Perls { public static int value = 5; } It can be accessed directly by using Class For E.g. Perls.value which will return 5
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow
for it to be overridden in a derived class For example, this method can be overridden by any class that inherits it. For E.g. public virtual double Area() { return x * y; } https://www.ifourtechnolab.com/aspdotnet-web-development
Thank You..
https://www.ifourtechnolab.com/aspdotnet-web-development