Microsoft C# language(pronounced as C sharp) is an object-oriented programming language used to develop websites, desktop applications, mobile apps, games, etc.. on .NET platform. Introduced along with .NET framework 1.0 in 2002.
C# is syntactically similar to other languages like C, C++, and Java but programmatically different. Having additional advantages of .NET framework features, C# comparatively efficient than others.
Basics of C#
What are the object-oriented features supported by C#?
C# supports the main object-oriented features like Inheritance, Encapsulation, Abstraction, and Polymorphism.
Syntax:
What are the main C# keywords or reserved words?
What are the main data types in C#?
int x;
How to implement 'Inheritance' in C#?
What is an Abstract Class in C#?
What is an Abstract Method?
An abstract method is a method which doesn't support method implementation. It supports only the method signature.
(or) in simple words, a method without a body.
What is an Interface in C#?
In C#,
"An interface is similar to a class without method definitions or a class only with declarations".
Also,
"An interface is a group of abstract members that must be implemented by classes".
Conclusion
using includenamespaces; //the keyword 'using' to include required namespaces
namespace namespacename // namespace declaration
{
class Program // class declaration
{
static void Main(string[] args){} //the default main method
}
}
In C#, all namespaces, classes, methods start with ('{') and end with ('}') curly braces.
Below is the sample program developed in a Console application
Below is the sample program developed in a Console application
using System; // default namespace to include
namespace ConsoleApp1
{
class Program // default class
{
public static int Sum(int x, int y) // sample method to take parameters
{
int z = x + y;
return z;
}
static void Main(string[] args)
{
int x = 5; // sample variables declared with values
int y = 5;
int z = x + y;
Console.Write("Sum = ");
Console.WriteLine(z);
Console.Write("Sum from method call = ");
Console.WriteLine(Sum(10, 10));
Console.Read();
}
}
}
What is '//' and /* */' used for?
In C#, For single-line comments use '//' and use /* */ for multiline comments.
What are the main C# keywords or reserved words?
Keywords:
1. class 13. int 25. delegate 37. bool
2. abstract 14. short 26. event 38. byte
3. virtual 15. enum 27. else 39. long
4. static 16. decimal 28. try 40. float
5. sealed 17. object 29. catch
6. interface 18. const 30. throw
7. public 19. readonly 31. break
8. private 20. if 32. void
9. protected 21. for 33. double
10. Struct 22. while 34. char
11. override 23. internal 35. true
12. string 24. foreach 36. false
Data Types:
1. string 6. doubleHow to create variables in C#?
2. int 7. long
3. short 8. float
4. bool 9. char
5. byte 10. decimal
In C#, we can use the below syntax to create the variables.
datatype variable_name;example:
int x;
We can assign the values to variables using the below syntax.
datatype variable_name = value;
What are the access specifiers in C#?
datatype variable_name = value;
example:
int x = 10;
What are the access specifiers in C#?
Access Specifier or Modifier:
The access specifier or modifier in C# is a keyword used to define the level of access for the classes, methods or variables etc...
The list of access specifiers or modifiers supported by C#:
Access specifiers:1. public
Classes, methods, and variables can be accessed anywhere. No access restrictions at all.
2. private
Restricted to access within the class only. default access specifier for variables and methods.
3. protected
Restricted to access within the class and in the derived classes as well.
4. internal
default access specifier for the class. Restricted to access within the assembly only.
5. protected internal
Restricted to access within the same assembly and in the derived classes in another assembly.
We can use these specifiers to achieve 'Encapsulation' in C#.
Encapsulation:Means that, not to show or reveal the full implementation of an object. More technically, "enclose the group of properties, methods, and other members and provide those as a single object". The basic encapsulation unit in C# is a 'class' i.e. we can encapsulate the properties, members of a class with the help of access specifiers (private, protected...).
What are the default access specifiers for class and variable in C#?
The default access specifier for the class is 'internal' and for the variable is 'private'.
What is Inheritance in C#?
'Inheritance' means a class is allowed to get other classes methods and properties.
C# language supports only single-level and multi-level inheritance, no support for multiple inheritances.
examples:
Single-level Inheritance:
public class A
{}
public class B: A // class B inherited class A. B is allowed to access all A class methods
{}
Multi-level Inheritance:
public class A
{}
public class B: A // class B inherited class A
{}
public class C: B // class C inherited class B
{
}
Multiple Inheritance:
public class A{}
public class B
{}
public class C: A, B // error: multiple inheritance not allowed
{
}
Still, we can achieve this using 'Interface' in C#.
What is an Abstraction and how to implement it in C#?
In simple words, "Abstraction provides a high-level view or interface without providing much implementation details".
To implement abstraction in C#, we can use either an 'Abstract' keyword or an 'Interface'.
We can use the 'Abstract' keyword along with Class or Method.
In simple words, "Abstraction provides a high-level view or interface without providing much implementation details".
To implement abstraction in C#, we can use either an 'Abstract' keyword or an 'Interface'.
We can use the 'Abstract' keyword along with Class or Method.
What is an Abstract Class in C#?
An abstract class is a class that doesn't support object creation and we can only inherit the class.
Along with regular abstract methods, the abstract class allows us to define normal methods as well.
example:Note: all abstract members are declared with 'public' access specifier only, otherwise its an error.
abstract class A
{
public abstract void method1(){}; // abstract method
public abstract void method2(){}; // abstract method
void normalmethods(){}; // normal method
}
A obj = new A(); // error: object instantiation not allowed
class B: A // only inheritance allowed
{
}
What is an Abstract Method?
An abstract method is a method which doesn't support method implementation. It supports only the method signature.
(or) in simple words, a method without a body.
example:
abstract class A{
public abstract void method1(); // only method declaration allowed
public abstract void method2()
{
Console.Write("This is abstract");} //error: cannot declare a body
}
}
Points to remember:
a. We cannot use any other access specifiers with virtual/abstract members other than 'public'.
a. We cannot use any other access specifiers with virtual/abstract members other than 'public'.
What is an Interface in C#?
In C#,
"An interface is similar to a class without method definitions or a class only with declarations".
Also,
"An interface is a group of abstract members that must be implemented by classes".
example:
interface IInter // Interface name ideally start with 'I'
{ void Tester(); // methods
string Name { get; set; } // property
event EventHandler sampleEvent; // event
string this[int count] // indexer
{ get;set; }
}
Points to remember:
a. While creating Interfaces, we are allowed to define methods, properties, events, and indexers only.
b. We can't define any access specifier explicitly. By default, all interface members are 'public' only.
What is the main difference between an Interface & Abstract in C#?
The main difference between these two is the scope or limitation.
"A class can implement more than one interface but a class can inherit from only one abstract class".
How to implement more than one interface or multiple inheritances in C#?
example:
interface InterfaceA
{
void method();
}
interface InterfaceB
{
void method();
}
class ClassC : InterfaceB, InterfaceA // multiple inheritance
{
void InterfaceA.method()
{
Console.Write("This is Interface A method\n");
}
void InterfaceB.method()
{
Console.Write("This is Interface B method");
}
}
class Program
{
static void Main(string[] args)
{
InterfaceA one = new ClassC();
InterfaceB two = new ClassC();
one.method(); // call interface A method
two.method(); // call interface B method
}
}
Note: the above is an example of a solution 'how to access the multiple interfaces with the same method names'.
What is the indexer in C#?
C# indexer is similar to a class property used to access the class member variables in an array-like manner.
(or) C# indexers allows the class objects to be indexed like an array.
Syntax:
<C# access specifier> <C# datatype> this[parameters]
{
get{};
set{};
}
example:
class IndexLists
{
private string[] numberList = new string[10];
public string this[int i]
{
get
{
return numberList[i];
}
set
{
numberList[i] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
IndexLists list = new IndexLists();
list[1] = "One";
list[2] = "Two";
list[3] = "Three";
list[4] = "Four";
list[5] = "Five";
for (int i = 0; i < 6; i++) { Console.WriteLine(list[i]); }
}
}
What is a delegate in C#?
In C#, a delegate is a one of the reference type used to declare as a reference to the group of methods, which are having the same method signature and return type.
syntax:
public delegate returntype delegatename(parameters list);
What is the advantage of delegates?
Instead of invoking or call multiple methods having the same signature and return type one by one, we can create a single delegate type as a reference to the group of methods and invoke the required method at run time.
How the delegate works in C#?
Step1:
Create a delegate signature similar to the multiple methods
delegate signature:
delegate void delegateName(string name)
sample method signatures:
static void refMethod1(string name1)
{
//print statement
}
public void refMethod2(string name2)
{
//print statement
}
Step2:
Create an instance to the delegate to invoke the method signatures at runtime
example:
delegateName delObj = new delegate(refMethod1); // to invoke refMethod1 at runtime
delegateName delObj = new delegate(refMethod2); // to invoike refMethod2 at runtime
another way of method invocation
delegateName delObj = refMethod1 ; // direct method assignment
We can use lambda expressions as well to instantiate delegates
delegateName delObj = name => {// print the variable name}
Here => is called lambda declaration operator, the 'name' is just a variable, delObj is delegate obj.
What is an Extension Method in C#?
In C#, the extension method is a method to add to other methods as an extension. The advantage is that no need to modify or re-execute the actual methods, also there is no need for new method creations.
Just with a few lines of code, we can extend or add additional functionalities to existing methods.
In C#, LINQ(Language integrated queries) query operators are mostly used extension methods.
There are still many more concepts in C# that are feature-rich and efficient for simple programming to complex programming. Tune for more add-ons.
No comments:
Post a Comment