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;


No comments:

Post a Comment

API interview questions

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