Tag Archives: C#

C# – Abstract Class vs Interface

Abstract Class:

  • An abstract class is one that is intended only to be a base class of other classes, and the “abstract” modifier is used to make a class abstract. An abstract class cannot be a sealed class. An abstract method cannot be private. The access modifier of the abstract method should be same in both the abstract class and its derived class. An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
  • An abstract class can have abstract members as well non abstract members.
  • It can have constants, read only variables, instance variables and static variables.
  • We cannot create an object of abstract class.
  • Abstract class contains Constructors.
  • Only Complete Member of abstract class can be Static.
  • A class can inherit only one abstract class.
  • A class inheriting an abstract class has to override the abstract methods from abstract class while implementing them.
  • We go for Abstract classes on such situations, When we have a requirement where our base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.

Interface:

  • An interface is an entity that is defined by the word Interface, it is not a class. An interface contains only the signatures of methods. Interface members cannot have any access specifier like public, private, protected, internal or protected internal. By default all the members of an interface are Public.
  • In an interface all the members are implicitly abstract.
  • It cannot have member variables.
  • We can create an object of interface by type casting it to class name.
  • Interface doesn’t contains Constructors.
  • Member of interface can not be Static.
  • A class can implement any number of Interfaces.
  • A class implementing an interface has to implement all the methods of the interface compulsorily.
  • We go for Abstract classes on such situations, If our child classes should all implement a certain group of methods but each of the child classes is free to provide its own implementation then use interfaces.

These are the basic differences between abstract class and interface.

How to check if another instance of the application is running?

Sometimes we need to check the application instance running status.

var isExists = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1;

If you want to kill the currently loading process instantly application process, you can use below code.

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();

You need to add a reference to System.Core.dll for the .Count() extension method. Alternatively, you can use the .Length property.

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 5.00 out of 5)
Loading...

Difference between Constant, Readonly and Static in C#

Constants:

  • Classes and structs can declare constants as members.
  • Constant fields or variables must be assigned a value at the time of declaration and after that they cannot be modified.
  • By default constant is static and we cannot change the value of a const variable throughout the entire program.
  • Constants can be marked as public, private, protected, internal, or protected internal access modifiers.

Readonly:

  • The readonly keyword is a modifier, which we can use on fields.
  • A readonly field can be initialized either at the time of declaration or with in the constructor of same class.
  • We can change readonly field value during runtime or we can assign it at run time but only through the non-static constructor.

Static:

  • The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
  • If the static keyword is applied to a class, all the members of the class must be static and Static methods can only access static members of same class.
  • Static properties are used to get or set the value of static fields of a class.
  • Static constructor can’t be parametrized. Access modifiers can not be applied on Static constructor, it is always a public default constructor which is used to initialize static fields of the class.

 

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)
Loading...

Salesforce Enterprise WSDL Error in .Net – Unable to generate a temporary class (result=1)

“Unable to generate a temporary class (result=1)” is returned when .Net integration tries to parse the Enterprise WSDL version 32.0
In Winter ’15 (API version 32.0), a number of additional sObjects are available including ListViewRecord, whose definition is as follows:

<complextype name="ListViewRecord">
    <sequence>
        <element maxoccurs="unbounded" name="columns" type="tns:ListViewRecordColumn"></element>
    </sequence>
</complextype>

Here is the solution:

Change above xml to below format:

<complextype name="ListViewRecord">
    <sequence>
        <element maxoccurs="unbounded" name="columns" type="tns:ListViewRecordColumn"></element>
    </sequence>
<xsd:attribute name="tmp" type="xsd:string"></xsd:attribute>
</complextype>

As a result .Net integration fail due to a bug in .NET’s XmlSerializer as described in the following links: https://connect.microsoft.com/VisualStudio/feedback/details/471297

download

How to get the system MAC address using C#?

Media Access Control (MAC) address is a unique identifier assigned to most network adapters or network interface cards (NICs) by the manufacturer for identification and used in the Media Access Control protocol sub-layer. If assigned by the manufacturer, a MAC address usually encodes the manufacturer’s registered identification number. It also be known as an Ethernet Hardware Address (EHA).

In this article, I will demonstrate how to get system MAC address using C#.

public string GetMACAddress()
{
    var macAddress = string.Empty;
    NetworkInterface[] onjNI = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in onjNI)
    {
        if (string.IsNullOrEmpty(macAddress))
        {
           IPInterfaceProperties objIP = adapter.GetIPProperties();
           macAddress = adapter.GetPhysicalAddress().ToString();
 
           if(!string.IsNullOrEmpty(macAddress))
           {
               break;
           }
        }
    }
  return macAddress;
 }

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 5.00 out of 5)
Loading...