C#

C#

C# TALTECH IT COLLEGE 2019-2020, ANDRES KVER C# - Delegates A delegate is a type that defines a method signature, and can provide a reference to any method with a compatible signature. You can invoke (or call) the method through the delegate. Delegates are used to pass methods as arguments to other methods. 2 C# - Delegates 3

public delegate void SampleDelegate(string str); class SampleClassDelegate { // Method that matches the SampleDelegate signature. public static void SampleMethod(string message) { // Add code here. } // Method that instantiates the delegate. void SampleDelegate() { SampleDelegate sd = SampleMethod; sd("Sample string"); } } C# - Methods A method is a code block that contains a series of statements.

A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started. 4 C# - Methods Method signature Methods are declared in a class or struct by specifying the access

level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method. A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to. 5 C# - Methods reference vs value Value types bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort

When value type is passed to method, copy is passed Changes to the argument wont change original value Use ref keyword for passing value types by reference When reference type argument is passed, you will change the original (objects) 6 C# - Methods - ref Ref has to be used in declaration

Ref has to be used in calling 7 public void RefExample(ref int x) { x = x*x; } var x = 5; RefExample(ref x); C# - Methods - out The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. 8 class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } } C# - Constructors

No parameters default constructor. If class has no constructors defined, compiler autogenerates one for you unless class is static. Constructors can be overloaded Default constructor is not required 9 public class Employee { public int Salary; public Employee(int annualSalary) { Salary = annualSalary; } public Employee(int weeklySalary, int

numberOfWeeks) { Salary = weeklySalary * numberOfWeeks; } } C# - Constructors 10 A constructor can use the base keyword to call the constructor of a base class. The base keyword can be used with or without parameters. Any parameters to the constructor can be used as parameters to base

In a derived class, if a base-class constructor is not called explicitly by using the base keyword, the default constructor, if there is one, is called implicitly. public class Manager : Employee { public Manager(int annualSalary) : base(annualSalary) { //Add further instructions here. } } C# - Constructors A constructor can invoke another constructor in the same object by using the this keyword.

Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this. 11 public class Employee { public int Salary; public Employee(int annualSalary) { Salary = annualSalary; } public Employee(int weeklySalary, int numberOfWeeks) : this(weeklySalary * numberOfWeeks) { } C# - Constructors

Constructors can be marked as public private protected internal protected internal A constructor can be declared static by using the static keyword.

Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. No access modifiers or parameters are allowed. 12 C# - this The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method. To qualify members hidden by similar names To pass an object as a parameter to other methods

To declare indexers Static member functions, because they exist at the class level and not as part of an object, do not have a this pointer 13 C# - this qualify members hidden by similar names 14 public class Employee { private int salary; public Employee(int salary) { this.salary = salary; } }

class SampleCollection { private T[] arr = new T[100]; public T this[int i] { Indexer get { return arr[i]; } var x = new SampleCollection(); set { arr[i] = value; } var s = x[0]; } } C# - Extension methods Extension methods enable you to "add" methods to existing types without creating a new derived type. public static class MyExtensions { public static int WordCount(this String str) { return str.Split(new[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; }

} string s = "Hello Extension Methods"; int i = s.WordCount(); 15 C# - Named arguments Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. A named argument can follow positional arguments.

A positional argument cannot follow a named argument. 16 C# - Named arguments 17 class NamedExample { static void Main(string[] args) { // The method can be called in the normal way, by using positional arguments. Console.WriteLine(CalculateBMI(180, 184)); // Named arguments can be supplied for the parameters in either order. Console.WriteLine(CalculateBMI(weight: 123, height: 64)); Console.WriteLine(CalculateBMI(height: 64, weight: 123)); // Named arguments can follow positional arguments. Console.WriteLine(CalculateBMI(123, height: 64)); } static int CalculateBMI(int weight, int height) { return (weight * 703) / (height * height);

} } C# - Optional arguments The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used.

Optional parameters are defined at the end of the parameter list, public void ExampleMethod(int required, after any required parameters. string optionalStr = "default string", int optionalInt = 10) 18 19 THE END

Recently Viewed Presentations

  • Electromagnetic Radiation - Oneonta

    Electromagnetic Radiation - Oneonta

    * * * * * * * * * * * * * * Shells, Subshells and Orbitals Which subshell does not exist? 5s 2p 2d 4f 15s NODES Spherical Nodes Quantum Numbers and Numbers of Orbitals * * *...
  • DESIGN RESOURCE 10: SKETCHING AS A FORM OF

    DESIGN RESOURCE 10: SKETCHING AS A FORM OF

    Develop sketch techniques, scale & proportion. Create a collection portfolio of ideas, notes and concepts. Sketching as opposed to Prototyping. ADVANTAGES OF SKETCHING. COMMONLY USED DESIGN SKETCHES. 2D View Scribble Sketch. 3D Pictorial Isometric. 3D Pictorial Oblique. 3D Wire Frame...
  • THEORIES OF THE ATOM - scienceatloyola.weebly.com

    THEORIES OF THE ATOM - scienceatloyola.weebly.com

    John Dalton: The Billiard Ball Model (1807) all matter is made up of tiny, indivisible particles called atoms • all atoms of an element are identical • atoms of different elements are . different. atoms are rearranged to form new...
  • Project In Computer Science Computer Networks

    Project In Computer Science Computer Networks

    Times Times New Roman Book Antiqua Monotype Sorts Symbol Arial Comic Sans MS Wingdings Arial Unicode MS Helvetica Tahoma ucr-yellow-BigLogo Microsoft Excel Worksheet CS-279-I: Design Project In Computer Science Computer Networks This is THE Class!
  • Theme 1: Relationships Weekly Theme: School Days Selection 1 ...

    Theme 1: Relationships Weekly Theme: School Days Selection 1 ...

    Comprehension Strategy: I can monitor my own comprehension as I read a passage. Comprehension Skill: I can compare and contrast two items. Spelling words for Unit 4 Week 3: "Washington Weed Whackers" I can decode words with soft c and...
  • Nutrition - Weebly

    Nutrition - Weebly

    Nutrition. Nutrition is the sum total of the processes involved in the taking in and the utilization of food substances by which growth, repair and maintenance of the body are accomplished… Carbohydrates. Fiber. Fats. Protein. Vitamins. Minerals. Water. All seven...
  • Hvac Equipment

    Hvac Equipment

    Static Regain Method Refers to increase or regain of static pressure in the ductwork when the air velocity decreases. Ducts are sized calculating air velocity for all sections where changes in air velocity takes place. Static regain calculations help to...
  • Chapter 3 Muscular Considerations for Movement

    Chapter 3 Muscular Considerations for Movement

    Chapter 3 Muscular Considerations for Movement ... Converts stimulation into force Parallel elastic (PEC) Allows the muscle to be stretched Associated with fascia surrounding muscle Series elastic (SEC) Transfers muscle force to bone Hill Muscle Model Role of Muscle Origin...