Code Samples
Code Samples
Inheritance
using System;
namespace Inheritance {
// base class
class Animal {
Console.ReadLine();
}
}
}
Output
I am an animal
My name is Rohu
Abstraction
Abstraction in C# is the process to hide the internal details and show only the
functionality. The abstract modifier indicates the incomplete implementation. The
keyword abstract is used before the class or method to declare the class or method
as abstract.
// Abstract class
abstract class Animal
{
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound(); // Call the abstract method
myPig.sleep(); // Call the regular method
}
}
Output
The pig says: wee wee
Zzz
Sub Topics
C# Interface
Output
drawing ractangle...
drawing circle...
Polymorphism
Sub Topics
Method Overloading
Method overloading allows programmers to use multiple methods with the same
name. The methods are differentiated with their number and type of method
arguments. Method overloading is an example of the polymorphism feature of an
object-oriented programming language.
using System;
namespace MethodOverload {
class Program {
Output
Arguments: 100
Arguments: 100 and 200
Method Overriding
If derived class defines same method as defined in its base class, it is known as
method overriding in C#. It is used to achieve runtime polymorphism. It enables
you to provide specific implementation of the method which is already provided by
its base class.
To perform method overriding in C#, you need to use virtual keyword with base
class method and override keyword with derived class method.
using System;
public class Animal{
public virtual void eat(){
Console.WriteLine("Eating...");
}
}
public class Dog: Animal
{
public override void eat()
{
Console.WriteLine("Eating bread...");
}
}
public class TestOverriding
{
public static void Main()
{
Dog d = new Dog();
d.eat();
}
}
Output
Eating bread...
C# Base
In C#, base keyword is used to access fields, constructors and methods of base
class.
You can use base keyword within instance method, constructor or instance
property accessor only. You can't use it inside the static method.
using System;
public class Animal{
public virtual void eat(){
Console.WriteLine("eating...");
}
}
public class Dog: Animal
{
public override void eat()
{
base.eat();
Console.WriteLine("eating bread...");
}
}
public class TestBase
{
public static void Main()
{
Dog d = new Dog();
d.eat();
}
}
Output
eating...
eating bread...
C# Sealed Class
C# sealed keyword applies restrictions on the class and method. If you create a
sealed class, it cannot be derived. If you create a sealed method, it cannot be
overridden.
using System;
sealed public class Animal{
public void eat() { Console.WriteLine("eating..."); }
}
public class Dog: Animal
{
public void bark() { Console.WriteLine("barking..."); }
}
public class TestSealed
{
public static void Main()
{
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output
Compile Time Error: 'Dog': cannot derive from sealed type 'Animal'
Encapsulation
Encapsulation, in the context of C#, refers to an object's ability to hide data and
behavior that are not necessary to its user. Encapsulation enables a group of
properties, methods and other members to be considered a single unit or object.
get
{
return studentName;
}
set
{
studentName = value;
}
}
get
{
return studentAge;
}
set
{
studentAge = value;
}
}
}
// Driver Class
class GFG {
// Main Method
static public void Main()
{
// creating object
DemoEncap obj = new DemoEncap();
Output
Name: Ankita
Age: 21
Constructor in C#
// Outputs "Mustang"
Data Structures
Queue
// Create a queue
// Using Queue class
Queue my_queue = new Queue();
Output
GFG
1
100
2.4
Geeks123
Stack
class GFG {
// Main Method
static public void Main()
{
// Create a stack
// Using Stack class
Stack my_stack = new Stack();
Output
490.98
1234
G
geeksforgeeks
Geeks
Star Patterns in C#
Example 01
*
**
***
****
*****
******
using System.IO;
using System;
class Program {
static void Main() {
for (int i = 1; i <= 6; ++i) {
for (int j = 1; j <= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
Example 02
******
*****
****
***
**
*
using System.IO;
using System;
class Program {
static void Main() {
for (int i = 6; i >= 1; --i) {
for (int j = 1; j >= i; ++j) {
Console.Write("*");
}
Console.WriteLine();
}
}
}
Example 03
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
using System.IO;
using System;
class Demo {
static void Main() {
int a = 1, spaces, k = 6, i = 0, j = 0;
spaces = k - 1;
for(i=1; i<k*2; i++) {
for(j=1; j<=spaces; j++) {
Console.Write(" ");
}
for(j=1; j<a*2; j++) {
Console.Write("*");
}
Console.WriteLine("");
if(i < k) {
spaces--;
a++;
} else {
spaces++;
a--;
}
}
}
}