Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, July 17, 2020

C# Predicate

C# - Predicate Delegate
predicate is also a delegate like Func and Action delegates. 

It represents a method that contains a set of criteria and checks whether the passed parameter meets those criteria or not. A predicate delegate methods must take one input parameter and return a boolean - true or false.






Monday, September 9, 2019

C#- string,enum

String To Enum Conversion [C#]

This example shows how to convert enum values to string and reversely.

Enum to string

To convert enum to string use simply Enum.ToString method.
[C#]
Animal animal = Animal.Cat;
string str = animal.ToString();  // "Cat"

String to enum

To convert string to enum use static method Enum.Parse. Parameters of this method are enum type, the string value and optionally indicator to ignore case.
[C#]
string str = "Dog";
Animal animal = (Animal)Enum.Parse(typeof(Animal), str);  // Animal.Dog
Animal animal = (Animal)Enum.Parse(typeof(Animal), str, true); // case insensitive

C#-Isnullorempty

IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.Empty (A constant for empty strings)

Syntax:
public static bool IsNullOrEmpty(String str)  
Explanation: This method will take a parameter which is of type System.String and this method will returns a boolean value. If the str parameter is null or an empty string (“”) then return True otherwise return False.
Example:
Input : str  = null         // initialize by null value
        String.IsNullOrEmpty(str)
Output: True

Input : str  = String.Empty  // initialize by empty value
        String.IsNullOrEmpty(str)
Output: True

Monday, September 2, 2019

C# access modifier

6. Access modifier in C#
A way to control access to a class and or its members

public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private
The type or member can be accessed only by code in the same class or struct.

protected
The type or member can be accessed only by code in the same class, or in a class that is derived from that class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.

Type Access Modifier
                     | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | internal  | public, internal

class                | internal  | public, internal

struct               | internal  | public, internal

delegate             | internal  | public, internal

Member accesss modifier
                    | Default   | Permitted declared accessibilities
------------------------------------------------------------------
namespace            | public    | none (always implicitly public)

enum                 | public    | none (always implicitly public)

interface            | public    | none

class                | private   | All¹

struct               | private   | public, internal, private²

delegate             | private   | All¹

constructor          | private   | All¹

interface member     | public    | none (always implicitly public)

method               | private   | All¹

field                | private   | All¹

user-defined operator| none      | public (must be declared public)

Saturday, August 17, 2019

C# -1

1Difference between static and non static class in c#
A C# static class is a class that can't be instantiated. The sole purpose of the class is to provide blueprints of its inherited classes. A static class is created using the "static" keyword in C#.

  • A static class can contain static members only, including static methods, static constructors, and static properties; You can‘t create an object for the static class.
  • a Non-static class can have static members, including static methods, constructors and static properties; You can create an object for the non-static class. You can only access the static members by the class name, not the instance name. 
  • It is more typical to declare a non-static class with some static members, than to declare an entire class as static.

Example of static class
public static class TemperatureConverter
{
    public static double CelsiusToFahrenheit(string temperatureCelsius)
    {
        ...........
    }
    }
}

class TestTemperatureConverter
{
    static void Main()
    {
        ....
        switch (selection)
        {
            case "1":
                F = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine());
                break;
            .....
        }
    }
}

Example of static members(static method, static data) in non-static class
public class Automobile
{
    public static int NumberOfWheels = 4;
    public static int SizeOfGasTank
    {
        get
        {
            return 15;
        }
    }
    // Other non-static fields and properties...
}

Automobile.Drive();
int i = Automobile.NumberOfWheels;

2. Create an instance of class. an example of instance of class
public class Addition
{

}


3. What's a constructor in c#

  • the method to initialize the class data members
  • gets automatically invoked whenever an instance of the class is created
  • a method whose name is the same as the name of its type. Its method signature includes only the method name and its parameter list; it does not include a return type
  • If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. 
  • A class can have any number of constructors
  • parameters for constructors can be none, some and object
Example
class Geek
{
    String name;
    int id;

    Geek()
    {
     }
    Geek(String name, int id)
    {
        this.name = name;
        this.id = id;
    }
    Geeks(Geeks s)
    {
        name = s.name;
        id = s.id;
    }
}

Geek geek1 = new Geek();
Geek geek2 = new Geek ("test", 2);
Geek geek3 = new Geek (geek2);

  • Static constructor<->Instance constructor
Instance constructors are used to create and initialize any instance member variables.


Static constructors
are used to initialize a static class, or static variables in a non-static class.
static constructors are parameterless
cannot be called directly, only be called by the CLR.
It is invoked automatically before the first instance is created or any static members are referenced.

4. What's Method overloading

  • the common way of implementing polymorphism. 
  • do method overloading by defining two or more functions in a class sharing the same name. (same name, different function)
  • distinguish the methods with different method signatures. i.e. the number of the parameters, order of the parameters, and data types of the parameters 
  • different method signatures can have different return type. But you cannot declare two methods with the same signature and different return type.
Example
  public int Add(int a, int b) 
    {
        int sum = a + b;
        return sum;
    }
    public int Add(int a, int b, int c) 
    {
        int sum = a + b + c;
        return sum;
    }
   public double Add(double a, double b, double c) 
    {
        double sum = a + b + c;
        return sum;
    }

5. Data types in c#

Data types in C# is mainly divided into three categories
Value Data Types, Reference Data Types,Pointer Data Type

  • Value Data Types, derived from the class System.ValueType
most often used, fixed length
TypeRepresentsRangeDefault Value
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
char16-bit Unicode characterU +0000 to U +ffff'\0'
decimal128-bit precise decimal values with 28-29 significant digits(-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
double64-bit double-precision floating point type(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32-bit single-precision floating point type-3.4 x 1038 to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
  • Reference Type
don't store the variable value directly
contain a memory address of variable value, refer to a memory location
The built-in reference types are string, object

string type: allows you to assign any string values to a variable,  represents a sequence of Unicode charactersstring s1 = "hello";
object type: it is the base class for all the data types in C#


  • Pointer type

contain a memory address of the variable value.
To get the pointer details we have a two symbols ampersand (&) and asterisk (*).
ampersand (&): It is Known as Address Operator. It is used to determine the address of a variable.
asterisk (*): It also known as Indirection Operator. It is used to access the value of an address.
Syntax
type* identifier;

int* n=&b;


API interview questions

  https://www.katalon.com/resources-center/blog/web-api-testing-interview-questions/ Top 50+ Web API Testing Interview Questions [Ultimate l...