C# Programming Language Part 2


 

C# Programming Language

A class is a logical unit of data. 

classes have members such as properties, fields, methods, and events.

Class - Fruits and Object - apple, banana, mango

* Class Access Modifiers *

Access Modifiers, which are used to set the access level/visibility for classes, fields, methods and properties. 
public, private, protected, internal, 

* Field *

A field member is a variable of a class and struct.
 public, private, protected, internal,  
A field member can also be read-only and static.

  1. public static read-only string author = "Umesh mahajan";  

Fields can be variables or constants.

  * Constructor *

Constructors are used to initialise the new objects. 
A class or struct can have one or more than one constructors.
Does not have a return type

constructor name is the same as a class name.
use of constructor is to initialise private fields of the class while Creating Objects.

  {
    Car Ford = new Car();  // Create an object of the Car Class (this will call the constructor)
    Console.WriteLine(Ford.model);  // Print the value of model
  }

  *Method*

The method member of a class is a block of code that performs certain Actions.
Methods belong to a class and they define how to an Object of a class behaves.
A method can return a void and Data type.

Method Parameters - information can be passed to Methods as a parameter. Parameter act as Variables inside the Method.

*Method Overloading*


Declare same name methods but different signatures is known as method overloading 
(Different number of parameters or different data types of parameters)

*Expression*


*Operator*

An operator is responsible for an operation. For example, the +, - indicate adding and subtracting operands, such as values, objects and literals. An operand can also be an expression or any number of sub-expressions.

using System;  
class Program  
{  
    static void Main(string[] args)  
    {  
        Console.WriteLine("Operators Sample!");  
  
        // Declare variables   
        int a = 10; int b = 20;  
        decimal d = 22.50m;  
        string first = "Mahesh"; string last = "Chand";  
  
        // Use + and - operators to add and subtract values     
        int total = a + b;  
        decimal dTotal = a + b + d;  
        int diff = b - a;  
        Console.WriteLine("diff: " + diff);  
        string name = first + " " + last;  
        Console.WriteLine("Name: " + name);  
  
        Console.ReadKey();  
    }  
}  

*Statement*

A statement is a part of a program that represents an action such as declaring variables, assigning values, calling methods, looping through a collection, and a code block with brackets. 
A statement can consist of a single line ending with a semicolon, or a code block enclosed in {} brackets.

*If... else statement*


If else statement is also known as a Conditional statement. Condition is Executed when the condition is true. if it's false then Control goes to else statement 

*Switch Statement*


C# switch statement is a conditional statement that pairs with one or more case blocks and a default block.
switch (expression)  
{  
    case expression_value1:  
        Statement  
        break;  
    case expression_value2:  
        Statement  
        break;  
    case expression_value3:  
        Statement  
        break;  
    default:  
        Statement  
        break;  
}  

*Foreach Statement*
*Do...While Statement*

The do-while statement executes a block of code until the specified while Condition is false.

do  
        {  
            Console.WriteLine(counter);  
            counter++;  
        } while (counter < 20);  

*While Statement*

while statement executes a block of code until the specified while Conditions is false.

while (counter < 20)  
        {  
            Console.WriteLine(counter);  
            counter++;  
        }   

*Goto Statement*

    The goto statement is used when the need to jump from one case block to another once a condition is met.

*Break Statement*

The break statement exits from the loop and switches instantly.
It'll use in conditions Statements

*Continue Statement*

*Return Statement*
The return statement returns from a method before the end of that method is reached

*Interface*
  • An interface has the following properties:
  • Any class or struct that implements the interface must implement all its members.
  • An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.
  • Interfaces can contain events, indexers, methods, and properties.
  • Interfaces contain no implementation of methods.
  • A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
  • The corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
  • Interfaces can also have base interfaces and an interface can be inherited from more than one base interface.

*Static Class*

  • A static class cannot be instantiated. That means you cannot create an instance of a static class using a new operator.
  • A static class is a sealed class. That means you cannot inherit any class from a static class.
  • A static class can have static members only. Having a non-static member will generate a compiler error.
  • A static class is cannot contain instance constructors.
  • A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

*Abstract Class*
characteristics of abstract classes:
  • An abstract class cannot be instantiated.
  • A class may inherit one abstract class only.
  • An abstract class may contain abstract methods, properties, and events.
  • An abstract class can’t be modified with the sealed class modifier.
  • A derived from an abstract class must implement all inherited abstract methods and accessors.

*Array*

  • An Array in C# is a collection of objects or types. 
  • C# Array elements can be of any type, including an array type. 
  • An array can be Single-Dimensional, Multidimensional or Jagged. 
  • A C# Array can be declared as fixed length or dynamic. 
  • An Array in C# can be a single dimension, multi-dimension, or jagged array. 

*String*

A string class in C# is an object of type System. String. The String class in C# represents a string.


*List*
List<T> class in C# represents a strongly typed list of objects. List<T> provides functionality to create a list of objects, find lost items, sort list, search list, and manipulate list items. In List<T>, T is the type of object.
 
List<T> is a generic class and is defined in the System.Collections.Generic namespace. You must import this namespace in your project to access the List<T> class.
List<T> class constructor is used to create a List object of type T.

*Dictionary*


The Dictionary type represents a collection of keys and values pair of data. 
 
C# Dictionary class defined in the System.Collections.The generic namespace is a generic class and can store any data type in a form of keys and values. Each key must be unique in the collection.
 
Before you use the Dictionary class in your code, you must import the System.Collections.Generic namespace using the following line.
using System.Collections.Generic;    
The Dictionary class constructor takes a key data type and a value data type. Both types are generic so it can be any .NET data type.
 
The following The Dictionary class is a generic class and can store any data type. This class is defined in the code snippet that creates a dictionary where both keys and values are string types.


If you found this article useful, share it.*

MS SQL Server interview questions for developers ~ SoftCodeLearner

SoftCodeLearner: ASP.NET

BookStore ~ SoftCodeLearner

FACEBOOK


Join telegram group -  https://t.me/softwareCodeLearner






No comments:

Post a Comment