Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

C# this & static

C# this

In C# programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C#.

  • It can be used to refer current class instance variable. It is used if field names (instance variables) and parameter names are same, that is why both can be distinguish easily.
  • It can be used to pass current object as a parameter to another method.
  • It can be used to declare indexers.

Let’s see the example of this keyword in C# that refers to the fields of current class.

 

using System;  
public class Employee  {  
    public int id;   
    public String name;  
    public float salary;  
    public Employee(int id, String name,float salary)  {  
        this.id = id;  
        this.name = name;  
        this.salary = salary;  
    }  
    public void display()  {  
        Console.WriteLine(id + " " + name+" "+salary);  
    }  
}  
class TestEmployee{  
    public static void Main(string[] args)  {  
        Employee e1 = new Employee(101, "Sonoo", 890000f);  
        Employee e2 = new Employee(102, "Mahesh", 490000f);  
        e1.display();  
        e2.display();  

    }  
}  

C# static

In C#, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C#, static can be field, method, constructor, class, properties, operator and event.

Note: Indexers and destructors cannot be static.

A field which is declared as static, is called static field. Unlike instance field which gets memory each time whenever you create object, there is only one copy of static field created in the memory. It is shared to all the objects.

It is used to refer the common property of all objects such as rateOfInterest in case of Account, companyName in case of Employee etc.

Let’s see the simple example of static field in C#.

 

using System;  
public class Account  {  
    public int accno;   
    public String name;  
    public static float rateOfInterest=8.8f;  
    public Account(int accno, String name)  {  
        this.accno = accno;  
        this.name = name;  
    }  

    public void display()  {  
        Console.WriteLine(accno + " " + name + " " + rateOfInterest);  
    }  
}  
class TestAccount{  
    public static void Main(string[] args)  {  
        Account a1 = new Account(101, "Sonoo");  
        Account a2 = new Account(102, "Mahesh");  
        a1.display();  
        a2.display();  

    }  
}  

Output

101 Sonoo 8.8
102 Mahesh 8.8

C# static class

The C# static class is like the normal class but it cannot be instantiated. It can have only static members. The advantage of static class is that it provides you guarantee that instance of static class cannot be created.

  • C# static class contains only static members.
  • C# static class cannot be instantiated.
  • C# static class is sealed.
  • C# static class cannot contain instance constructors.

Question

In C#, we cannot create an object of the __ class.

 

Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test